Imagemagick – Tips and Tricks No. 3

Add a Label to a set of images

First bash script coming up, I’ll explain the best I can: I have a set of images that are all square: w=h. I want to create an image of each image with a label on top,using the filename as the label (without the extension), with a white background, ending up with a 200×250 image with the same name as the original but with “-tab” added. IM will by default squeeze the font size so that the text label always fits.

#!/bin/bash

FILES=”*.png”

extbit=”-tab.png”

for F in $FILES

do
newname=$(basename “$F” .png)
fullname=$newname$extbit

convert “$F” -resize 200×200^! -background White -border 0x1 -bordercolor Black\
-gravity center -size 200×50 -font DejaVu-Sans-Bold label:$newname +swap\
-gravity north -append -border 1×1 -bordercolor Black -resize 200×250^!\
PNG24:$fullname

done

Output:

Green-tab

Orange-tab

Red-tab

Then either use individually or you could put together in a grid using Montage

Take an Image of any Aspect Ratio and place a Square White canvas around it

#!/bin/bash
#sqcan.sh
pic=$1
convert $pic -trim $pic
width=$(identify -format "%w" $pic)
height=$(identify -format "%h" $pic)
new_dim=$((width > height ? width+10 : height+10))
convert $pic -gravity center -extent "${new_dim}x${new_dim}" sc-$pic

Usage:

On the command line, in the directory with the image you want to add the canvas:

./sqcan.sh mg.jpg

Output

sc-mg

Make a square image from an image of any dimensions based upon the centre but using either full width or height

#!/bin/bash
#sqit.sh

convert -define jpeg:size=800x800 "$1" -thumbnail 400x400^ -gravity center -extent 400x400 sq-$1

Usage:

./sqit.sh mg.jpg

Output:

sq-mg

Leave a Reply

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