Home > Software design >  generate thumbnail for every image in folder
generate thumbnail for every image in folder

Time:04-26

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; enter image description here

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?

CodePudding user response:

  • The usage of 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.
  • It is not recommended to use uppercases for user variables. It may conflict with system variables.

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

CodePudding user response:

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
  command -V "$cmd"
done >/dev/null 2>&1; then
  printf 'Missing required command %s\n' "$cmd" >&2
  exit 1
fi

img_folder=~/Images
thumbs_folder="$img_folder/thumbnails"

# Creates thumbnails directory if not exit
mkdir -p "$thumbs_folder"

for file in "$img_folder/"*; do
  # If $file = pattern then no match, exit
  [ "$file" = "$img_folder/*" ] && exit

  mime_type="$(file -b --mime-type "$file" 2>&1)" || continue

  # Check 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 200 ] || [ "$height" -gt 150 ]; }; then
      basename="${file##*/}"
      extension="${basename##*.}"
      ext_less="${basename%.*}"
      thumb_file="${thumbs_folder}/${ext_less}_thumb.${extension}"
      printf 'Create thumb file for %s, size: %d %d\n' \
        "$file" "$width" "$height"
      convert -sample 200x150 "$file" "$thumb_file"
    fi
  }
done
  • Related