用于创建自定义缩略图的Bash脚本

时间:2012-10-16 11:26:06

标签: linux shell imagemagick thumbnails

我需要一个bash脚本来获取某个指定文件夹中的所有图像;取出它们的分辨率,如果它低于最小值则不做任何操作,否则创建一个中等拇指图像(200x150像素)。

我在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

3 个答案:

答案 0 :(得分:2)

另一种不使用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

答案 1 :(得分:0)

此代码可能更容易理解:

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

答案 2 :(得分:0)

您的脚本略有变化,安装imageinfo按预期工作。 见下面的解决方案:

安装imageinfo工具(在我的情况下已安装,检查您是否已经安装)

sudo apt-get install 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