GimpTalk Announcement :
Try your digital-art skills against your fellow gimpers in GIMPtalk's Biweekly Art competition !


Board index » GIMP Art Galleries and Resources » GIMP Plugins, Filters, and Scripts Web Development Services . Convert FLV to Cell Phone Format
Featured Tutorial : Learn how to create characters by Griatch
Search for :  


Post new topic Reply to topic   [ 11 posts ]  


Author Message
Offline
 Post subject: Applying a Filter to all images in a directory
PostPosted: Tue Sep 16, 2008 11:43 am 
User avatar

Joined:
Tue Sep 16, 2008 11:29 am

Topics:
Posts: 6
Location: gmt+1

Find User's Topics

Hi everybody!

I'd like to apply a filter to all images in a directory. I'm using the GIMP 2.2 on Debian Linux and the Filter I'm talking about is the Normalmap plugin available here. I've already installed the plugin and it is available in GIMP under Filters --> Map --> Normalmap... and works.

The plugin creates a new image (the normalmap) when applied to an existing file. I'd like to apply it to many files (> 500) and I'll have to do that again from time to time, so I have to automate the process. What I want is
  • to apply the Filter to all images in a directory
  • to make GIMP write the resulting normalmap to image_nm.ext if the original image is called image.ext.

Any hints on how to achieve this?

Thanks in advance,

spirit


_________________
spirit


Top
 Profile E-mail  
 
Online
 Post subject: Re: Applying a Filter to all images in a directory
PostPosted: Wed Sep 17, 2008 6:29 am 
User avatar

Joined:
Mon Aug 22, 2005 8:35 pm

Topics:
Posts: 5087

Find User's Topics
Are you using the same alphamap for every image?

_________________
Everybody makes their own fun. If you don't make it yourself it's not fun, it's entertainment.


Top
 Profile  
 
Offline
 Post subject: Re: Applying a Filter to all images in a directory
PostPosted: Wed Sep 17, 2008 10:09 am 
User avatar

Joined:
Tue Sep 16, 2008 11:29 am

Topics:
Posts: 6
Location: gmt+1

Find User's Topics
I don't have an alphamap, I just have the texture. Maybe the plugin tries to generate the alphamap from the image or whatever, don't know. All I do is open the image and click Filters --> Map --> Normalmap.... :)

_________________
spirit


Top
 Profile E-mail  
 
Online
 Post subject: Re: Applying a Filter to all images in a directory
PostPosted: Wed Sep 17, 2008 3:46 pm 
User avatar

Joined:
Mon Aug 22, 2005 8:35 pm

Topics:
Posts: 5087

Find User's Topics
The following function should be callable from a command shell but I don't think the normalmap plugin is producing anything useful. Perhaps you can figure out what is going wrong and modify the parameters to work (I used the default values from the plugin except for the 'alphamapid', owing to a bug in the plugin).

Save the script as a .scm file in your ~/username/.gimp-2.4/scripts directory, and then run the following command from a shell:

gimp -i -b '(save-normal-map "*")' -b '(gimp-quit 0)'

The "*" wildcard can be replaced with any filename pattern; for example, "/home/username/images/*.png".

Code:
;; This code is licensed for any imaginable purpose

