为文件夹中的每个图像生成缩略图

generate thumbnail for every image in folder

我正在尝试编写一个 bash 脚本,为文件夹中的每个图像生成小缩略图版本,因此我可以使用它在 React 中更高效地加载图像。

这个问题的选答我一直在努力工作; Bash script to create customized thumbnails

通过使用修改后的代码:

#!/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 }'`
  if [ x$IMAGE_TYPE = "ximage" ]; then
      IMAGE_SIZE=`file -b $file | sed 's/ //g' | sed 's/,/ /g' | awk  '{print }'`
      WIDTH=`echo $IMAGE_SIZE | sed 's/x/ /g' | awk '{print }'`
      HEIGHT=`echo $IMAGE_SIZE | sed 's/x/ /g' | awk '{print }'`           
      # 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

虽然我的项目布局看起来像这样,但在 /src/ 文件夹中调用 bash .sh 脚本:

但是 运行 带有 bash generate-thumbnails.sh 的脚本导致控制台出错:

$ ./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

我配置 bash 脚本的方式有问题吗?还是我调用它的过程?

  • 使用 file 命令确定图像大小不可靠。 输出格式因图像格式而异。 而是使用 identify,一个 ImageMagick 套件命令。
  • 不建议用户变量使用大写字母。它可能 与系统变量冲突。

你能试试吗:

#!/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

这应该是一个非常可靠的脚本re-implementation:

#!/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