Search code examples
gimppython-fu

Pixels regions comparision


I'm trying to write a python script for GIMP, who's aim is to slice a picture into a tileset (identify each unique 16x16 tiles in a picture).

So far, I'm able to read tiles (in fact a 16x16 pixels region) and write it somewhere. But all my attempts at comparing tiles failed.

Did I miss Something ? My script is as follow:

#!/usr/bin/env python

from gimpfu import *

    # compare 2 tiles,
    # return 1 if identical, false otherwise
def tileCompare(tile1, tile2):
    if(tile1 == tile2):
        return 1
    return 0

     # return tile at (x, y) coordinates
def readTile(layer, x, y):
    pr = layer.get_pixel_rgn(x,y,16,16)
    return pr[x:x+16, y:y+16]

    # write tile at (x, y) coordinates on given layer
def writeTile(layer, x, y, tile):    
    pr = layer.get_pixel_rgn(x,y,16,16)
    pr[x:x+16, y:y+16] = tile

def TilesSlicer(sourceLayer, targetLayer):
        # Actual plug-in code will go here

        # iterate tiles (result in tileSource)
    for x in range(0, sourceLayer.width, 16):
        for y in range(0, sourceLayer.height, 16):
            tileSource = readTile(sourceLayer, x, y)
            found = 0
                # iterate tiles again (result in tileIterator)
            for a in range(0, sourceLayer.width, 16):
                for b in range(0, sourceLayer.height, 16):
                    tileIterator = readTile(sourceLayer, x, y)
                        # compare tiles
                        # if identical and not yet found
                        # write it in the target layer
                    if (tileCompare(tileSource, tileIterator) == 1):
                        if(found == 0):
                            writeTile(tileIterator, a, b, tileSource)
                        found = 1

register(
    "TilesSlicer",
    "Tiles slicer",
    "Slice a picture into tiles",
    "Fabrice Lambert",
    "Fabrice Lambert",
    "April 2019",
    "Tiles slicer...",
    "RGB*",
    [
    (PF_DRAWABLE, "sourceLayer", "Source Layer: ", None),
    (PF_DRAWABLE, "targetLayer", "Target Layer: ", None),
    ],
    [],
    TilesSlicer,
        menu="<Image>/Filters/My Scripts")

main()

Thanks for your suggestions.


Solution

  • Nvm,

    I found the problem:
    tileIterator = readTile(sourceLayer, a, b)
    instead of:
    tileIterator = readTile(sourceLayer, x, y)