Sorry for any errors, english is not my native language
I'm in need of a certain script to manipulate a bunch of images, to not bother people to death by asking "can someone do this for me?", i'm trying to learn how to write them myself.
I start by creating a script that will move images from one folder to another, from /Images/origin to /Images/destination, I need this to be a relative path since i need it to work anywhere as long as theres a origin and destination folder inside a Images folder.
Currently I have the next script (taken from what i could learn, copy and understand) but when saving an image it doesn't save where i expect it to go, actualy it does nothing.
(define (batch-move)
(let* (filelist (cadr (file-glob "/Image/origin/*.png" 1))))
(while (not (null? filelist))
(let* ((filename (car filelist))
(image (car (gimp-file-load RUN-NONINTERACTIOVE filename filename)))
(draw (car (gimp-image-get-active-layer image)))
(gimp-file-save RUN-NONINTERACTIVE image draw filename/../destination filename)
(set! filelist (cdr filelist))
))))
The place where is the function(?) gimp-file-save, i've tried to put the path before, and in this example after, but it doesn't work.
This is assuming the filename is the complete path using /../ to go back a folder and /destination/ go to the next, same way as to navigate windows using relative path directories
I also understand that the gimp-image-get-active-layer is probably not necessary but this is for me to later add all the functions that i need.
And my question is why is it not working?, from what I gather the filename variable is the entire path, but if it isn't then what is is?
Related
I would like to add a drop shadow to picture files without using the Gimp UI. I've saved this content to ~~/.config/GIMP/2.10/scripts/my.scm~:
(define (my/add-drop-shadow filename)
(let* ((image (car (gimp-file-load RUN-NONINTERACTIVE filename filename)))
(drawable (car (gimp-image-get-active-layer image))))
;; Apply transformations
(script-fu-drop-shadow RUN-NONINTERACTIVE image drawable
20.0 20.0 10.0 0 0.5 nil)))
(I know this code is not saving the image, I will add that when this works).
Unfortunately, when trying to use that, I get an error:
$ gimp --version
2.10.30
$ gimp -i -b '(my/add-drop-shadow "2022-11-01-080429.png")' -b '(gimp-quit 0)'
GIMP-Error: Calling error for procedure 'gimp-image-set-active-layer':
Procedure 'gimp-image-set-active-layer' has been called with an invalid ID for argument 'active-layer'. Most likely a plug-in is trying to work on a layer that doesn't exist any longer.
batch command experienced an execution error:
Error: Procedure execution of gimp-image-set-active-layer failed on invalid input arguments: Procedure 'gimp-image-set-active-layer' has been called with an invalid ID for argument 'active-layer'. Most likely a plug-in is trying to work on a layer that doesn't exist any longer.
/home/cassou/.nix-profile/bin/gimp: GEGL-WARNING: (../gegl/buffer/gegl-tile-handler-cache.c:1076):gegl_tile_cache_destroy: runtime check failed: (g_queue_is_empty (&cache_queue))
EEEEeEeek! 2 GeglBuffers leaked
Am I doing something incorrect or is the plug-in buggy?
Update
If I add these statements to the script at the start of the let body:
(print "=============================")
(print image)
(print drawable)
(print "=============================")
I get:
"============================="
1
2
"============================="
as output.
No a script-fu expert but at least 3 problems in your call:
The "color" argument isn't just an integer (at least in a full-RGB image), most likely a triplet, or the result of (gimp-context-get-foreground)
The opacity is a 0-100 number, with .5 you won't see much, you probably want 50 instead
The resizing argument should be 0 or 1
Calling from python this works:
pdb.script_fu_drop_shadow(image, image.active_layer,20.0, 20.0, 10.0, (0,0,0), 80, 0)
I am struggling with getting the actual path (or vector) object from an id. I want to stroke a path and the currently advised way of doing so seems to be the method gimp-drawable-edit-stroke-item. This needs an item as input. By the way I tried to find a list of all predefined types in script-fu but also didn't find anything. So I am not sure what the typ Item really is but it looks like you can pass a vector to it.
All I can find so far to identify a path is using (cadr(gimp-image-get-vectors p-image)) which seems to only give me an id. As the following (gimp-drawable-edit-stroke-item p-drawable (cadr(gimp-image-get-vectors p-image))) leads to an "Error: Invalid type for argument 2 to gimp-drawable-edit-stroke-item".
Having to navigate lists instead of using names for fields is the reason why I never bothered with Scheme/script-fu since Gimp can be scripted in Python.
This said, with my limited Lisp knowledge:
(gimp-image-get-vectors p-image) returns a (count (v1 v2 v3 ...)) list
so (cadr (gimp-image-get-vectors p-image)) returns the list, and not a single item of the list.
You can get the "active path" directly with (gimp-image-get-vectors p-image) (using the paths list doesn't tell you which path in the list is meant by the user anyway).
"I want to stroke a path"
Rather than gimp-drawable-edit-stroke-item, gimp-pencil (or gimp-brush) can do it:
(define (stroke-path drawable color width . path)
(gimp-context-set-line-miter-limit 5) ; default: 10, default mitre up to 60 pixels
(gimp-context-set-stroke-method STROKE-LINE) ; default STROKE-PAINT-METHOD
(gimp-context-set-line-cap-style CAP-BUTT) ; CAP-ROUND, CAP-SQUARE
(gimp-context-set-line-join-style JOIN-ROUND) ; JOIN-MITER, JOIN-ROUND, JOIN-BEVEL
(gimp-context-set-foreground color)
(gimp-context-set-line-width width) ; default: 6
(let ((vec (apply vector path)))
(gimp-pencil drawable (vector-length vec) vec)
)
)
I'm trying to set up a script for raw photos, to allow me to add two copies of the existing file (all taken from ufraw), at different exposures so I can recover shadow and highlight detail. The part for the shadow file is now working, hard-coded to use the same layer for its mask and with a mode of NORMAL.
But I want to pass an option for the shadow mask, so that I can take it from the shadow layer or the base layer (the normal exposure) AND set the mode to NORMAL or LIGHTEN-ONLY. The following code does not error:
(cond ( < shadow-option 2 ) (define (sm-source shadow-layer))
(else (define (sm-source base-layer))))
(cond ( = shadow-option 0 ) (define (shadow-mode NORMAL))
(else (define (shadow-mode LIGHTEN-ONLY))))
Maybe my cond tests are not going to do what I want, but AFAICS both sm-source and shadow-mode must have a value, either from the test being true, or from the else ?
But both appear to not be defined when I try to reference them. The first use is on the next line of my script:
(gimp-layer-set-mode shadow-layer shadow-mode)
But I get Error: ( : 1) eval: unbound variable: shadow-mode. If I comment that line and replace it by a hardcoded mode, I then get a similar error for sm-source.
I have images which have saved in the desk. The data saved as follow: 4 main folders (1,2,3 and 4) each folder has 26 subfolders ( these subfolders represent the class of images (A, B, C, D, ..,Z)). Each of these subfolder contains more than 500 images. However, I am looking for file or code in torch that can read these images. In MATLAB I could wrote a code but here I find it confuse. Could you please advise me.
What you can do is use Penlight (the library is installed when you install Torch).
Penlight provides pl.dir that makes it easy to scan files in (sub-)folders. For example what you can do is:
local pl = require('pl.import_into')()
local t = {}
for i,f in ipairs(pl.dir.getallfiles('/data/foo', '*.jpg')) do
t[i] = { f, pl.path.basename(pl.path.dirname(f)) }
end
This creates a list of pairs (filename, class label = "A" or "B" ...). Of course you are free to change the file pattern (*.jpg) or to omit it (in such a case Penlight will simply list all files). You can also load the images on the fly:
t[i] = { image.load(f), pl.path.basename(pl.path.dirname(f)) }
Or do that right after when manipulating t.
So i tried to run the batch code from this tutorial: http://www.gimp.org/tutorials/Basic_Batch/
I copied and saved this to the .gimp-2.8 scripts folder
(define (batch-unsharp-mask pattern
radius
amount
threshold)
(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))))
(plug-in-unsharp-mask RUN-NONINTERACTIVE
image drawable radius amount threshold)
(gimp-file-save RUN-NONINTERACTIVE
image drawable filename filename)
(gimp-image-delete image))
(set! filelist (cdr filelist)))))
And ran the command:
gimp-2.8 -i -b '(batch-unsharp-mask "*.png" 5.0 0.5 0)' -b '(gimp-quit 0)'
But i get the error
Error: ( : 1) eval: unbound variable: *.png
This doesn't seem to be a problem with the script; probably the way i am calling it or i am missing something but i cant figure it out.
I know it's been four long months, but I was just fighting with a gimp (2.8) script I had written and getting a recurring
batch command experienced an execution error:
Error: ( : 32578) eval: unbound variable: while
It turns out that, in my haste to consolidate script directories, I had removed the reference to /usr/share/gimp/2.0/scripts. Apparently there's something in that directory that defines "while"
Edit->Preferences->Folders->Scripts
Got the original script working in gimp 2.8 ubuntu 14.04lts Just had to copy and paste the line breaks back together. I am having trouble pasting this in so the lines are not broken, just remove the blank lines as they are a posting anomaly. Place script in ~/.gimp-2.9/scripts
Using this to remove haze
run as gimp -i -b '(batch-unsharp-mask "*.JPG" 100 0.3 0)' -b '(gimp-quit 0)'
(define (batch-unsharp-mask pattern radius amount threshold)
(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))))
(plug-in-unsharp-mask RUN-NONINTERACTIVE image drawable radius amount threshold)
(gimp-file-save RUN-NONINTERACTIVE image drawable filename filename)
(gimp-image-delete image))
(set! filelist (cdr filelist)))))
I resolved this error by confirming the name of my script. I copied my script from an existing script and the name was duplicate. After I updated the name of my script the error went away and everything started working.
You must have unique script names, that includes the define name
What helped me was making sure that the script had the correct extension, .scm.