Gimp-Forum.net
gimp-image-get-filename - 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: gimp-image-get-filename (/Thread-gimp-image-get-filename)

Pages: 1 2


gimp-image-get-filename - PixLab - 10-07-2024

I got errors when trying to set the original name, what did I missed?
It seems to be from the second "if" (first one works fine) "pathchar (car (gimp-image-get-filename image)) ".webp"))"

BTW is there a "else" or sort of in scheme?

Code:
   (while (> i 0)

     (set! image (vector-ref (cadr (gimp-image-list)) (- i 1)))
     
     (if (= KeepName 0)
         (set! newFileName (string-append inDir
                 pathchar inFileName
                 (substring "0000000" (string-length
                 (number->string (+ inFileNumber i))))
                 (number->string (+ inFileNumber (- ii i))) ".webp"))
       )
     (if (= KeepName 1)
         (set! newFileName (string-append inDir
                 pathchar (car (gimp-image-get-filename image)) ".webp"))
     )
   
         (file-webp-save2 isInteractive
                     image
                     (car (gimp-layer-new-from-visible image image "export"))
                     newFileName
                     newFileName

[attachment=12429]


RE: gimp-image-get-filename - Ofnuts - 10-07-2024

file-webp-save2 takes 19 arguments and you are only showing 2.


RE: gimp-image-get-filename - PixLab - 10-08-2024

(10-07-2024, 11:01 PM)Ofnuts Wrote: file-webp-save2 takes 19 arguments and you are only showing 2.

They are all there, see below last code section, i did put the full code

The thing is if I use only
(while (> i 0)

     (set! image (vector-ref (cadr (gimp-image-list)) (- i 1)))
     
     (if (= KeepName 0)
         (set! newFileName (string-append inDir
                 pathchar inFileName
                 (substring "0000000" (string-length
                 (number->string (+ inFileNumber i))))
                 (number->string (+ inFileNumber (- ii i))) ".webp"))
       )

This part works as intended, (if (= KeepName 0) ➤ it's for if  SF-TOGGLE  "Keep the original name" FALSE

but if I add the below code, that part below throw an error when the SF-Toggle is checked >  SF-TOGGLE  "Keep the original name" TRUE

(if (= KeepName 1)
        (set! newFileName (string-append inDir
                pathchar (car (gimp-image-get-filename image)) ".webp"))
    )


i would like to concatenate the path + original image name + extension (here webp), It is to propose if user wants to keep the original image name or not,

I also read there > https://developer.gimp.org/api/2.0/libgimp/libgimp-gimpimage.html#gimp-image-get-filename
that "gimp-image-get-filename" returns The filename. The returned value must be freed with g_free().
What does it mean "must be freed with g_free()"? what is g_free()?

The full script

Code:
(define (script-fu-pxl-save-all-images-as-webp inDir inAskAfter inKeepName inFileName inFileNumber inPreset inLossLess inQuality inAlphaQuality inAnimation inLoop inMiniSize inKeyframe inExif inIPTC inXMP inThumbnail inDelay inForceDelay runChoice)
 (let* (
         (i (car (gimp-image-list)))
         (ii (car (gimp-image-list)))
         (image)
         (isInteractive)
         (KeepName)        
         (newFileName "")
         (pathchar (if (equal?
                (substring gimp-dir 0 1) "/") "/" "\\"))
         (webPreset)  
         (isLossless)    
         (qualityImage)
         (qualityAlpha)
         (isAnimation)
         (isLoop)
         (isMinimize)
         (keyframeNum)
         (isExif)
         (isIPTC)
         (isXMP)
         (isThumb)
         (delayNum)
         (isForceDelay)
       )
   (set! KeepName
      (cond
      ((equal? inKeepName FALSE) 0 )
      ((equal? inKeepName TRUE) 1 )
      )
   )
   (set! isInteractive
     (cond
       (( equal? runChoice 0 ) RUN-NONINTERACTIVE )
       (( equal? runChoice 1 ) RUN-INTERACTIVE )
       (( equal? runChoice 2 ) RUN-WITH-LAST-VALS )
     )
   )
   (set! webPreset                      ; re-order as I did "Picture" first
       (cond
       ((equal? inPreset 0) 1 )
       ((equal? inPreset 1) 2 )
       ((equal? inPreset 2) 3 )
       ((equal? inPreset 3) 4 )
       ((equal? inPreset 4) 5 )
       ((equal? inPreset 5) 0 )
       )
   )
   (set! isLossless
      (cond
      ((equal? inLossLess FALSE) 0 )
      ((equal? inLossLess TRUE) 1 )
      )
   )
   (set! qualityImage inQuality )
   (set! qualityAlpha inAlphaQuality )
   (set! isAnimation
      (cond
      ((equal? inAnimation FALSE) 0 )
      ((equal? inAnimation TRUE) 1 )
      )
   )
   (set! isLoop
      (cond
      ((equal? inLoop FALSE) 0 )
      ((equal? inLoop TRUE) 1 )
      )
   )
   (set! isMinimize
      (cond
      ((equal? inMiniSize FALSE) 0 )
      ((equal? inMiniSize TRUE) 1 )
      )
   )
   (set! keyframeNum inKeyframe)    
   (set! isExif
      (cond
      ((equal? inExif FALSE) 0 )
      ((equal? inExif TRUE) 1 )
      )
   )
   (set! isIPTC
      (cond
      ((equal? inIPTC FALSE) 0 )
      ((equal? inIPTC TRUE) 1 )
      )
   )
   (set! isXMP
     (cond
      ((equal? inXMP FALSE) 0 )
      ((equal? inXMP TRUE) 1 )
     )
   )
   (set! isThumb
     (cond
      ((equal? inThumbnail FALSE) 0 )
      ((equal? inThumbnail TRUE) 1 )
     )
   )
   (set! delayNum inDelay)
   (set! isForceDelay
     (cond
      ((equal? inForceDelay FALSE) 0 )
      ((equal? inForceDelay TRUE) 1 )
     )
   )

   (while (> i 0)

     (set! image (vector-ref (cadr (gimp-image-list)) (- i 1)))
     
     (if (= KeepName 0)
         (set! newFileName (string-append inDir
                 pathchar inFileName
                 (substring "0000000" (string-length
                 (number->string (+ inFileNumber i))))
                 (number->string (+ inFileNumber (- ii i))) ".webp"))
       )
     (if (= KeepName 1)
         (set! newFileName (string-append inDir
                 pathchar (car (gimp-image-get-filename image)) ".webp"))
     )
   
         (file-webp-save2 isInteractive
                     image
                     (car (gimp-layer-new-from-visible image image "export"))
                     newFileName
                     newFileName
                     webPreset        ; preset 0=default 1=pic 2=photo 3=drawing 4=icon 5=text
                     isLossless       ; Use lossless encoding (0/1)
                     qualityImage     ; Quality of the image (0 <= quality <= 100)
                     qualityAlpha     ; alpha-quality  0<>100
                     isAnimation      ; Use layers for animation (0/1)
                     isLoop           ; Loop animation infinitely (0/1)
                     isMinimize       ; Minimize animation size (0/1)
                     keyframeNum      ; Maximum distance between key-frames (>=0)
                     isExif           ; Toggle saving exif data (0/1)
                     isIPTC           ; Toggle saving iptc data (0/1) works only if save XMP data is also checked ?
                     isXMP            ; Toggle saving xmp data (0/1)
                     isThumb          ; Toggle saving thumbnail (0/1)
                     delayNum         ; Delay to use when timestamps are not available or forced
                     isForceDelay     ; Force delay on all frames (0/1)

         )

     (if (not (= inAskAfter TRUE)) (gimp-image-clean-all image))
     (set! i (- i 1))


)))

(script-fu-register "script-fu-pxl-save-all-images-as-webp" ; script-fu-register-filter for gimp 2.99
"1 - As WebP..."
"Export all opened images at once as WebP with your settings..."
"PixLab"
"GPL-v2+"
"2023/12/12"
"*"
SF-DIRNAME    "Select a directory to export your images" "Desktop"
SF-TOGGLE     "Ask me to save the XCF when closing GIMP or an image" TRUE
SF-TOGGLE     "Keep the original name" TRUE
SF-STRING     "Input an image base name" "IMAGE-"
SF-ADJUSTMENT "Input a start number for image name auto-numbering" (list 1 0 999999 1 100 0 SF-SPINNER)
SF-OPTION     "Select a WebP Preset" (list "Picture" "Photo" "Drawing" "Icon" "Text" "Default")
SF-TOGGLE     "Use lossless encoding" FALSE
SF-ADJUSTMENT "Quality of the image" (list 90 0 100 1 1 0 SF-SLIDER)
SF-ADJUSTMENT "Quality of Transparency" (list 90 0 100 1 1 0 SF-SLIDER)
SF-TOGGLE     "Use Layers for Animation" FALSE
SF-TOGGLE     "Loop Animation Infinitely" TRUE
SF-TOGGLE     "Minimize Animation Size" FALSE
SF-ADJUSTMENT "Maximum distance between key-frames:\n    0 = No Keyframes:\n    1 = All Frames are Keyframes" (list 50 0 10000 1 1 0 SF-SPINNER)
SF-TOGGLE     "Save Exif Data" TRUE
SF-TOGGLE     "Save IPTC Data" FALSE
SF-TOGGLE     "Save XMP Data" FALSE
SF-TOGGLE     "Save Thumbnail" FALSE
SF-ADJUSTMENT "Delay to use between frames when unspecified" (list 100 0 10000 1 1 0 SF-SPINNER)
SF-TOGGLE     "Use delay entered above for all frames" FALSE
SF-OPTION     "Select How to Export (Explanations in the manual)" (list "Automated: Above settings for all images" "Full control: You confirm each setting on each image" "Use settings from latest Export")
)
(script-fu-menu-register "script-fu-pxl-save-all-images-as-webp" "<Image>/File/E_xport/Export All Images As")



RE: gimp-image-get-filename - teapot - 10-08-2024

Hi PixLab,
 
I gave it a try on linux. Ran your script on image /home/teapot/test.xcf with export directory /home/teapot/Desktop/

With 'Keep the original name' un-ticked got newFileName /home/teapot/Desktop/IMAGE-0000001.webp which looks OK.

With 'Keep the original name' ticked got newFileName /home/teapot/Desktop//home/teapot/test.xcf.webp
This is not OK, the path is in 'twice' and there's a .xcf.

gimp-image-get-filename gives the whole path so /home/teapot/test.xcf
Whereas presumably what you want is gimp-image-get-name which will give test.xcf
Then you would need to strip off the .xcf

Also you should consider the case when the user has a brand new .xcf file that's never been saved.

Any reason why you aren't using python?


RE: gimp-image-get-filename - Ofnuts - 10-08-2024

(10-08-2024, 04:00 AM)PixLab Wrote: I also read there > https://developer.gimp.org/api/2.0/libgimp/libgimp-gimpimage.html#gimp-image-get-filename
that "gimp-image-get-filename" returns The filename. The returned value must be freed with g_free().
What does it mean "must be freed with g_free()"? what is g_free()?

You are looking at the doc for the C API. For script-fu, stick to the doc in the script-fu console, much less confusing (the C doc also contains a lot of calls that aren't available in script-fu.


RE: gimp-image-get-filename - PixLab - 10-09-2024

Hi teapot, thanks for trying it out  Smile

(10-08-2024, 05:39 AM)teapot Wrote: With 'Keep the original name' ticked got newFileName /home/teapot/Desktop//home/teapot/test.xcf.webp
This is not OK, the path is in 'twice' and there's a .xcf.

You did not get an error? I mean, the script as it is throw an error if "Keep the original name" is ticked, at least on my distro (Ubuntu-MATE 20.04 /GIMP 2.10.38)
I found a partial "solution" but ... see below the code

(10-08-2024, 05:39 AM)teapot Wrote: gimp-image-get-filename gives the whole path so /home/teapot/test.xcf
Whereas presumably what you want is gimp-image-get-name which will give test.xcf
Then you would need to strip off the .xcf

Thanks for the  "gimp-image-get-name" it worked,
I did find a partial solution with "strbreakup" to remove any file extension and add the extension I want, see code below.

Code:
(if (= isKeepName 1)
        (set! newFileName (string-append inDir
                pathchar (car (strbreakup (car (gimp-image-get-name image)) ".")) ".webp"))
       )

but... (yes there is a "but"  Big Grin )  "gimp-image-get-name" take the temporary name in GIMP when we drag and drop images in GIMP
For instance if I drag and drop an image with the name "MyImage.jpg", in GIMP it will be called "[Myimage] (Imported)" and it is this name that GIMP will export with "gimp-image-get-name" ➤ [Myimage] (Imported).jpg

Drag and drop MyImage.jpg in GIMP look at the name

[attachment=12432]

Result using "gimp-image-get-name", it export the "temporary?" name in GIMP

[attachment=12433]

So, I'm telling myself that I need to use "gimp-image-get-filename", and get the name before the extension in the "list?".

There is another problem, on *nix system we can have names with multiple dot/period (and certainly on Windows nowadays), if I use (car (strbreakup (car (gimp-image-get-name image) ".") on an image named "this.is.my.image.xcf", I will only get "this" in the name...

[attachment=12434]

The problem with car, cdr, cddr, cadar, and so on.. first is, that I have a very hard time to get it  Sad ,
second I did not find any explanations to fetch the last in the list or the one before the last, they (websites) are all speaking about fetching/catching the first in the list and the remaining list after the first.

If I can find out how to get only the name with this code just by using the right combination of "car" / "cdr" / "caaddr" / "whatnot", to get the one before the last (the extension) in the "list" that would be a huge step for me  Rolleyes
-------------------------
(if (= isKeepName 1)
        (set! newFileName (string-append inDir
                pathchar (car (strbreakup (car (gimp-image-get-filename image)) ".")) ".webp"))
       )
-----------------------

(10-08-2024, 05:39 AM)teapot Wrote: Also you should consider the case when the user has a brand new .xcf file that's never been saved.

Indeed, you're right, but I'm a slow person, one thing at a time  Big Grin

(10-08-2024, 05:39 AM)teapot Wrote: Any reason why you aren't using python?

The 1 Trillion dollar question   Blush

All started with with a simple saule goode script which was saving the active layer, it was an scm.
then rich2005 opened my eyes ➤ https://www.gimp-forum.net/Thread-Export-all-opened-images-in-GIMP-at-once
Then add more option ➤ https://www.gimp-forum.net/Thread-Batch-export-all-opened-images-at-once-V2-more-options
Then change the way the script works + a suggestion to reverse order by Krikor ➤ https://www.gimp-forum.net/Thread-Export-ALL-opened-images-in-GIMP-at-once-with-your-settings
Then a suggestion to add an option from Michael Schumacher on reddit ➤ https://www.gimp-forum.net/Thread-Batch-export-all-opened-images-in-GIMP-at-once-V3-22343567
Then few days ago I "updated" on my gitlab to make it compatible for GIMP 3 (I did not test yet)
Then I recall that many people asked to be able to save with the original name...
thus I get caught in that spiral down of the scheme scripting  Big Grin

Today I just want to finish this part, and "never" come back to it again if possible  Wink

(10-08-2024, 07:22 AM)Ofnuts Wrote: You are looking at the doc for the C API. For script-fu, stick to the doc in the script-fu console, much less confusing (the C doc also contains a lot of calls that aren't available in script-fu.

Looks like I get lost during my searches  Big Grin
Thank you Ofnuts.


RE: gimp-image-get-filename - rich2005 - 10-09-2024

(10-09-2024, 05:14 AM)PixLab Wrote: ....snip.....
Then few days ago I "updated" on my gitlab to make it compatible for GIMP 3 (I did not test yet)
Then I recall that many people asked to be able to save with the original name...
thus I get caught in that spiral down of the scheme scripting  

Maybe script-fu is the simpler alternative for users.  There are (were) Gimp 3 examples around:
https://discourse.gnome.org/t/gimp-3-plugins-and-script-fu-examples/15114  and the link to the scripts worked a couple of weeks ago, looks like it has been pulled, probably changes to syntax, who-knows.

I did download a couple, and one that works (at least it does here with 2.99.19 appimage) is attached. This is a script-fu plugin. Goes in the plugins folder in its own folder, has to be executable. Backs up at intervals. and saves a .xcf in a ~/Autosaved folder. Registers bottom of the File menu.

[attachment=12436]

There are lines in the script for vector and for making a file name.ext so it might help.

The python examples I have seen look horribly complicated to me, but then I am only a dabbler. A proper "macro" would be nice, but that is only mentioned for the dim-and-distant-future. In Gimp glacial development terms, I do not think I will be around to see that Wink


RE: gimp-image-get-filename - PixLab - 10-09-2024

(10-09-2024, 08:37 AM)rich2005 Wrote:
(10-09-2024, 05:14 AM)PixLab Wrote: ....snip.....
Then few days ago I "updated" on my gitlab to make it compatible for GIMP 3 (I did not test yet)
Then I recall that many people asked to be able to save with the original name...
thus I get caught in that spiral down of the scheme scripting  

Maybe script-fu is the simpler alternative for users.  There are (were) Gimp 3 examples around:
https://discourse.gnome.org/t/gimp-3-plugins-and-script-fu-examples/15114  and the link to the scripts worked a couple of weeks ago, looks like it has been pulled, probably changes to syntax, who-knows.

I did download a couple, and one that works (at least it does here with 2.99.19 appimage) is attached. This is a script-fu plugin. Goes in the plugins folder in its own folder, has to be executable. Backs up at intervals. and saves a .xcf in a ~/Autosaved folder. Registers bottom of the File menu.

There are lines in the script for vector and for making a file name.ext so it might help.

The python examples I have seen look horribly complicated to me, but then I am only a dabbler. A proper "macro" would be nice, but that is only mentioned for the dim-and-distant-future. In Gimp glacial development terms, I do not think I will be around to see that Wink

Oh... indeed it helps a lot for GIMP 3, I already saw few things that I need to update on my "updated GIMP 3" version ASAP Big Grin
Also I was not aware we need to put in a folder and give permissions
Thank you very much rich2005 for that invaluable info and a working GIMP 3 script to study Smile


RE: gimp-image-get-filename - teapot - 10-09-2024

(10-08-2024, 04:00 AM)PixLab Wrote: I did not find any explanations to fetch the last in the list or the one before the last, they (websites) are all speaking about fetching/catching the first in the list and the remaining list after the first.

Well you could reverse the list. Or getting the last element of the list without reversing it:

Code:
(let* ((l '(3 45 4 "abc" "def"))
       (n (length l)))
      (list-ref l (- n 1)))

There's some ideas here but they aren't very robust:
https://stackoverflow.com/questions/1386293/how-to-parse-out-base-file-name-using-script-fu

Personally I'd be doing this in Python using os:

Code:
$ python2
➤> import os
➤> help(os.path.basename)
Help on function basename in module posixpath:

basename(p)
    Returns the final component of a pathname

➤> help(os.path.splitext)
Help on function splitext in module posixpath:

splitext(p)
    Split the extension from a pathname.

    Extension is everything from the last dot to the end, ignoring leading dots.
    Returns "(root, ext)"; ext may be empty.



RE: gimp-image-get-filename - PixLab - 10-10-2024

(10-09-2024, 04:28 PM)teapot Wrote: Well you could reverse the list. Or getting the last element of the list without reversing it:

Code:
(let* ((l '(3 45 4 "abc" "def"))
       (n (length l)))
      (list-ref l (- n 1)))

Thanks, ok I understood how it works in the Script-fu console, not sure how to adapt it to the gimp-image-get-filename, nor understand what's the ' in front of the parenthesis

(10-09-2024, 04:28 PM)teapot Wrote: There's some ideas here but they aren't very robust:
https://stackoverflow.com/questions/1386293/how-to-parse-out-base-file-name-using-script-fu

not a lot about script-fu on internet, or things are from 20 years ago ➤ we all go by the same different results in google Big Grin
I was not able to make one of them works, but it's my extremely limited knowledge (if I can call "knowledge") about it.

In all case thank you very much teapot Smile