linux – 用于创建自定义缩略图的Bash脚本
发布时间:2020-12-13 22:49:30 所属栏目:Linux 来源:网络整理
导读:我需要一个bash脚本来获取某些指定文件夹中的所有图像;取出它们的分辨率,如果它低于最小值则不做任何操作,否则创建一个中等拇指图像(200×150像素). 我在Windows中使用Imagemagick.但是在linux上,我不能使用相同的脚本,所以我需要编写一个新的脚本. 这就是我
我需要一个bash脚本来获取某些指定文件夹中的所有图像;取出它们的分辨率,如果它低于最小值则不做任何操作,否则创建一个中等拇指图像(200×150像素).
我在Windows中使用Imagemagick.但是在linux上,我不能使用相同的脚本,所以我需要编写一个新的脚本. 这就是我到目前为止所提出的. #!/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_thumb.jpg,但以下脚本的问题是它存储为filename.jpg_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 解决方法
不使用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 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |