FrancoisMalan / DivideScannedImages

GIMP Scheme (.scm) script for splitting separate sub-images from a composite image that has a uniform background. Creates a new image from each; will call the 'deskew' plugin on each image (if it is installed)
111 stars 22 forks source link

Use InFileName as part of the NewFileName #2

Open SpudmanWP opened 9 years ago

SpudmanWP commented 9 years ago

Currently the saved images are named incrementally, regardless of the source file name.

I would like an option to have the original file name as part of the NewFileName.

For example, for an original file name of "Smith, John A.jpg" that had 3 images would result in: Smith, John A_IMAGE000001.jpg Smith, John A_IMAGE000002.jpg Smith, John A_IMAGE000003.jpg

Other than that, the script rocks, thanks.

BenutzenHusam82 commented 7 years ago

Latest from a thread I posted on reddit:

https://www.reddit.com/r/GIMP/comments/5rz877/batch_divide_scanned_images_with_names/

in the code of the script around line 209 it sets the new name

(set! newFileName (string-append targetDir pathchar inFileName (substring "00000" (string-length (number->string (+ inFileNumber numextracted)))) (number->string (+ inFileNumber numextracted)) saveString))

in line 59 I think has the original filename gimp-image-get-filename

(set! imgpath (car (gimp-image-get-filename img)))

maybe you can try adding that string at the from of setting of new filename

set! newFileName (string-append targetDir pathchar gimp-image-get-filename inFileName (substring "00000" (string-length (number->string (+ inFileNumber numextracted)))) (number->string (+ inFileNumber numextracted)) saveString))

DarrellJonsson commented 6 years ago

I found gimp-image-get-filename per bablakely's suggestion in linux troublesome, as it did not return a clean enough base root file name to be practical. I found the full path was included in gimp-image-get-filename. The '\' slashes were included in the file "base" name and not the path. Saving files with names like /media/john/scans/Crop_media/john/scans/00001.jpg was as messy as it was impossible.

In order to make an elegant human readable file name it was necessary to create another field called imagepathbase and use 2 functions found online as as follows.

(set! imgpathbase (filename-basename imgpath))           ; get base file name and path

; [X] Save output to source directory in which case the inDir variable holds path to file.
(set! imgpathbase (string-replace imgpathbase inDir "")) ; remove path from file name

; Do no save output to source directory in which case the targetDir variable holds path to file.
(set! targetDir (unbreakupstr (butlast (strbreakup imgpath pathchar)) pathchar)) ; remove targetDir 
(set! imgpathbase (string-replace imgpathbase targetDir "")) ; remove path from file name

(set! imgpathbase (string-replace imgpathbase "/" ""))   ; remove / from file name
(set! imgpathbase (string-append imgpathbase "_"))       ; add underscore separator

After cleaning up imgpathbase then it could be used later when assigning newFilename later in the code.

See these links for the code for the scm functions added. https://stackoverflow.com/questions/1386293/how-to-parse-out-base-file-name-using-script-fu https://stackoverflow.com/questions/11509500/string-replace-in-gimp-script-fu