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

发布于 2025-01-24 10:24:33 字数 2707 浏览 1 评论 0原文

我正在尝试编写一个Bash脚本,该脚本为文件夹中的每个图像生成小缩略图版本,因此我可以将其用于React中更有效的图像加载。

我一直试图开始工作的这个问题的选定答案; bash脚本来创建自定义的缩略图

通过使用此修改后的代码:

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

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

“在此处输入图像说明”

但是使用bash bash generate-thumbnails.shs.shs.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脚本的问题是一个问题?还是我打电话给它的过程?

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:

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

哽咽笑 2025-01-31 10:24:33

这应该是您脚本的可靠重新实现:

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

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
三岁铭 2025-01-31 10:24:33
  • 文件命令的使用情况不可靠。
    输出格式取决于图像格式。
    而是使用标识,一个ImageMagick suite命令。
  • 不建议将大写用于用户变量。可能
    与系统变量冲突。

您能改用:

#!/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
  • 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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文