(define (save-normal-map file-pattern)
  (define (save-layer orig-image layer name)
    (let* (
        (image)
        (buffer)
        )
      (set! buffer (car (gimp-edit-named-copy layer "temp-copy")))
      (set! image (car (gimp-edit-named-paste-as-new buffer)))
      (when (and (not (= (car (gimp-image-base-type image)) INDEXED))
                 (string-ci=? (car (last (strbreakup name "."))) "gif"))
        (gimp-image-convert-indexed image
                                    NO-DITHER
                                    MAKE-PALETTE
                                    255
                                    FALSE
                                    FALSE
                                    "")
        )
      (gimp-file-save RUN-NONINTERACTIVE image (car (gimp-image-get-active-layer image)) name name)
      (gimp-buffer-delete buffer)
      (gimp-image-delete image)
      )
    )
  (let* (
      (filelist (cadr (file-glob file-pattern 1)))
      (filename)
      (ext)
      (image)
      (drawable)
      )
    (while (not (null? filelist))
      (set! ext "png") ;; failsafe if filename has no extension
      (set! filename (car filelist))
      (set! image (car (gimp-file-load RUN-NONINTERACTIVE filename filename)))
      ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
      ;; convert filename.txt to filename_nm.ext
      (set! filename (strbreakup filename "."))
      (when (> (length filename) 1)
        (set! ext (car (last filename)))
        (set! filename (butlast filename))
        )
      (set! filename (unbreakupstr filename "."))
      (set! filename (string-append filename "_nm." ext))
      ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
      ;; assure that image is a single-layer RGB
      (when (= (car (gimp-image-base-type image)) INDEXED)
        (gimp-image-convert-rgb image)
        )
      (set! drawable (car (gimp-image-merge-visible-layers image CLIP-TO-IMAGE)))
      ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
      ;; Run the filter
      (plug-in-normalmap RUN-NONINTERACTIVE image drawable
              0     ; filter (0 = 4 sample, 1 = sobel 3x3, 2 = sobel 5x5,
                    ;         3 = prewitt 3x3, 4 = prewitt 5x5,
                    ;         5-8 = 3x3,5x5,7x7,9x9)
              0.0   ; minz (0 to 1)
              1.0   ; scale (>0)
              TRUE  ; wrap
              0     ; height_source (0=RGB, 1=Alpha)
              0     ; Alpha (0 = unchanged, 1 = set to height,
                    ;        2 = set to inverse height, 3 = set to 0,
                    ;        4 = set to 1, 5 = invert,
                    ;        6 = set to alpha map value)
              0     ; conversion (0 = normalize only, 1 = Biased RGB,
                    ;             2 = Red, 3 = Green, 4 = Blue, 5 = Max RGB,
                    ;             6 = Min RGB, 7 = Colorspace,
                    ;             8 = Normalize only, 9 = Convert to height map)
              0     ; DU/DV map (0 = none, 1 = 8-bit, 2 = 8-bit unsigned,
                    ;            3 = 16-bit, 4 = 16-bit unsigned)
              FALSE ; xinvert
              FALSE ; yinvert
              FALSE ; swapRGB
              0.0   ; contrast
              drawable ; alphamapid (using 'drawable' is a bit of a kludge)
              )
      ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
      ;; save the file
      (set! drawable (car (gimp-image-merge-visible-layers image CLIP-TO-IMAGE)))
      (save-layer image drawable filename)
      (gimp-image-delete image)
      (set! display 0)
      (set! filelist (cdr filelist))
      )
    )
  )


_________________
Everybody makes their own fun. If you don't make it yourself it's not fun, it's entertainment.


Top
 Profile  
 
Offline
 Post subject: Re: Applying a Filter to all images in a directory
PostPosted: Wed Sep 17, 2008 4:10 pm 
User avatar

Joined:
Tue Sep 16, 2008 11:29 am

Topics:
Posts: 6
Location: gmt+1

Find User's Topics
Thanks a lot!

Just tried it on Debian with Gimp 2.2 and it says
Code:
spirit@threat:~/projects$ gimp -i -b '(save-normal-map "/home/spirit/tmp/normalmaptest/")' -b '(gimp-quit 0)'
No batch interpreter specified, using the default 'plug_in_script_fu_eval'.
batch command: executed successfully.
batch command: experienced an execution error.
spirit@threat:~/projects$                       


but I'll try it under win32 with Gimp 2.4 soon.

_________________
spirit


Top
 Profile E-mail  
 
Online
 Post subject: Re: Applying a Filter to all images in a directory
PostPosted: Wed Sep 17, 2008 10:38 pm 
User avatar

