It is currently Mon Aug 17, 2026 4:42 pm


All times are UTC - 5 hours [ DST ]



Post new topic Reply to topic  [ 8 posts ] 
Author Message
 Post subject: Batch script: add a predefined image layer, set layer mode, export.
PostPosted: Sat Jul 19, 2014 6:14 pm  (#1) 
Offline
GimpChat Member

Joined: Jul 19, 2014
Posts: 31
Please can someone help me create this script? I want GIMP to do the following to each file in a folder:

-Import an existing image as a layer above the image that's being processed. This layer is a .png file with a fixed name and location.
-Set the layer mode of the new layer to "divide".
-Flatten all layers
-Change the colour mode to Indexed and select a preset Palette.
-Export the file to a different directory as a .png (and close the opened file).

It's not exactly "my first script" stuff so I'm hoping someone here can save me a dozen hours :-)

Thank you!


Share on Facebook Share on Twitter Share on Orkut Share on Digg Share on MySpace Share on Delicious Share on Technorati
Top
 Post subject: Re: Batch script: add a predefined image layer, set layer mode, export
PostPosted: Sat Jul 19, 2014 11:45 pm  (#2) 
Offline
Script Coder
User avatar

Joined: Apr 23, 2010
Posts: 1553
Location: not from Guildford after all
There are three basic parts to every script: defining what your script does, registering your script with GIMP's Procedural DataBase (PDB), and registering a menu command that will run your script.

Here is some code that attempts what you described (it has not been tested and is intended as a starting point for you to study).
(define (script-fu-lumo-save image layer)
  (let ((new-layer (car (gimp-file-load-layer RUN-NONINTERACTIVE image "fixed-filename"))))
    (gimp-image-insert-layer image new-layer 0 0)
    (gimp-layer-set-mode new-layer DIVIDE-MODE)
    (set! layer (car (gimp-image-flatten image)))
    (gimp-convert-indexed image NO-DITHER CUSTOM-PALETTE 0 FALSE FALSE "fixed-palette-name")
    (let ((filename (car (last (strbreakup "fixed-filename" DIR-SEPARATOR)))))
      (set! filename (unbreakupstr (butlast (strbreakup filename ".")) "."))
      (set! filename (string-append "fixed-directory-name" DIR-SEPARATOR filename ".png"))
      (file-png-save2 RUN-NONINTERACTIVE
                      image
                      layer
                      filename
                      filename
                      FALSE ; interlace
                      9 ; compression
                      FALSE ; bkgd
                      FALSE ; no alpha
                      FALSE ; offs
                      FALSE ; phys
                      FALSE ; time
                      TRUE  ; comment
                      FALSE ; svtrans
                      ))))


The two registration procedures would be similar to the following:
(script-fu-register "script-fu-lumo-save"
  "Lumo's save as Indexed"
  "Save an indexed version of file per Lumo"
  "Contact"
  "Author"
  "Date"
  "*"
  SF-IMAGE    "Image"    0
  SF-DRAWABLE "Layer" 0
  )

(script-fu-menu-register "script-fu-lumo-save"
"<Image>/Filters/Misc/Lumo"
)


Note: it is not possible for a script to close the opened file (only files that the script opens itself can be closed by a script).

_________________
Any sufficiently primitive technology is indistinguishable from a rock.


Top
 Post subject: Re: Batch script: add a predefined image layer, set layer mode, export
PostPosted: Sun Jul 20, 2014 5:26 am  (#3) 
Offline
GimpChat Member
User avatar

Joined: May 10, 2013
Posts: 1435
Location: FInland
Not sure but I can think of a routine which could save You time. You´d be using existing filtrer(s).
1. Put the processing images in one folder, open as layers to have a 1 multilayer image.
2. If You are using same bottom image for all pngs, just open the image.
3. Use Animation Merge script by Will, set merge type to divide.

Or use `Edit attributes of Layers´ by Bates (if You need custom opacity for divide) and saulgoodes combine background. Does the same trick.

No need to flatten. Then You can set mode to indexed using the palette You want.
Export all layers as png. I use AniTools by Lars for this, but there are dozen of these export scripts available.

If You have different background image for each overlaying png, then it´s bit more complex but not much harder. Just put em in order in 2 different folders, load both separately as multilayer images and do from step 3.


Top
 Post subject: Re: Batch script: add a predefined image layer, set layer mode, export
PostPosted: Sun Jul 20, 2014 6:21 am  (#4) 
Offline
GimpChat Member

Joined: Jul 19, 2014
Posts: 31
Thank you both. I will try these out a little later :)


Top
 Post subject: Re: Batch script: add a predefined image layer, set layer mode, export
PostPosted: Mon Aug 11, 2014 3:54 pm  (#5) 
Offline
GimpChat Member

Joined: Jul 19, 2014
Posts: 31
I've been using Nidhogg's solution quite happily, but I'm now at a point where I want to process more files than GIMP can handle without crashing (on my system).

I'm trying to piece Saulgoode's solution together, but so far the last level of the menu entry is always greyed out. I made both the "fixed-filename" fields equal to the path of the top image, set the directory as the one containing the images to be processed, and I set the palette name.

Can anyone give a hint as to what may be wrong?
Thanks


Top
 Post subject: Re: Batch script: add a predefined image layer, set layer mode, export
PostPosted: Mon Aug 11, 2014 6:02 pm  (#6) 
Offline
Script Coder
User avatar

Joined: Oct 25, 2010
Posts: 4812
saulgoode wrote:
There are three basic parts to every script: defining what your script does, registering your script with GIMP's Procedural DataBase (PDB), and registering a menu command that will run your script.

Here is some code that attempts what you described (it has not been tested and is intended as a starting point for you to study).
(define (script-fu-lumo-save image layer)
  (let ((new-layer (car (gimp-file-load-layer RUN-NONINTERACTIVE image "fixed-filename"))))
    (gimp-image-insert-layer image new-layer 0 0)
    (gimp-layer-set-mode new-layer DIVIDE-MODE)
    (set! layer (car (gimp-image-flatten image)))
    (gimp-convert-indexed image NO-DITHER CUSTOM-PALETTE 0 FALSE FALSE "fixed-palette-name")
    (let ((filename (car (last (strbreakup "fixed-filename" DIR-SEPARATOR)))))
      (set! filename (unbreakupstr (butlast (strbreakup filename ".")) "."))
      (set! filename (string-append "fixed-directory-name" DIR-SEPARATOR filename ".png"))
      (file-png-save2 RUN-NONINTERACTIVE
                      image
                      layer
                      filename
                      filename
                      FALSE ; interlace
                      9 ; compression
                      FALSE ; bkgd
                      FALSE ; no alpha
                      FALSE ; offs
                      FALSE ; phys
                      FALSE ; time
                      TRUE  ; comment
                      FALSE ; svtrans
                      ))))


The two registration procedures would be similar to the following:
(script-fu-register "script-fu-lumo-save"
  "Lumo's save as Indexed"
  "Save an indexed version of file per Lumo"
  "Contact"
  "Author"
  "Date"
  "*"
  SF-IMAGE    "Image"    0
  SF-DRAWABLE "Layer" 0
  )

(script-fu-menu-register "script-fu-lumo-save"
"<Image>/Filters/Misc/Lumo"
)


Note: it is not possible for a script to close the opened file (only files that the script opens itself can be closed by a script).


The OP wants to process all files in a directory. Since the script would load them, it would be able to close them (or more accurately to delete the image in memory).

_________________
Image


Top
 Post subject: Re: Batch script: add a predefined image layer, set layer mode, export
PostPosted: Mon Aug 11, 2014 6:05 pm  (#7) 
Offline
Script Coder
User avatar

Joined: Oct 25, 2010
Posts: 4812
Lumo wrote:
Please can someone help me create this script? I want GIMP to do the following to each file in a folder:

-Import an existing image as a layer above the image that's being processed. This layer is a .png file with a fixed name and location.
-Set the layer mode of the new layer to "divide".
-Flatten all layers
-Change the colour mode to Indexed and select a preset Palette.
-Export the file to a different directory as a .png (and close the opened file).

It's not exactly "my first script" stuff so I'm hoping someone here can save me a dozen hours :-)

Thank you!

Why Gimp? This kind of processing is best handled by ImageMagick and a shell script (.BAT, in Windows). It will take you less time to figure out the necessary ImageMagick call (they have a nice forum, otherwise) that to come up with the equivalent Gimp script.

Start here: http://www.imagemagick.org/Usage/compose/

Otherwise, if the number of files is reasonable (ie, the whole set fits in memory) (or if you can work in reasonably sized sets):

- load the additional layer in one image
- in a second image, use File>Open as layers to load all (or a reasonably sized subset of) the images you want to process
- use my interleave-layers script (title mode) to interleave a copy of you additional layer and merge it down with divide mode
- use some script to save the resulting layers as PNGs (I haven't got one, but such a script isn't hard to find).

_________________
Image


Top
 Post subject: Re: Batch script: add a predefined image layer, set layer mode, export
PostPosted: Tue Aug 12, 2014 1:21 pm  (#8) 
Offline
GimpChat Member

Joined: Jul 19, 2014
Posts: 31
I've finally worked out how to do this using Photoshop macros. It's going to take the PC several days, but that's better than me spending several more hours trying to do it in FOSS ;-)

I'm still using GIMP for all the other things I'm doing though.

There's no "close thread" button...


Top
Post new topic Reply to topic  [ 8 posts ] 

All times are UTC - 5 hours [ DST ]



* Login  



Powered by phpBB3 © phpBB Group