๐Ÿ“œ โฌ†๏ธ โฌ‡๏ธ

Batch Image Processing in GIMP

From time to time it becomes necessary to upload a photo album or a package of scanned documents to the network. In most cases, the images must first be reduced, and sometimes additional processing must be done, and labels must be added. And now the moment came when it became completely lazy to perform operations of the same type. We remember about great automation tools in a great and free GIMP editor.

Suppose we have a bunch of photos of different sizes, with different aspect ratios and saved in different formats. In general, a difficult case. We need to align this whole zoo in size, taking into account proportions, and save it in JPEG with the specified quality. Yes, and let us sometimes want to set the size not in pixels, but in percentages. Well, let him then want to choose files by mask, like โ€œC: \ images \ img_01 * .jpgโ€.

We sit down and scribble on Script-Fu (available by default in GIMP, language) first scenario:
')
(define (batch-resize pattern size quality)
(let * ((filelist (cadr (file-glob pattern 1))))
(len (if (string? size) (string-length size) 0))
(rate (if (and (> len 0) (char =? # \% (string-ref size (- len 1)))) (/ (string-> number (substring size 0 (- len 1))) 100 ) 0))
(size (if (> rate 0) 0 (if (> len 0) (string-> number size) size)))
)
(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))))
(old-width (car (gimp-image-width image))))
(old-height (car (gimp-image-height image))))
(width (if (> rate 0) (* rate old-width) (if (> old-width old-height) size (* size (/ old-width old-height)))))
(height (if (> rate 0) (* rate old-height) (if (> old-width old-height) (* size (/ old-height old-width)) size)))
)
(gimp-image-scale image width height)
(file-jpeg-save RUN-NONINTERACTIVE image drawable filename quality 0 1 1 "" 2 1 0 0)
(gimp-image-delete image)
)
(set! filelist (cdr filelist))
)
)
)

The easiest way to use this joy is to insert (with text or from a file) into the Script-Fu console, and press Enter. Now our batch-resize feature is available for use. We use it like this (in the console): (batch-resize "C:\\images\\*.jpg" 640 1) . Or this: (batch-resize "C:\\images\\i_am_clever_*.*" "25%" 0.75) . Do not forget that in the lines you need to escaped slashes, so in the paths under Windows you will have to double slashes. The second parameter is the size of the larger side, indicated by a number (in pixels) or a string (if with a percentage, then the size is calculated in percent). The third is the quality of saved JPEG images (from 0 to 1).





Further only for the curious. :)

In the file-jpeg-save function, in addition to the quality of the saved image, some other parameters are set that affect the same quality and, accordingly, size. The description of the parameters of this function is clear in the procedure viewer, but it is not written anywhere which indices are assigned to the values โ€‹โ€‹of the subsample (the 4th parameter from the end) and the DCT method (the last parameter). I saved images with different values โ€‹โ€‹of these parameters from the program itself and with the help of the function, and by the size of the files I calculated the necessary indices.

So, to specify the required subsample, they are used: 0 - 2 ร— 2โ€“1 ร— 1โ€“1 ร— 1 (by default, the worst color transfer, the smallest file), 1 - 2 ร— 1โ€“1 ร— 1โ€“1 ร— 1 (4: 2 : 2, colors are better transmitted, 2 - 1 ร— 1โ€“1 ร— 1โ€“1 ร— 1 (colors are not distorted, the file is larger than others), 3 - 1 ร— 2โ€“1 ร— 1โ€“1 ร— 1 (in color reproduction it is similar to 2 ร— 1โ€“1 ร— 1โ€“1 ร— 1, size is larger).

DCT methods: 0 - integer, 1 - fast integer, 2 - with a floating point. Visually and in terms of speed, I did not notice the difference, the file sizes were also quite slightly different. Theoretically, floating point runs slower and introduces less distortion.

In the scenario published above, the DCT (0) integer method is used and the type of sub-sample with the best color rendition is 1 ร— 1โ€“1 ร— 1โ€“1 ร— 1 (2).

Related Links:
1. A good, though outdated, description of Script-Fu in Russian .
2. Description of the Scheme language . Here you can spy features that are not in the procedures routine. But keep in mind that not all Scheme functions are available in Script-Fu.
3. Description Scheme and more links to Wikipedia .
4. GIMP 2.0 Api Reference Manual .
5. Many different scenarios , broken down by category.
6. More scripts .
7. Our script with syntax highlighting and nested parentheses.

Source: https://habr.com/ru/post/25911/


All Articles