Category Archives: imagemagick

Experimenting with adding labels to photos

A-‘s gotten really interested in letters lately, so I’m looking for ways to add more personally relevant text and images to her life. She often flips through the 4″x6″ photos I got printed. Since they’re photos, they’re sturdier and can stand up to a little bending. I wanted to experiment with printing more pictures and labeling them with text, building on the script I used to label our toy storage bins. Here’s a sample:

2018-06-14-16-12-14 ★★★ A- gently lifted the flaps in the book. 🏷fine-motor

(Name still elided in the sample until I figure out what we want to do with names and stuff. I’ll use NAME_REPLACEMENTS to put her name into the printed ones, though, since kids tend to learn how to read their names first.)

I haven’t printed these out yet to see how legible they are, but a quick on-screen check looks promising.



# Add label from ImageDescription EXIF tag to bottom of photo

IFS=$(echo -en "\n\b")

# NAME_REPLACEMENTS is an environment variable that's a sed expression
# for any name replacements, like s/A-/Actual name goes here/g

for file in $*; do
  description=$(exiftool -s -s -s -$description_field "$file" \
     | sed "s/\"/\\\"/g;s/'/\\\'/g;$NAME_REPLACEMENTS")
  date=$(exiftool -s -s -s -DateTimeOriginal "$file" \
     | cut -c 1-10 | sed s/:/-/g)
  width=$(identify -format "%w" "$file")
  height=$(identify -format "%h" "$file")
  largest=$(( $width > $height ? $width : $height ))
  density=$(( $largest / $output_width_inches ))
  correct_height=$(( $output_height_inches * $density ))
  captionwidth=$(( $width - $border * 2 ))
  convert "$file" -density $density -units PixelsPerInch \
    -gravity North -extent ${width}x${correct_height} \
    -strip \( -undercolor white -background white \
    -fill black -font "$font" -bordercolor White \
    -gravity SouthWest -border $border -pointsize $pointsize \
    -size ${captionwidth}x  caption:"$date $description" \) \
    -composite "$destination/$file"
gwenview $destination

Here’s my current rename-based-on-exif, too. I modified it to use the ImageDescription or the UserComment field, and I switched to using Unicode stars and labels instead of # to minimize problems with exporting to HTML.


date="\${DateTimeOriginal;s/[ :]/-/g}"
rating="\${Rating;s/([1-5])/'★' x \$1/e}"
tags="\${Subject;s/^/🏷/;s/, / 🏷/g}"
field=FileName  # TestName for testing
exiftool -m -"$field<$date $rating \${ImageDescription} $tags.%e" \
         -"$field<$date $rating \${UserComment} $tags.%e" "[email protected]"

In order to upload my fancy-shmancy Unicode-filenamed files, I also had to convert my WordPress database from utf8 to utf8mb4. This upgrade plugin was very helpful.

Creating thumbnails with Imagemagick

Creating proper thumbnails in Photoshop was a mission and a half. I found a handy Imagemagick incantation for thumbnails. Here’s the one I’ll use to create 400×400 centered thumbnails for the photo gallery.

convert -size 400x400 sacha-20051201.jpg -thumbnail '400x400>' -bordercolor black -border 200 -gravity center -crop 400x400+0+0 +repage test.jpg

On Technorati: ,