Showing posts with label GIMP. Show all posts
Showing posts with label GIMP. Show all posts

Sunday, December 19, 2021

Reducing image size with GIMP in batch mode

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


~/.gimp-2.8/scripts

with .scm extension.

In 2.10, the script directory is ~/.config/GIMP/2.10/scripts


(define (batch-reduce-img-size pattern reduceby)
  (let* ((filelist (cadr (file-glob pattern 1))))
    (while (not (null? filelist))
           (let* ((filename (car filelist))
                  (image (car (gimp-file-load RUN-NONINTERACTIVE filename filename)))
                  (imageWidth (car (gimp-image-width image)))
		   (imageHeight (car (gimp-image-height image)))
		   (newWidth (* imageWidth reduceby))
		   (newHeight (* imageHeight reduceby))
                  (drawable (car (gimp-image-get-active-layer image))))
				   (gimp-image-scale image newWidth newHeight)
                                  (gimp-file-save 1 image drawable filename filename)
                                  
(gimp-image-delete image))
           (set! filelist (cdr filelist)))))


run the script as 


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


IMPORTANT: The images will get over written




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%