Reputation: 1578
I am trying to write a bash script that generates small thumbnail versions for every image in a folder, so I can use it for more efficient image loading in react.
The selected answer from this question I have been trying to get working; Bash script to create customized thumbnails
By using this modified code:
#!/bin/bash
THUMBS_FOLDER=./aesthetic-images/thumbnails
for file in ./aesthetic-images/*
do
# next line checks the mime-type of the file
IMAGE_TYPE=`file --mime-type -b "$file" | awk -F'/' '{print $1}'`
if [ x$IMAGE_TYPE = "ximage" ]; then
IMAGE_SIZE=`file -b $file | sed 's/ //g' | sed 's/,/ /g' | awk '{print $2}'`
WIDTH=`echo $IMAGE_SIZE | sed 's/x/ /g' | awk '{print $1}'`
HEIGHT=`echo $IMAGE_SIZE | sed 's/x/ /g' | awk '{print $2}'`
# If the image width is greater that 200 or the height is greater that 150 a thumb is created
if [ $WIDTH -ge 201 ] || [ $HEIGHT -ge 151 ]; then
#This line convert the image in a 200 x 150 thumb
filename=$(basename "$file")
extension="${filename##*.}"
filename="${filename%.*}"
convert -sample 200x150 "$file" "${THUMBS_FOLDER}/${filename}_thumb.${extension}"
fi
fi
done
While my project layout looks like so, calling the bash .sh script inside the /src/ folder:
But running the script with bash generate-thumbnails.sh
leads to errors in console:
$ ./generate-thumbnails.sh
./generate-thumbnails.sh: line 12: [: JFIFstandard1.01: integer expression expected
./generate-thumbnails.sh: line 12: [: -ge: unary operator expected
./generate-thumbnails.sh: line 12: [: JFIFstandard1.01: integer expression expected
./generate-thumbnails.sh: line 12: [: -ge: unary operator expected
./generate-thumbnails.sh: line 12: [: JFIFstandard1.01: integer expression expected
./generate-thumbnails.sh: line 12: [: -ge: unary operator expected
./generate-thumbnails.sh: line 12: [: progressive: integer expression expected
./generate-thumbnails.sh: line 12: [: -ge: unary operator expected
./generate-thumbnails.sh: line 12: [: JFIFstandard1.01: integer expression expected
./generate-thumbnails.sh: line 12: [: -ge: unary operator expected
./generate-thumbnails.sh: line 12: [: JFIFstandard1.01: integer expression expected
./generate-thumbnails.sh: line 12: [: -ge: unary operator expected
./generate-thumbnails.sh: line 17: convert: command not found
./generate-thumbnails.sh: line 12: [: JFIFstandard1.01: integer expression expected
./generate-thumbnails.sh: line 12: [: -ge: unary operator expected
Is it a problem with how I configured the bash script? Or my process in calling it?
Upvotes: 2
Views: 2443
Reputation: 19665
This should be a pretty solid re-implementation of your script:
#!/usr/bin/env sh
# Checks required ImageMagic commands are available or exit fail
if ! for cmd in identify convert; do
if ! command -V "$cmd" >/dev/null 2>&1; then
printf 'Missing ImageMagic required command: %s\n' "$cmd"
false
fi
done >&2; then
exit 1
fi
img_folder=~/Images
thumbs_folder="$img_folder/thumbnails"
thumb_width=200
thumb_height=150
# Creates thumbnails directory if not exist
mkdir -p "$thumbs_folder"
for file in "$img_folder/"*; do
# If $file = pattern then no match, exit
[ "$file" = "$img_folder/*" ] && exit
# Gets file MIME type or skip file if it fails
mime_type="$(file -b --mime-type "$file" 2>&1)" || continue
# Checks what to do based on mime-type
case $mime_type in
image/x-xcf) continue ;; # Not supported
image/*) ;; # Accept for processing
*) continue ;; # Not an image
esac
identify -format '%w %h' "$file" | {
# Reads piped-in width and height
read -r width height
if [ "$width" ] && [ "$height" ] && {
[ "$width" -gt "$thumb_width" ] || [ "$height" -gt "$thumb_height" ]
}; then
basename="${file##*/}"
extension="${basename##*.}"
ext_less="${basename%.*}"
thumb_file="${thumbs_folder}/${ext_less}_thumb.${extension}"
printf 'Create thumb file for %s, size: %dx%d\n' \
"$file" "$width" "$height"
convert -sample "${thumb_width}x${thumb_height}" "$file" "$thumb_file"
fi
}
done
Upvotes: 2
Reputation: 22042
file
command to determine image size is unreliable.
The output format varies depending on the image format.
Instead make use of identify
, a ImageMagick suite command.Would you please try instead:
#!/bin/bash
thumbs_folder=./aesthetic-images/thumbnails
mkdir -p "$thumbs_folder"
for file in ./aesthetic-images/*; do
# next line checks the mime-type of the file
image_type=$(file --mime-type -b "$file")
if [[ $image_type = image/* ]]; then
image_size=$(identify -format "%[fx:w]x%[fx:h]" "$file")
IFS=x read -r width height <<< "$image_size"
# If the image width is greater that 200 or the height is greater that 150 a thumb is created
if (( width > 200 || height > 150 )); then
#This line convert the image in a 200 x 150 thumb
filename=$(basename "$file")
extension="${filename##*.}"
filename="${filename%.*}"
convert -sample 200x150 "$file" "${thumbs_folder}/${filename}_thumb.${extension}"
fi
fi
done
Upvotes: 1