Rajkumar
Rajkumar

Reputation: 440

Bash script to create customized thumbnails

I need a bash script that get all the images inside some specified folder; take their resolution and if it is below minimum then do nothing, otherwise create a medium thumb image (200x150 pixels).

I am using Imagemagick in Windows. But on linux, I can't use the same script so I need to write a new script.

This is what i have come up so far.

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

But when i run this script, it is not giving me thumbnails nor giving me any errors. I am pretty new to these scripting.

Update :

I have come up with this script, thanks for all. But now i need one more help. Now i want to store the new image in a folder inside the images folder. For example, /home/image is where all the files are. I want thumb images to store in /home/image/thumbs. Also i want to rename files as filename_thumb.jpg, but the issue with following script is it is storing as 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

Upvotes: 5

Views: 6376

Answers (3)

sgroh
sgroh

Reputation: 354

Another approach without use imageinfo:

Please remember to change the images path, in my case I use a folder called imgs at the same folder level.

Copy the content in a file called create_thumbs.sh, and paste next code:

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

To call it:

bash create_thumbs.sh

Upvotes: 4

sgroh
sgroh

Reputation: 354

your script with a slight change and installing imageinfo works as you expect. See below the solution:

Install the imageinfo tool (in my case it was installed, check if you already have it)

sudo apt-get install imageinfo

And the script:

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

Upvotes: 0

Kurt Pfeifle
Kurt Pfeifle

Reputation: 90335

This code may be easier to understand:

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

Upvotes: 0

Related Questions