Saturday, January 2, 2010

Batch Resize Images in GIMP

It is always required to resize a set of images at once. So that, no need to do it one by one. Let's see, how this can be achieved using GIMP image editor in Ubuntu.

We are using the batch mode of GIMP to do image processing from the command line. Let's write a simple script in Scheme language to resize a set of image files.

(define (batch-resize pattern width height)
(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))))
(gimp-image-scale-full image width height INTERPOLATION-CUBIC)
(gimp-file-save RUN-NONINTERACTIVE image drawable filename filename)
(gimp-image-delete image))
(set! filelist (cdr filelist)))))

This particular script takes a pattern for filename, desired width and height as inputs and resize all image files that are matched. This script overwrite the existing file. In order to run the script, save it with .scm extension in the ~/.gimp-/scripts/ directory.
Then goto the directory which contains images, then run the following command to resize images.

gimp -i -b '(batch-resize "*.JPG" 604 453)' -b '(gimp-quit 0)'
The above command will resize all the image files end with .JPG to 604X453
The above script can be customized to any other image processing requirements as well. Refer the Help -> Procedure Browser in GIMP for more operations.

The following script rename the resized image file by adding 'a' to the beginning of the file name

(define (batch-resize-rename pattern width height)
(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))))
(gimp-image-scale-full image width height INTERPOLATION-CUBIC)
(let ((nfilename (string-append "a" filename)))
(gimp-file-save RUN-NONINTERACTIVE image drawable nfilename nfilename))
(gimp-image-delete image))
(set! filelist (cdr filelist)))))

By referring to the GIMP procedure and plugins browser and a Scheme tutorial, it is possible to customize the above script as required.

References:

7 comments:

  1. How about if I need to resize the images by percentage, as the images may have various dimensions.

    ReplyDelete
  2. Awesome, thanks! I changed interpolation to INTERPOLATION-LANCZOS since it looks a little nicer, but I couldn't have done anything without your blog here.

    ReplyDelete
  3. Thanks for this helpful Blog.
    I used a WordPress plugin that I found interesting. So I would like to recommend it.
    The plugin helps me quickly create and customize images before inserting them in my blog post.
    Check at: https://getpikiz.com/wordpress-plugin/
    It’s free.

    ReplyDelete
  4. Thanks for this helpful Blog.
    I used a WordPress plugin that I found interesting. So I would like to recommend it.
    The plugin helps me quickly create and customize images before inserting them in my blog post.
    Check at: https://getpikiz.com/wordpress-plugin/
    It’s free.

    ReplyDelete
  5. Thanks for this helpful Blog.
    I used a WordPress plugin that I found interesting. So I would like to recommend it.
    The plugin helps me quickly create and customize images before inserting them in my blog post.
    Check at: https://getpikiz.com/wordpress-plugin/
    It’s free.

    ReplyDelete
  6. Get the Best Bulk Product Upload Services From Us With the drastic facelift that the retail sector has seen in the recent past, the internet gas become the hub for most of the small, medium as well as large scale businesses that are involved in the same.The ecommerce has seen a monumental growth prospective over the last couple of years and the trend is expected to remain the same for the foreseeable future as well. In this regard, what concerns the businesses and the online retail outlet is the data entry aspect. Tech data entry India provides a number of products and services to cater to the Amazon Bulk Product Listing Services.

    ReplyDelete