It is currently Sat Jun 22, 2024 5:54 am


All times are UTC - 5 hours [ DST ]



Post new topic Reply to topic  [ 8 posts ] 
Author Message
 Post subject: Iterate through a folder of images
PostPosted: Mon Jun 07, 2021 2:09 pm  (#1) 
Offline
New Member

Joined: Jun 07, 2021
Posts: 4
GIMP Version: 2.10.6
Operating System: Windows
OS Version: Win 10
GIMP Experience: New User



Hi everyone,

This is my time trying to write scripts for GIMP. I have 600 PNG files, and would like to run some very specific processes on them and then overwrite the original files. After an afternoon of headache, I've managed to run the script (copying it into the Script-Fu console) and make it work on 1 image. Now I'd like to iterate these processes for a whole folder of images. Here is what was running successfully in the console:

(define image (car (gimp-file-load RUN-NONINTERACTIVE "D:/Temp/Test.png" "Test.png")))

(gimp-display-new image)

(define drawable (car (gimp-image-get-active-layer image)))

(gimp-fuzzy-select drawable 1 1 1 CHANNEL-OP-ADD TRUE FALSE 0 FALSE)

(gimp-image-select-rectangle image CHANNEL-OP-ADD 11560 8950 2480 980)

(gimp-selection-invert image)

(gimp-selection-grow image 100)

(gimp-selection-invert image)

(gimp-edit-clear drawable)

(gimp-fuzzy-select drawable 1 1 1 CHANNEL-OP-ADD TRUE FALSE 0 FALSE)

(gimp-fuzzy-select drawable 1 9930 1 CHANNEL-OP-ADD TRUE FALSE 0 FALSE)

(gimp-fuzzy-select drawable 14040 9930 1 CHANNEL-OP-ADD TRUE FALSE 0 FALSE)

(gimp-fuzzy-select drawable 14040 1 1 CHANNEL-OP-ADD TRUE FALSE 0 FALSE)

(gimp-edit-clear drawable)

(gimp-file-save RUN-NONINTERACTIVE image drawable "D:/Temp/Test.png" "Test.png")

I know this knowledge is out there somewhere, but I've been struggling to make it work for like 10 hours. Thanks in advance for any help.


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: Iterate through a folder of images
PostPosted: Mon Jun 07, 2021 6:22 pm  (#2) 
Offline
GimpChat Member
User avatar

Joined: Nov 04, 2015
Posts: 1361
Welcome to Gimp Chat Fejetlenfej.
Something to consider is the image size Gimp will be working with. Depending on the width/height of the PNGs that could be huge. You need a lot of ram for images of that size.
With this method I could run different filters sequentially.
So I'm running my scripts completely outside of Gimp but in a way thats what you want to do.

Have a look at the Gimp batch mode.
You will definitely get ideas from that :bigthup


Top
 Post subject: Re: Iterate through a folder of images
PostPosted: Tue Jun 08, 2021 7:02 am  (#3) 
Offline
Script Coder
User avatar

Joined: Oct 25, 2010
Posts: 4750
There are likely much better non-Gimp tools for this (Python libraries such as pillow). What are you trying to do exactly (looks like you are trying to remove a background)

Also:
(gimp-selection-invert image)
(gimp-selection-grow image 100)
(gimp-selection-invert image)

.. looks like it could be replaced by a single call to gimp-selection-shrink

_________________
Image


Top
 Post subject: Re: Iterate through a folder of images
PostPosted: Tue Jun 08, 2021 7:33 am  (#4) 
Offline
New Member

Joined: Jun 07, 2021
Posts: 4
First of all, I've never used this forum before, but thanks for your quick and helpful answers. I'm a mapmaker, and have hundreds of very large images (usually 14043 x 9933 pixels [A0 size for prints]). It's always a map in the middle, with either black or white background and my logo at the bottom right corner. I'm trying to keep the map only, plus a 100 pixels wide buffer of the background colour around it. Stickerize, if you will. See attached image. My PC is strong, so it runs quite fast, I'd be fine leaving it on for a few nights until it finishes with all the PNGs. If I'm not mistaken, there's a way to close the image '(gimp-image-delete image)' (?), so RAM usage shouldn't be an issue, as it's only one image that's open at any given point. Do correct me if I'm wrong.

Thanks for the GMIC and other tips, but basically I have no idea what I'm doing, so I wouldn't go too deep if I don't have to. :) I will read up on it in a moment, but if I understand this correctly I might be very close to the solution, something line insert something like this at the beginning:

(let* ((filelist (cadr (file-glob pattern 1))))
(while (not (null? filelist))
(let* ((filename (car filelist))

and insert something like this at the end:

(set! filelist (cdr filelist)))))

but I just can't figure it out. As I said, no idea what I'm doing. I will read everything you sent, but I'd really appreciate some help with iterating the code above.

Thanks again!


Attachments:
File comment: Before/After
Sample.PNG
Sample.PNG [ 688 KiB | Viewed 5669 times ]
Top
 Post subject: Re: Iterate through a folder of images
PostPosted: Thu Jun 10, 2021 10:29 am  (#5) 
Offline
New Member

Joined: Jun 07, 2021
Posts: 4
If anyone is still reading this, I still haven't managed to make this script work, and would appreciate any help.


Top
 Post subject: Re: Iterate through a folder of images
PostPosted: Fri Jun 11, 2021 4:19 am  (#6) 
Offline
Script Coder
User avatar

Joined: Oct 25, 2010
Posts: 4750
Can't help much with script-fu, I use python-fu, much simpler(*) and powerful.

(*) Script-fu is Scheme which is LISP which was invented in 1958. Python was invented in 1991. Things have changed a bit in between.

_________________
Image


Top
 Post subject: Re: Iterate through a folder of images
PostPosted: Fri Jun 11, 2021 4:20 am  (#7) 
Offline
Script Coder
User avatar

Joined: Jun 22, 2010
Posts: 1171
Location: Here and there
In order for us to be able to give you help, you need to us give more information.

What doesn't work? - are you able to work through the list of files?

Attach all your code, not just fragments.

I've attached a sample batch processing template, see if that helps you.


Attachments:
batch_template.scm [2.59 KiB]
Downloaded 94 times
Top
 Post subject: Re: Iterate through a folder of images
PostPosted: Mon Jun 14, 2021 9:38 am  (#8) 
Offline
New Member

Joined: Jun 07, 2021
Posts: 4
Thanks for your help everyone. I've managed to make this work, with the following code:

---

(script-fu-register
"script-fu-batch-sticker-landscape" ;func name
"Batch Sticker Landscape" ;menu label
"Stickerize images" ;description
"GG" ;author
"copyright Meh" ;copyright notice
"June 10, 2021" ;date created
"" ;image type that the script works on
SF-STRING "Files" "D:/*.png" ;folder to stickerize

)

(script-fu-menu-register "script-fu-batch-sticker-landscape" "<Image>/File/Batch")

(define (script-fu-batch-sticker-landscape pattern)
(let* ((filelist (cadr (file-glob pattern 1))))
(while (not (null? filelist))
(let* ((filename (car filelist))
(image (car (gimp-file-load RUN-NONINTERACTIVE
filename filename)))
(drawable (car (gimp-image-get-active-layer image))))

(gimp-fuzzy-select drawable 1 1 1 CHANNEL-OP-ADD TRUE FALSE 0 FALSE)

(gimp-image-select-rectangle image CHANNEL-OP-ADD 11560 8950 2480 980)

(gimp-selection-invert image)

(gimp-selection-grow image 100)

(gimp-selection-invert image)

(gimp-edit-clear drawable)

; Selection is sometimes not perfect, so go through the edges
; and add everything that might have been missed

(gimp-fuzzy-select drawable 1 1 1 CHANNEL-OP-ADD TRUE FALSE 0 FALSE)

(gimp-fuzzy-select drawable 1 9930 1 CHANNEL-OP-ADD TRUE FALSE 0 FALSE)

(gimp-fuzzy-select drawable 14040 9930 1 CHANNEL-OP-ADD TRUE FALSE 0 FALSE)

(gimp-fuzzy-select drawable 14040 1 1 CHANNEL-OP-ADD TRUE FALSE 0 FALSE)

; check the very last pixels on each side for grey line

(gimp-fuzzy-select drawable 1 1 1 CHANNEL-OP-ADD TRUE FALSE 0 FALSE)

(gimp-fuzzy-select drawable 1 9932 1 CHANNEL-OP-ADD TRUE FALSE 0 FALSE)

(gimp-fuzzy-select drawable 14042 9932 1 CHANNEL-OP-ADD TRUE FALSE 0 FALSE)

(gimp-fuzzy-select drawable 14042 1 1 CHANNEL-OP-ADD TRUE FALSE 0 FALSE)

(gimp-edit-clear drawable)

(gimp-file-save RUN-NONINTERACTIVE
image drawable filename filename)
(gimp-image-delete image))
(set! filelist (cdr filelist)))))

---

This registers the script with GIMP, and creates the File/Batch menu where I can select the script and run it. Funny enough, it only works with D:/, if I try any other folder it doesn't run. No idea why.

So long, and see you soon I guess. :)


Top
Post new topic Reply to topic  [ 8 posts ] 

All times are UTC - 5 hours [ DST ]


   Similar Topics   Replies 
No new posts Attachment(s) missing plug in folder

4

No new posts Folder for Exposure plugin

8

No new posts [solved] Mathmap folder in Preferences

12

No new posts Permission to access folder SOLVED.

2

No new posts Watch Folder and load image as layer

1



* Login  



Powered by phpBB3 © phpBB Group