0
\$\begingroup\$

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)
\$\endgroup\$
11
  • \$\begingroup\$ I looked at the code for Alferd SpriteSheet Unpacker but I don't understand it. D: \$\endgroup\$
    – Emily
    Commented Dec 1, 2022 at 0:24
  • \$\begingroup\$ Wouldn't your floodfill detect where images start and end? When you find an unmarked, non-transparent pixel, begin a floodfill spilling into all adjacent non-transparent pixels, marking them as you go. When the floodfill finishes, all the pixels it flooded are one sprite. Then you progress to the next unmarked non-transparent pixel and repeat. How does this differ from what you tried or what you need? \$\endgroup\$
    – DMGregory
    Commented Dec 1, 2022 at 0:29
  • \$\begingroup\$ The problem is is that it doesn't end. \$\endgroup\$
    – Emily
    Commented Dec 1, 2022 at 0:35
  • \$\begingroup\$ How does it not end? Every time you visit a non-transparent pixel, you mark it, and you skip visiting already-marked pixels. So the number of unmarked pixels must strictly decrease to zero, after which the algorithm terminates. \$\endgroup\$
    – DMGregory
    Commented Dec 1, 2022 at 1:12
  • \$\begingroup\$ When I run it, it just keeps going and going. There must be an error somewhere. \$\endgroup\$
    – Emily
    Commented Dec 1, 2022 at 1:25

0

You must log in to answer this question.

Browse other questions tagged .