Create thumbnails with a mask

First we create a mask like this

with this script:

 
#!/bin/bash
# Author:     kdguntu@gmail.com
# Copyright:  (c) 2011 GNU Copyright v3
# Name:       create_mask.sh
# Function:   create thumbnail mask
# Requires:   imagemagick (convert)
 
THUMBRECT="thumbnail_rectangle.png"
THUMBMASK="thumbnail_mask.png"
SIZE=96                                   # thumbnail output size
CSIZE=$((SIZE/10))                        # corner size (10%)
SIZEOK=$((SIZE-1))                        # minus 1 pixel
SIZEX="$SIZEOK"; SIZEY="$SIZEOK"          # shadow will use an extra pixel
 
# Create a gray rectangle with rounded corners
RECT="roundrectangle $CSIZE,$CSIZE $SIZEX,$SIZEY $CSIZE,$CSIZE"
convert -size ${SIZEX}x${SIZEY} xc:none -fill gray -draw "${RECT}" $THUMBRECT
 
# Create a mask with nice lights
convert $THUMBRECT -bordercolor None -border 1x1 \
          -alpha Extract -blur 0x5  -shade 80x8 -alpha On \
          -background gray50 -alpha background -auto-level \
          -function polynomial  3.5,-4.05,2.05,0.3 \
          \( +clone -alpha extract  -blur 0x3 \) \
          -channel RGB -compose multiply -composite \
          +channel +compose -chop 1x1 \
          $THUMBMASK

The next script requires the previous mask image (thumbnail_mask.png) and generates a mask for an existing image (the image is the first argument of the script).
This picture:

Will be combined with the mask and become this image:

#!/bin/bash
# Author:    kdguntu@gmail.com
# Copyright: (c) 2011 GNU Copyright v3
# Function:  create thumbnail
# Requires:  imagemagick (convert)
#            thumbnail mask (see previous script to generate a mask)
# Arguments: First argument: inputfile
#            Second argument (optional): outputfile
 
THUMBMASK="thumbnail_mask.png"
SIZE="96x96"
THUMBOUT="$2"
THUMBPREFIX="thumb_"
THUMBSUFFIX=".png"
 
if ! [ -f $1 ]; then
  echo 1>&2 "Usage: $0 inputfile [outputfile]"; exit 1
fi
 
BIGIN="$1"; BIGINFILE="`basename $1`"
 
if [ -z "$2" ]; then
 THUMBOUT="$THUMBPREFIX$BIGINFILE$THUMBSUFFIX"
fi
 
convert   -define jpeg:size=200x200 $BIGIN  -thumbnail $SIZE^ \
          -gravity center -extent $SIZE $BIGIN.temp.png
 
convert $BIGIN.temp.png $THUMBMASK \
          \( -clone 0,1 -compose Hardlight -composite \) \
          -delete 0 -compose In -composite \
          $THUMBOUT
 
rm -f $BIGIN.temp.png

Both scripts can be combined and used with find to process a lot of images:

./create_mask.sh
find . -iname "*.jpg" -exec ./create_thumb_with_mask.sh {} \;
This entry was posted in Uncategorized. Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *