GIMP Script-fu

Check-in [e76675ea2d]
Login

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:Added 'filename' as a predefined variable.
Timelines: family | ancestors | descendants | both | sg-with-files
Files: files | file ages | folders
SHA1:e76675ea2d1da38c022d62c05f5a07270df892bd
User & Date: saul 2015-04-24 01:13:58
Context
2015-10-09
15:38
Missing single quote in comment example Leaf check-in: 832f8225b4 user: saul tags: sg-with-files
2015-04-24
01:13
Added 'filename' as a predefined variable. check-in: e76675ea2d user: saul tags: sg-with-files
2014-03-29
23:30
Script to simplify batch processing. check-in: 595631dbca user: saul tags: sg-with-files
Changes
Hide Diffs Unified Diffs Ignore Whitespace Patch

Changes to with-files.scm.

15
16
17
18
19
20
21
22
23

24
25
26
27
28
29
30
31
32

33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
; along with this program.  If not, see <http://www.gnu.org/licenses/>.

; Allow command line usage of GIMP such as:
; 
;     gimp -i -b '(with-files "*.png" <body>)'
; 
; where <body> is the code that handles whatever processing you want to 
; perform on the files. There are three variables that are available within
; the <body>: 'basename', 'image', and 'layer'. The 'basename' is the 

; name of the file with its extension removed, while the other two 
; variables are self-explanatory. You basically write your code as though 
; it were processing a single 'image' and the 'with-files' macro applies 
; it to all of the files matching the pattern.
; 
; For example, to invert the colors of all of the PNG files in the 
; start directory:
;    
;    gimp -i -b '(with-files "*.png" (gimp-invert layer) \

;                  (gimp-file-save 1 image layer \
;                                  (string-append basename ".png") \
;                                  (string-append basename ".png") ))'

(define-macro (with-files pattern . body)
  (let ((loop (gensym))
        (filenames (gensym))
        (filename (gensym)))
    `(begin
       (let ,loop ((,filenames (cadr (file-glob ,pattern 1))))
         (unless (null? ,filenames)
           (let* ((,filename (car ,filenames))
                  (image (catch #f (car (gimp-file-load RUN-NONINTERACTIVE
                                                        ,filename 
                                                        ,filename ))))
                  (layer (if image (car (gimp-image-get-active-layer image)) #f))
                  (basename (catch ,filename 
				   (unbreakupstr (butlast (strbreakup ,filename ".")) "."))))
             (when image
               ,@body
               (gimp-image-delete image)))
           (,loop (cdr ,filenames))))
       (gimp-quit 0))))







|
|
>








|
>
|
<
<



|
<



|

|
|

|
|





15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35


36
37
38
39

40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
; along with this program.  If not, see <http://www.gnu.org/licenses/>.

; Allow command line usage of GIMP such as:
; 
;     gimp -i -b '(with-files "*.png" <body>)'
; 
; where <body> is the code that handles whatever processing you want to 
; perform on the files. There are four variables that are available within
; the <body>: 'filename', 'basename', 'image', and 'layer'. The 'filename'
; is the full path of the original file. The 'basename' is the 
; name of the file with its extension removed, while the other two 
; variables are self-explanatory. You basically write your code as though 
; it were processing a single 'image' and the 'with-files' macro applies 
; it to all of the files matching the pattern.
; 
; For example, to invert the colors of all of the PNG files in the 
; start directory:
;    
;    gimp -i -b '(with-files "*.png" \
;                  (gimp-invert layer) \
;                  (gimp-file-save 1 image layer filename filename))



(define-macro (with-files pattern . body)
  (let ((loop (gensym))
        (filenames (gensym)))

    `(begin
       (let ,loop ((,filenames (cadr (file-glob ,pattern 1))))
         (unless (null? ,filenames)
           (let* ((filename (car ,filenames))
                  (image (catch #f (car (gimp-file-load RUN-NONINTERACTIVE
                                                        filename 
                                                        filename ))))
                  (layer (if image (car (gimp-image-get-active-layer image)) #f))
                  (basename (catch filename 
				   (unbreakupstr (butlast (strbreakup filename ".")) "."))))
             (when image
               ,@body
               (gimp-image-delete image)))
           (,loop (cdr ,filenames))))
       (gimp-quit 0))))