Gimp-Forum.net
Help with Image stacking - Printable Version

+- Gimp-Forum.net (https://www.gimp-forum.net)
+-- Forum: GIMP (https://www.gimp-forum.net/Forum-GIMP)
+--- Forum: Extending the GIMP (https://www.gimp-forum.net/Forum-Extending-the-GIMP)
+---- Forum: Scripting questions (https://www.gimp-forum.net/Forum-Scripting-questions)
+---- Thread: Help with Image stacking (/Thread-Help-with-Image-stacking)



Help with Image stacking - csoulman - 07-18-2019

I've been using GIMP for a long time and have some familiarity using BIMP but I recently came across something I need to automate that it can't without scripting or a plugin.  I would like to start with an image with a white background and stack multiple PNG images on top of each other.  For what I need, it will just be the same image stacked on top of itself a variable number of times.  Basically like this:

----
Start with a new, square dimension image.  
Take a PNG file with a transparent background
Put the first image near the top left corner
Then place another copy a few pixels down and to the right (variable pixels and number of times).
----

Something tells me this can be done with a script or plugin.  I do have some programming experience and I'm learning Python now, but if a method, plugin, or script exists I would be able to use I would appreciate being steered in the right direction.  Any help you can give me would be greatly appreciated!


RE: Help with Image stacking - Ofnuts - 07-18-2019

You just need to set the offsets of the layers, for instance, to stack the layers in a diagonal:

Code:
for i,l in enumerate(image.layers):
   l.set_offsets(i*stepSize,i*stepSize)

Plenty of example Python plugins here. If any questions about them, just ask below.


RE: Help with Image stacking - tmanni - 07-18-2019

A non-optimized way to do it :
Code:
#!/usr/bin/env python
#-*- coding: utf8 -*-

from gimpfu import *


def your_plug_in_function(image, filename, n_copy, x_start, y_start, h_offset, v_offset):
   image.undo_group_start()

   offset_x = x_start
   offset_y = y_start

   if not n_copy:
       return

   for i in range(n_copy):
       layer = pdb.gimp_file_load_layer (image, filename)
       pdb.gimp_layer_set_offsets(layer, offset_x, offset_y)
       image.insert_layer(layer)
       offset_x += h_offset
       offset_y += v_offset

   image.undo_group_end()


register(
   "python_fu_plug_in_name_here",
   "the short plug-in description",
   "the long plug-in description",
   "your name here ",
   "your name here",
   "the year",
   "the plug-in name",
   "*",
   [
       (PF_IMAGE,    "image",    "active image", None),
       (PF_FILENAME, "filename", "File to load", None),
       (PF_INT,      "n_copy",   "Number of image copies", 5),
       (PF_INT,      "x_start",  "x position of the first layer", 0),
       (PF_INT,      "y_start",  "y position of the first layer", 0),
       (PF_INT,      "h_offset", "Horizontal spacing", 1),
       (PF_INT,      "v_offset", "Vertical spacing", 1),
   ],
   [],
   your_plug_in_function, menu="<Image>/csoulman")


main()

Save the above code in a file in the appropriate place (sorry, I'm linux addict, I don't know where windows user must save their plug-ins)
Ensure the file has execution rights.
Run GIMP : you will have a menu called 'csoulman' with one entry : 'the plug-in name'

To improve it :
- load the image only once, then use layer.copy()
- check pdb.gimp_file_load_layer success