Source

python-fu-divide-layer / layer-slice.py

Full commit
from gimpfu import *
import gtk
import os
import collections
import ctypes

def slice_layer(img, drawable, width, height) :

        MessageBox = ctypes.windll.user32.MessageBoxA

        hslices = img.width / width
        if 0 != (img.width % width) :
                hslices += 1

        vslices = img.height / height
        if 0 != (img.height % height) :
                vslices += 1

        #MessageBox(None, 'vertical slices = ' + str(vslices) + '\nhorizontal slices = ' + str(hslices), 'Window title', 0)

        layer_to_slice = img.active_layer

        for i in range(vslices) :
                for j in range(hslices) :
                        #MessageBox(None, 'Slice (' + str(i) + ',' + str(j) + ')', 'Window title', 0)
                        layer = layer_to_slice.copy()
                        layer.name = 'slice (' + str(j) + ',' + str(i) + ')'
                        img.add_layer(layer, len(img.layers))
                        layer.resize(width, height, -1 * (j * width), -1 * (i * height))


        return None

register(
        #name
        "layer_slice",
        #blurb
        "<blurb>",
        #help
        "<help text>",
        #author
        "<author>",
        #copyright
        "<copyright>",
        #date
        "<date>",
        #menu path
        "<Image>/Layer/Slice",
        #image types
        "*",
        #params
        [
                #(type, name, description, default [, extra])
            #(PF_IMAGE, "image", "Input image", None),
            #(PF_DRAWABLE, "drawable", "Input drawable", None),
            (PF_INT, "width", "px slice width", None),
            (PF_INT, "height", "pc slice height", None),
        ],
        #results
        [],
        #function
        slice_layer
)

main()