为文件夹中的每个图像生成缩略图
我正在尝试编写一个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:
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这应该是您脚本的可靠重新实现:
This should be a pretty solid re-implementation of your script:
文件
命令的使用情况不可靠。输出格式取决于图像格式。
而是使用
标识
,一个ImageMagick suite命令。与系统变量冲突。
您能改用:
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.conflict with system variables.
Would you please try instead: