Linux 创建自定义缩略图的Bash脚本
我需要一个bash脚本,它可以获取指定文件夹中的所有图像;取它们的分辨率,如果低于最小值,则什么也不做,否则创建一个中等大小的拇指图像(200x150像素) 我正在Windows中使用Imagemagick。但是在linux上,我不能使用相同的脚本,所以我需要编写一个新的脚本 这就是我到目前为止提出的问题Linux 创建自定义缩略图的Bash脚本,linux,shell,imagemagick,thumbnails,Linux,Shell,Imagemagick,Thumbnails,我需要一个bash脚本,它可以获取指定文件夹中的所有图像;取它们的分辨率,如果低于最小值,则什么也不做,否则创建一个中等大小的拇指图像(200x150像素) 我正在Windows中使用Imagemagick。但是在linux上,我不能使用相同的脚本,所以我需要编写一个新的脚本 这就是我到目前为止提出的问题 #!/bin/bash for files in /path/to/image/* do TESTFILE=`echo "$files" | sed 's/|/ /g' |
#!/bin/bash
for files in /path/to/image/*
do
TESTFILE=`echo "$files" | sed 's/|/ /g' | xargs file -b | awk '{print $1}'`
while read F
CHECKSIZE=`file "$TESTFILE" -b | sed 's/ //g' | sed 's/,/ /g' | awk '{print $2}' | sed 's/x/ /g' | awk '{print $1}'`
if [ $CHECKSIZE -ge 200 ]; then
convert -sample 200x150 "$F" "$F{_thumb}"
fi
done
done
但是当我运行这个脚本时,它既没有给我缩略图,也没有给我任何错误。我对这些脚本非常陌生
更新:
我已经想出了这个剧本,谢谢大家。但现在我需要更多的帮助。现在我想将新图像存储在images文件夹中的一个文件夹中。例如,/home/image是所有文件所在的位置。我希望拇指图像存储在/home/image/thumbs中。我还想将文件重命名为filename\u thumb.jpg,但以下脚本的问题是它存储为filename.jpg\u thumb
#!/bin/bash
THUMBS_FOLDER=/home/temp/thumbs
for file in /home/temp/*
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=`identify -format "%w" "$file"`
HEIGHT=`identify -format "%h" "$file"`
# 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
此代码可能更容易理解:
#!/bin/bash
for file in /path/to/images/*
do
# next line checks the mime-type of the file
CHECKTYPE=`file --mime-type -b "$file" | awk -F'/' '{print $1}'`
if [ "x$CHECKTYPE" == "ximage" ]; then
CHECKSIZE=`stat -f "%z" "$file"` # this returns the filesize
CHECKWIDTH=`identify -format "%W" "$file"` # this returns the image width
# next 'if' is true if either filesize >= 200000 bytes OR if image width >=201
if [ $CHECKSIZE -ge 200000 ] || [ $CHECKWIDTH -ge 201 ]; then
convert -sample 200x150 "$file" "$(dirname "$file")/thumb_$(basename "$file")"
fi
fi
done
您的脚本只需稍作更改,安装imageinfo即可正常工作。 请参见下面的解决方案: 安装imageinfo工具(在我的情况下,它已安装,请检查您是否已经安装) 还有剧本:
#!/bin/bash
for file in ./image/*
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
WIDTH=`imageinfo --width "$file"` # obtaining the image width
HEIGHT=`imageinfo --height "$file"` # obtaining the image height
# 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
convert -sample 200x150 "$file" "$(dirname "$file")/thumb_$(basename "$file")"
fi
fi
done
不使用imageinfo的另一种方法: 请记住更改图像路径,在我的情况下,我在同一文件夹级别使用名为imgs的文件夹 将内容复制到名为create_thumbs.sh的文件中,然后粘贴下一个代码:
#!/bin/bash
THUMBS_FOLDER=/home/image/thumb
for file in /home/image/*
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
称之为:
bash create_thumbs.sh
在读取F行时,紧跟在
之后的行中缺少一个do
。你能回顾一下你问题中的代码吗?@Rajkumar:当然,你需要用真实的目录路径替换/path/to/images/
(在脚本的第二行中)…@Rajkumar:我仔细检查了文件大小和图像宽度,因为从你的问题中我不清楚你要测试哪一个。。。修改代码以仅使用所需的代码应该很容易。我将路径/to/images更改为我的文件夹,现在它给了我newScript.sh:第10行:[:%W:整数表达式预期错误。第10行指向[$CHECKWIDTH-ge 201](当然,我只想检查分辨率)。你能告诉我是什么问题吗?请看我的问题。我用新代码更新了它,并帮助我。顺便说一句,谢谢#sgroh当你在Ubuntu上安装imageinfo时也安装了:imagemagick common liblqr-1-0 libmagickcore4#sgroh。请看我的原始帖子并帮助我。是的,只是发布solution@Rajkumar,该解决方案没有使用imageinfo和identify(这是ImageMagick库的一部分)。我只保留ImageMagic中的convert。请参阅,我希望它对您有所帮助。不起作用。给出以下错误。*moddified-script.sh:第10行:p:未找到命令moddified-script.sh:第12行:[:JFIFstandard1.02:需要整数表达式moddified-script.sh:第12行:[:-ge:unary operator expected*请编辑我的更新代码,以便我可以轻松进行。因为我的更新脚本工作正常,我只想将其存储在新文件夹中。谢谢您的帮助again@Rajkumar,我已添加了要以您正在查找的方式保存的更改。关于错误,您是否尝试使用./creat调用脚本e_thumbs.sh?@Rajkumar,我测试了解决方案并解决了您发布的错误,请再次检查并告诉我它是否有效。谢谢,伙计,它工作得很好。我刚刚将您的代码改为使用“标识”而不是$IMAGE_SIZE,它正在工作。谢谢您的帮助
bash create_thumbs.sh