Joined:
Mon Aug 22, 2005 8:35 pm

Topics:
Posts: 5087

Find User's Topics
Which version of GIMP are you using? Were any *_nm files created?

I wouldn't recommend using Windows, unless you're prepared to figure things out on your own.

_________________
Everybody makes their own fun. If you don't make it yourself it's not fun, it's entertainment.


Top
 Profile  
 
Offline
 Post subject: Re: Applying a Filter to all images in a directory
PostPosted: Wed Sep 17, 2008 11:13 pm 
User avatar

Joined:
Tue Sep 16, 2008 11:29 am

Topics:
Posts: 6
Location: gmt+1

Find User's Topics
I'm using Gimp 2.2 under Debian Etch. I'll have to compile 2.4 then.

_________________
spirit


Top
 Profile E-mail  
 
Online
 Post subject: Re: Applying a Filter to all images in a directory
PostPosted: Thu Sep 18, 2008 2:56 am 
User avatar

Joined:
Mon Aug 22, 2005 8:35 pm

Topics:
Posts: 5087

Find User's Topics
I thought that might be the case, since the batch interpreter warning is not produced in GIMP 2.4.

Should installing 2.4 be a problem, you might be able to make a couple of changes to the script so that it functions under 2.2 (I can't test this myself because I don't have 2.2 installed anywhere).

The main problem is probably that "when" is not implemented in 2.2 and needs to be replaced with "if" in the corresponding places.
Code:
      (if (> (length filename) 1)
        (begin
          (set! ext (car (last filename)))
          (set! filename (butlast filename))
          )
        )


Code:
      ;; assure that image is a single-layer RGB
      (if (= (car (gimp-image-base-type image)) INDEXED)
        (gimp-image-convert-rgb image)
        )


_________________
Everybody makes their own fun. If you don't make it yourself it's not fun, it's entertainment.


Top
 Profile  
 
Online
 Post subject: Re: Applying a Filter to all images in a directory
PostPosted: Thu Sep 18, 2008 3:04 am 
User avatar

Joined:
Mon Aug 22, 2005 8:35 pm

Topics:
Posts: 5087

Find User's Topics
For what it's worth, here is what the filter produces using the default settings.

Original:
Image
Result:
Image

Is this what should be expected? If not, could you provide an example?

_________________
Everybody makes their own fun. If you don't make it yourself it's not fun, it's entertainment.


Top
 Profile  
 
Offline
 Post subject: Re: Applying a Filter to all images in a directory
PostPosted: Thu Sep 18, 2008 10:34 am 
User avatar

Joined:
Tue Sep 16, 2008 11:29 am

Topics:
Posts: 6
Location: gmt+1

Find User's Topics
That result looks good. I'm testing the script under win32/Gimp2.4 now and I asked a friend to test it on Linux/Gimp 2.4. I'll also adapt the when statement and try it under Linux/Gimp2.2 later.

Thanks again for your efforts!

Btw: Is there a way to have gimp report more detailed error messages? Execution error doesn't help that much...

_________________
spirit


Top
 Profile E-mail  
 
Offline
 Post subject: Re: Applying a Filter to all images in a directory
PostPosted: Thu Sep 18, 2008 1:24 pm 
User avatar

Joined:
Tue Sep 16, 2008 11:29 am

Topics:
Posts: 6
Location: gmt+1

Find User's Topics
Hello saulgoode,

I'd like to tell you that your script works great. I am a member of the Quake2World team and we're now using your script together with that plugin to generate normalmaps for all our textures. We've written a small shellscript that makes using it even more convenient, it's in our svn in src/tools/batch-normalmaps/ together with your scm script.

You really saved us some efforts. Thanks heaps!

_________________
spirit


Top
 Profile E-mail  
 
Display posts from previous:  Sort by  

Post new topic Reply to topic
 [ 11 posts ] 



Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
Related Website
Gimp tutorials database
All rights reserved © GimpTalk 2008
forum software by
phpBB