The sprites are not aligned to a grid, they are packed as tightly as possible to preserve space (1px spacing). I tried using Alferd SpriteSheet Unpacker but it didn't work on my image because the format is nonstandard even though it's just a normal png image. I am trying to write a script with flood fill algorithm to determine where the sprites are, but I don't know how to detect where the images start and end.
func separate(image: Image) -> void:
var images := 0
image.lock()
var start := {"position": Vector2(0, 0), "previous_blank": true}
var checked_pixels := []
var pixels := [start]
while not pixels.empty():
var dict: Dictionary = pixels.pop_back()
var pixel: Vector2 = dict.position
checked_pixels.append(dict)
var blank: bool = image.get_pixelv(pixel).a == 0
if not blank and dict.previous_blank:
images += 1
var neighbors := []
if not pixel.x == image.get_width() - 1:
neighbors.append({"position": Vector2(pixel.x + 1, pixel.y), "previous_blank": blank})
if not pixel.y == image.get_height() - 1:
neighbors.append({"position": Vector2(pixel.x, pixel.y + 1), "previous_blank": blank})
for n in neighbors:
var found_match := false
for p in pixels:
if p.position == n.position:
found_match = true
break
if found_match:
continue
for c in checked_pixels:
if c.position == n.position:
found_match = true
break
if found_match:
continue
pixels.append(n)
image.unlock()
print(images)