Thursday, September 14, 2017

Reducing image quality with GIMP in batch mode

Recently I got some photos and I needed to reduce its quality. I was able to open an image in GIMP. and by following the following menu I can reduce the image quality

File --> Export As... --> (choose file name) --> Export --> (select quality) Export



This is fine for a single image. But when there are lot of images it is not easy to open one by one and reducing its quality.

This is where the GIMP's batch mode processing helps a lot.

First we define a function which contains all the needed procedures. and save the script in

~/.gimp-2.8/scripts

with .scm extension.

To view all the procedures and its parameters, go to
Help --> Procedure Browser

Now call the function as follows
gimp -i -b '(<function name> <arguments>)' -b '(gimp-quit 0)'


Following is a sample of reducing the image quality.
(define (batch-reduce-img-quality pattern quality)
  (let* ((filelist (cadr (file-glob pattern 1))))
    (while (not (null? filelist))
           (let* ((filename (car filelist))
                  (image (car (gimp-file-load RUN-NONINTERACTIVE filename filename)))
                  (drawable (car (gimp-image-get-active-layer image))))
                                  (file-jpeg-save 1 image drawable filename filename quality 0 1 1 "" 0 1 0 0)
(gimp-image-delete image))
           (set! filelist (cdr filelist)))))

once the above script is saved in  ~/.gimp-2.8/scripts with .scm extension (any name)

enter the following command to reduce the image quality from the directory which contains the images.
IMPORTANT: this is overwrite the original image with the reduced quality image.


gimp -i -b '(batch-reduce-img-quality "*.JPG" 0.5)' -b '(gimp-quit 0)'

The above command will reduce the quality of the images available in the directory to 50% 

2 comments:

  1. Hi. When trying to run it on the jpg's dir: "Error: unmatched parentheses: 3


    script-fu-Warning: Error while loading /home/leopoldo/.gimp-2.6/scripts/reduce.scm:

    Error: unmatched parentheses: 3


    batch command experienced an execution error"

    ReplyDelete
    Replies
    1. Hi,
      This may be due to the gimp version. Because I was able to test this in gimp 2.8
      Further you can verify the syntax in your gimp itself for errors
      In gimp. Go to "Filters" --> "Script-Fu" --> "Console"

      Delete