It is currently Thu Apr 25, 2024 6:40 pm


All times are UTC - 5 hours [ DST ]



Post new topic Reply to topic  [ 48 posts ]  Go to page Previous  1, 2, 3
Author Message
 Post subject: Re: How to automate saving, closing and opening of images?
PostPosted: Sat Jan 09, 2016 1:23 pm  (#41) 
Offline
Script Coder
User avatar

Joined: Jul 14, 2010
Posts: 697
OK -

Here is with SG's sort thrown in in front of each of the file-globs.

; sequential_processing.scm
; by Rob Antonishen
; http://silent9.com

; Version 1.2 (20120802)

; Description:
; Written to answer this request:
; There a directory with few tenth of a *.xcf images, and the operator opens them one by one starting
; from the first image (and doing some stuff on them between the closing and opening the images).
; The sequence I want to automate is:
; 1. save current image
; 2. close it
; 3. open next image in directory
; The question: Is there a way to automate it? If this sequence cannot be done by script, is there other ways to do it?
;
; Changes:
; v1.0 Initial release
; v1.1 Name changed, added another script to select the start file rather than just a folder
; v1.2 Option to specify file types.

; License:
; This program is free software; you can redistribute it and/or modify
; it under the terms of the GNU General Public License as published by
; the Free Software Foundation; either version 2 of the License, or
; (at your option) any later version.
;
; This program is distributed in the hope that it will be useful,
; but WITHOUT ANY WARRANTY; without even the implied warranty of
; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
; GNU General Public License for more details.
;
; The GNU Public License is available at
; http://www.gnu.org/copyleft/gpl.html

; global defines for all scripts
(define seq-file-list-ext (list "xcf" "jpg" "bmp" "png" "tiff" "orf"))
(define seq-file-list-glob (list "[xX][cC][fF]" "[jJ][pP][gG]" "[bB][mM][pP]" "[pP][nN][gG]" "[tT][iI][fF][fF]" "[oO][rR][fF]"))
(define (sort < lis)
      (define (dosort < lis n)
        (if (= n 1)
          (list (car lis))
          (let ((i (quotient n 2)))
            (domerge <
                     (dosort < lis i)
                     (dosort < (list-tail lis i) (- n i))))))
      (define (domerge < lis1 lis2)
        (cond
          ((null? lis1)
            lis2)
          ((null? lis2)
            lis1)
          ((< (car lis2) (car lis1))
            (cons (car lis2) (domerge < lis1 (cdr lis2) )))
          (else
            (cons (car lis1) (domerge < (cdr lis1) lis2)))))
     
      (if (null? lis)
        lis
        (dosort < lis (length lis))))

(define (seq-open-first-file inDir inType)
(let*
((fileexts (list-ref seq-file-list-glob inType))
(fileext (list-ref seq-file-list-ext inType))
(filelist (sort string<? (cadr (file-glob (string-append inDir DIR-SEPARATOR "*." fileexts) 1))))
(img -1)
(disp -1)
(parasite())
)
(if (= (length filelist) 0)
(gimp-message (string-append "No " fileext " files in this directory!"))
(begin
(set! img (car (gimp-file-load RUN-NONINTERACTIVE (car filelist) (car filelist))))
(set! disp (number->string (car (gimp-display-new img))))
(set! parasite (list "display-number" 1 disp))
;set parasite in image so the display can be closed later
(gimp-image-attach-parasite img parasite)
;global parasite for sequential file type
(set! parasite (list "seq-file-type" 1 (number->string inType)))
(gimp-attach-parasite parasite)
)
)
(gimp-progress-end)
)
)

(script-fu-register "seq-open-first-file"
"<Image>/File/Sequential/Open First File..."
"Open the first file in a directory setting up for repeated file processing"
"Rob Antonishen"
"Rob Antonishen"
"Aug 2012"
""

SF-DIRNAME "Load from" ""
SF-OPTION "File Types" seq-file-list-ext
)

(define (seq-open-specific-file inFile)
(let*
((fileType (let ((i 0) (k #f))
(for-each (lambda (n)
(if (string-ci=? n (car (last (strbreakup inFile ".")))) (set! k i))
(set! i (+ i 1))) seq-file-list-ext) k))
(fileexts (if fileType (list-ref seq-file-list-glob fileType)))
(fileext (if fileType (list-ref seq-file-list-ext fileType)))
(img -1)
(disp -1)
(parasite())
)
(if (not fileType)
(gimp-message (string-append "File must be one of: " (unbreakupstr seq-file-list-ext " ")))
(begin
(set! img (car (gimp-file-load RUN-NONINTERACTIVE inFile inFile)))

(set! disp (number->string (car (gimp-display-new img))))
(set! parasite (list "display-number" 1 disp))
;set parasite in image so the display can be closed later
(gimp-image-attach-parasite img parasite)
;global parasite for sequential file type
(set! parasite (list "seq-file-type" 1 (number->string fileType)))
(gimp-attach-parasite parasite)
(gimp-message (string-append "Sequential filetype set to " fileext))
)
)
(gimp-progress-end)
)
)

(script-fu-register "seq-open-specific-file"
"<Image>/File/Sequential/Open Specific File..."
"Opens a specific file in a directory setting up for repeated file processing of that filetype"
"Rob Antonishen"
"Rob Antonishen"
"Aug 2012"
""
SF-FILENAME "Starting File" ""
)


(define (seq-open-next-file inImg inLayer)
(let*
((filename (car (gimp-image-get-filename inImg)))
(filelist ())
(img -1)
(disp -1)
(parasite (catch #f (car (gimp-get-parasite "seq-file-type"))))
(fileexts "")
(fileext "")
(fileType -1)
)
(if (equal? filename "") ; check that file is saved already
(gimp-message "Not a saved file!")
(when parasite
; get filetype from parasite
(set! fileType (string->number (caddr parasite)))
(set! fileexts (list-ref seq-file-list-glob fileType))
(set! fileext (list-ref seq-file-list-ext fileType))

; check that the file matches the parasite file type
(if (not (string-ci=? fileext (car (last (strbreakup (car (last (strbreakup filename DIR-SEPARATOR))) ".")))))
(gimp-message (string-append "Not a " fileext " file!"))
(begin
; save current file
(gimp-file-save RUN-NONINTERACTIVE inImg inLayer filename (car (last (strbreakup filename DIR-SEPARATOR))))

; check for a display parasite and delete the display, erase the parasite and delete the image
(if (catch #f (gimp-image-get-parasite inImg "display-number"))
(gimp-display-delete (string->number (caddr (car (gimp-image-get-parasite inImg "display-number")))))
)

; get file list starting with this one
(set! filelist (member filename (sort string<? (cadr (file-glob (string-append (unbreakupstr (butlast (strbreakup filename DIR-SEPARATOR)) DIR-SEPARATOR) DIR-SEPARATOR "*." fileexts) 1)))))
; check for more than one file in the list
(if (= (length filelist) 1)
(begin
(gimp-message (string-append "Last " fileext " file in directory!"))
;delete filetype parasite
(gimp-detach-parasite "seq-file-type")
)
(begin
; load up new file, display.
(set! img (car (gimp-file-load RUN-NONINTERACTIVE (cadr filelist) (cadr filelist))))
(set! disp (number->string (car (gimp-display-new img))))
(set! parasite (list "display-number" 1 disp))
;set parasite in image so the display can be closed later
(gimp-image-attach-parasite img parasite)
)
)
)
)
)
)
(gimp-progress-end)
)
)

(script-fu-register "seq-open-next-file"
"<Image>/File/Sequential/Save and Close then Open Next File"
"Save and close the current file then open the next file of the matching type in that directory"
"Rob Antonishen"
"Rob Antonishen"
"Aug 2012"
"*"
SF-IMAGE "image" 0
SF-DRAWABLE "drawable" 0
)


Give that a try and see if it fixes the issue. There could be something about your installation that is changing the system file order while the script is running, as suggested.

I have NOT tested this as I don't have 2.9, so it may not even run....

-Rob A>

_________________
Image
Fantasy Cartography and Mapping by RobA


Last edited by RobA on Sun Jan 10, 2016 9:12 am, edited 1 time in total.

Top
 Post subject: Re: How to automate saving, closing and opening of images?
PostPosted: Sat Jan 09, 2016 1:43 pm  (#42) 
Offline
Script Coder
User avatar

Joined: Apr 23, 2010
Posts: 1553
Location: not from Guildford after all
I believe that the following line will not work:
(filelist (cadr (sort (file-glob (string-append inDir DIR-SEPARATOR "*." fileexts) 1))))


It should be substituted with:
(filelist (sort string<? (cadr (file-glob (string-append inDir DIR-SEPARATOR "*." fileexts) 1))))


Like RobA, i do not have a means of testing this.

_________________
Any sufficiently primitive technology is indistinguishable from a rock.


Top
 Post subject: Re: How to automate saving, closing and opening of images?
PostPosted: Sat Jan 09, 2016 4:32 pm  (#43) 
Offline
GimpChat Member

Joined: Jul 31, 2012
Posts: 22
I forgot to mention that I plan to switch back to lubuntu 14.04 since there old Rob's script works well (particulary in unstable gimp 2.9.3). Suppose there is something wrong with 15.10, there are also other odd things like mirroring and wrongly rotated previews of raw images in geeqie (other image viewers are OK). Don't know what is wrong there. It's easier for me to switch back to old distro than to find the problem.

But anyway I tried the new script. After I choose the directory and push "OK" button It shows error dialog:
Error while executing seq-open-first-file:

Error: not enough arguments

After I switch the strings as Saul suggests, the script opened first file.
When I invoke "save and open next" new error dialog appeared:
Execution error for 'Save and Close then Open Next File':
Error: not enough arguments


On lubuntu 15.10 and same Gimp 2.9.3 script showed the same behaviour.

In both cases script opened first file alphabetically.


And I do not mind to help with testing this script. No problem at all.


Top
 Post subject: Re: How to automate saving, closing and opening of images?
PostPosted: Sun Jan 10, 2016 9:15 am  (#44) 
Offline
Script Coder
User avatar

Joined: Jul 14, 2010
Posts: 697
@SG-

Thanks - There were two lines requiring your changes

@kobyakov

I have updated the inline code to correct both - can you give it one more try with both lines changed?

-Rob A>

_________________
Image
Fantasy Cartography and Mapping by RobA


Top
 Post subject: Re: How to automate saving, closing and opening of images?
PostPosted: Sun Jan 10, 2016 3:37 pm  (#45) 
Offline
GimpChat Member

Joined: Jul 31, 2012
Posts: 22
Now it works both in lubuntu 14.04 and 15.10!

Files are open alphabetically, everything nice and smooth!


Top
 Post subject: Re: How to automate saving, closing and opening of images?
PostPosted: Sun Jan 10, 2016 9:14 pm  (#46) 
Offline
Script Coder
User avatar

Joined: Jul 14, 2010
Posts: 697
Glad it works!

-Rob A>

_________________
Image
Fantasy Cartography and Mapping by RobA


Top
 Post subject: Re: How to automate saving, closing and opening of images?
PostPosted: Wed May 04, 2016 5:50 pm  (#47) 
Offline
Global Moderator
User avatar

Joined: Apr 01, 2012
Posts: 7714
Location: On the other side of this screen
Loving the sequential script. Thanks :)

_________________


Top
 Post subject: Re: How to automate saving, closing and opening of images?
PostPosted: Tue Oct 04, 2016 9:19 am  (#48) 
Offline
New Member

Joined: Oct 04, 2016
Posts: 1
I love it too! It helps me a lot. How can I save an open file without using "Save and Close then Open Next File"? Standard save option asks for a filename. So I think the script would need an option for this - very helpful - feature.


Top
Post new topic Reply to topic  [ 48 posts ]  Go to page Previous  1, 2, 3

All times are UTC - 5 hours [ DST ]


   Similar Topics   Replies 
No new posts Attachment(s) Gimp still running after an hour of closing it down

9

No new posts Attachment(s) how to automate the procedure?

14

No new posts Attachment(s) Automate merge of multiple files horizontally

3

No new posts Can ScriptFu or anything in Gimp automate the creation of layer groups

7

No new posts Attachment(s) GIMP 2.10.12 is not opening on Mac

10



* Login  



Powered by phpBB3 © phpBB Group