Gimptalk - Premier Gimp Community: Applying a Filter to all images in a directory - Gimptalk - Premier Gimp Community

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Applying a Filter to all images in a directory

#1 User is offline   sp1r1t 

  • Newbie
  • Pip
  • Group: Members
  • Posts: 6
  • Joined: 16-September 08
  • Locationgmt+1

Posted 16 September 2008 - 11:43 AM

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
    [*:lv0ca3ca]to apply the Filter to all images in a directory
    [*:lv0ca3ca]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
0

#2 User is offline   saulgoode 

  • Retired Staff
  • PipPipPip
  • Group: Retired Staff
  • Posts: 5,324
  • Joined: 22-August 05

Posted 17 September 2008 - 06:29 AM

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.
0

#3 User is offline   sp1r1t 

  • Newbie
  • Pip
  • Group: Members
  • Posts: 6
  • Joined: 16-September 08
  • Locationgmt+1

Posted 17 September 2008 - 10:09 AM

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
0

#4 User is offline   saulgoode 

  • Retired Staff
  • PipPipPip
  • Group: Retired Staff
  • Posts: 5,324
  • Joined: 22-August 05

Posted 17 September 2008 - 03:46 PM

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".

;; 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.
0

#5 User is offline   sp1r1t 

  • Newbie
  • Pip
  • Group: Members
  • Posts: 6
  • Joined: 16-September 08
  • Locationgmt+1

Posted 17 September 2008 - 04:10 PM

Thanks a lot!

Just tried it on Debian with Gimp 2.2 and it says
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
0

#6 User is offline   saulgoode 

  • Retired Staff
  • PipPipPip
  • Group: Retired Staff
  • Posts: 5,324
  • Joined: 22-August 05

Posted 17 September 2008 - 10:38 PM

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.
0

#7 User is offline   sp1r1t 

  • Newbie
  • Pip
  • Group: Members
  • Posts: 6
  • Joined: 16-September 08
  • Locationgmt+1

Posted 17 September 2008 - 11:13 PM

I'm using Gimp 2.2 under Debian Etch. I'll have to compile 2.4 then.
spirit
0

#8 User is offline   saulgoode 

  • Retired Staff
  • PipPipPip
  • Group: Retired Staff
  • Posts: 5,324
  • Joined: 22-August 05

Posted 18 September 2008 - 02:56 AM

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.
      (if (> (length filename) 1)
        (begin 
          (set! ext (car (last filename)))
          (set! filename (butlast filename))
          )
        )


      ;; 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.
0

#9 User is offline   saulgoode 

  • Retired Staff
  • PipPipPip
  • Group: Retired Staff
  • Posts: 5,324
  • Joined: 22-August 05

Posted 18 September 2008 - 03:04 AM

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

Original:
Posted Image
Result:
Posted 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.
0

#10 User is offline   sp1r1t 

  • Newbie
  • Pip
  • Group: Members
  • Posts: 6
  • Joined: 16-September 08
  • Locationgmt+1

Posted 18 September 2008 - 10:34 AM

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
0

#11 User is offline   sp1r1t 

  • Newbie
  • Pip
  • Group: Members
  • Posts: 6
  • Joined: 16-September 08
  • Locationgmt+1

Posted 18 September 2008 - 01:24 PM

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
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic