I try to use batch to transform images into defined quality .jpg with tags and with file name normalization, plus generation of .png thumbnails restricted to max dimensions of 169×127 px, scaled proportionally. The thumbnails part of code doesn’t work.
Algorithm for part of code I need is simple:
Checks if original-width > original-height,
if true: new-height = 127, new-width = original-width * new-height / original-height
else: new-width = 169, new-height = original-height * new-width / original-width
Then just create .png with compression level = 7, name it as NEWFILE & “_t.png”
So it’s composed as below with support of ChatGPT – the .bat file accessing GIMP’s console:
%GIMP_PATH% -i -b "(let* ((image (car (gimp-file-load RUN-NONINTERACTIVE "%%F" "%%F"))) (drawable (car (gimp-image-get-active-layer image))) (original-width (car (gimp-image-width image))) (original-height (car (gimp-image-height image))) (new-width 169) (new-height 127) (if (> original-width original-height) ((gimp-image-scale image (round ((* (original-width) (/ (new-height) (original-height)))))) (new-height)) ((gimp-image-scale image (new-width) (round ((* (original-height) (/ (new-width) (original-width)))))))) (file-png-save RUN-NONINTERACTIVE image drawable (string-append "!NEWFILE!_t.png") (string-append "!NEWFILE!_t.png") 0 7 0 0 0 0 0) (gimp-image-delete image)))" -b "(gimp-quit 0)"
When launching .bat above part of code cause an error and does not save .png thumbnail. Full error message:
C:Program FilesGIMP 2bingimp-console-2.10.exe: GEGL-OSTRZEŻENIE: (../gegl/buffer/gegl-tile-handler-cache.c:1076):gegl_tile_cache_destroy: runtime check failed: (g_queue_is_empty (&cache_queue))
EEEEeEeek! 2 GeglBuffers leaked
To debug GeglBuffer leaks, set the environment variable GEGL_DEBUG to "buffer-alloc"
Full actual code of .bat, which works without above part:
@echo off
setlocal enabledelayedexpansion
chcp 65001
:: Prefix
set pref="arma3_"
set GIMP_PATH="C:Program FilesGIMP 2bingimp-console-2.10.exe"
if not exist %GIMP_PATH% (
echo GIMP is not installed at %GIMP_PATH%. Please check the installation path.
exit /b
)
for %%F in (*.jpg *.jpeg *.png *.bmp *.gif *.tiff) do (
set FILE=%%~nF
set EXT=%%~xF
for /f "delims= eol=" %%A in ('powershell -NoLogo -NoProfile -Command "'!FILE!'.ToLower()"') do (set "STRING=%%~A")
for /f "tokens=*" %%i in ('powershell -NoLogo -NoProfile -Command "($env:STRING) -replace '[()]','' -replace ' ','_' -replace 'ą','a' -replace 'ć','c' -replace 'ę','e' -replace 'ł','l' -replace 'ń','n' -replace 'ó','o' -replace 'ś','s' -replace 'ź','z' -replace 'ż','z'"') do (set STRING=%%i)
set "NEWFILE=!pref!!STRING!"
:: Convert to JPG with 97% quality
%GIMP_PATH% -i -b "(let* ((image (car (gimp-file-load RUN-NONINTERACTIVE "%%F" "%%F"))) (drawable (car (gimp-image-get-active-layer image)))) (file-jpeg-save RUN-NONINTERACTIVE image drawable (string-append "!NEWFILE!.jpg") (string-append "!NEWFILE!.jpg") 0.97 0 0 0 "" 0 1 0 1) (gimp-image-delete image))" -b "(gimp-quit 0)"
:: Convert to PNG with compression level 7 and scaled dimensions
%GIMP_PATH% -i -b "(let* ((image (car (gimp-file-load RUN-NONINTERACTIVE "%%F" "%%F"))) (drawable (car (gimp-image-get-active-layer image))) (original-width (car (gimp-image-width image))) (original-height (car (gimp-image-height image))) (new-width 169) (new-height 127) (if (> original-width original-height) ((gimp-image-scale image (round ((* (original-width) (/ (new-height) (original-height)))))) (new-height)) ((gimp-image-scale image (new-width) (round ((* (original-height) (/ (new-width) (original-width)))))))) (file-png-save RUN-NONINTERACTIVE image drawable (string-append "!NEWFILE!_t.png") (string-append "!NEWFILE!_t.png") 0 7 0 0 0 0 0) (gimp-image-delete image)))" -b "(gimp-quit 0)"
)
echo All images have been processed.
pause
Sorry in advance that vital not working part is in a single line and has additional brackets, which was a way to try debug it. I’m just starting with .bat files and especially first thing related with GIMP (Script-Fu).
I tried to debug it, checking if phrasing is correct, used ChatGPT to check for errors and added additional brackets, nothing helped, error still appears.
Frozen Mind is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.