Gimp-Forum.net
batch command experienced an execution error - Printable Version

+- Gimp-Forum.net (https://www.gimp-forum.net)
+-- Forum: GIMP (https://www.gimp-forum.net/Forum-GIMP)
+--- Forum: Extending the GIMP (https://www.gimp-forum.net/Forum-Extending-the-GIMP)
+--- Thread: batch command experienced an execution error (/Thread-batch-command-experienced-an-execution-error)



batch command experienced an execution error - tristan - 07-17-2023

This script - full disclosure, I made it with the help of AI as I'm not much of a coder  - is getting the following error:

batch command experienced an execution error:

Error: /: argument 1 must be: number

gimp: GEGL-WARNING: (../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


I haven't been able to figure out how to fix it. I've tried the usual things--making sure GIMP is up to date, etc.

Thanks for any help offered.

Code:
#!/bin/bash

# Prompt for image selection
input_image=$(zenity --file-selection --title="Select an image or document" --file-filter="Image files | *.jpg *.jpeg *.png *.tiff *.webm *.pdf")

# Check if a file was selected or the dialog was canceled
if [ -z "$input_image" ]; then
 echo "No file selected. Exiting."
 exit 0
fi

# Extract the directory path from the input file
input_dir=$(dirname "$input_image")

# Extract the filename from the full path
filename=$(basename "$input_image")

# Construct the output image path in the same directory as the input file
output_image="${input_dir}/${filename}.jpg"

# Step 1: Scale the image to a width of 800px
# Step 2: Fit canvas to image size
# Step 3: Export to JPG with quality set to 80
gimp -i -b "(let* ((image (car (gimp-file-load RUN-NONINTERACTIVE \"$input_image\" \"$input_image\")))
                 (drawable (car (gimp-image-get-active-layer image))))
             (gimp-image-scale image 800 (/ (gimp-image-width image) (gimp-image-height image)))
             (gimp-image-resize-to-layers image)
             (file-jpeg-save RUN-NONINTERACTIVE image drawable \"$output_image\" \"$output_image\" 0.8 0 1 1 \"\" \"\")
             (gimp-image-delete image))" -b "(gimp-quit 0)"



RE: batch command experienced an execution error - Ofnuts - 07-17-2023

Plenty of wrong things:
  • The input extension isn't removed when generating the output name, so you get things like foobar.jpg.jpg (note that this also avoid overwriting and input .jpg file).
  • Gimp is started/exited for each image: huge startup overhead
  • When you scale an image it scales canvas and layers, so gimp-image-resize-to-layers image is pointless
  • file-jpeg-save is missing a lot of parameters
Error: /: argument 1 must be: number is likely because (gimp-image-width image) didn't return a number.

Now, why would you use Gimp just to resize images. This is my personal script to create low-res copies of existing images (also gives a subtle contrast boost and some sharpening to compensate the blur from the rescale operation)

Code:
#! /bin/bash

size=3000
quality=85
dir=.
suffix=""


function error {
    >&2 echo "*** $*"
    exit 1
}

function usage {
    echo "$0 [-g <geometry>] [-q quality] [-d dir] [-s suffix] files"
}
        
OPTIND=1         # Reset in case getopts has been used previously in the shell.
while getopts "h?q:g:d:s:" opt; do
    case "$opt" in
    h|\?)
        usage
        exit 1
        ;;
    g)  size="$OPTARG"
        [[ "$size" =~ ^[0-9]+$ ]] || error "Size not a number: $size"
        [[ "$size" -lt 400 ]] && error "Size too small (<400)"
        ;;
    q)  quality="$OPTARG"
        [[ "$quality" =~ ^[0-9]+$ ]] || error "Quality not a number: $quality"
        [[ "$quality" -lt 50 || "$quality" -gt 98 ]] && error "Quality not in [50..98]"
        ;;
    s)  suffix="$OPTARG"
        ;;
    d)  dir="$OPTARG"
        ;;
    esac
done

shift $((OPTIND-1))
[[ "${1:-}" = "--" ]] && shift

mkdir -p "$dir" || error "Can't create directory"

for f in "$@"
do
    outname=$(basename "$f")
    if [[ -z $suffix ]]
    then
        out=$dir/${outname%.*}.jpg
    else
        out=$dir/${outname%.*}-$suffix.jpg
    fi
    echo "Exporting $f to $out"
    [[ -e $out ]] && error "$out already exists"
    convert "$f" -modulate 100,120  -geometry ${size}x${size} -sharpen 0x1.0 -quality $quality "$out"
done