Is there a better way?

Questions and postings pertaining to the usage of ImageMagick regardless of the interface. This includes the command-line utilities, as well as the C and C++ APIs. Usage questions are like "How do I use ImageMagick to create drop shadows?".
Post Reply
dkw
Posts: 3
Joined: 2011-09-25T19:03:19-07:00
Authentication code: 8675308

Is there a better way?

Post by dkw »

So I've written a small shell script to batch caption/title a directory of images with a 20px bar on the top and include copyright info in a 20px bar on the bottom. What I'm wondering is if there is a better way to do it, or if my script can be leaner or more efficient. This was my first real attempt at using ImageMagick to work with my files in a batched manner, so I'm quite pleased that I got everything to do what I wanted - but now I want it better. Anyone able to offer suggestions to improve this? Or should I just leave it alone since it already works...

My script:

Code: Select all

cat ~/directory/filelist.txt |
while read filename text
do
# path to images
cpath="/home/user/path/to/images/";
# file extension
pext=".png";
# determines image width
width=`identify -format %w "${filename}${pext}"`;
# font to use for captioning
font="/usr/share/fonts/truetype/ttf-larabie-deco/cranberr.ttf";
#font size
pointsize="18";
# height of top/bottom caption bars
height="20";
# header text position
toppos="NorthWest";
# footer text position
botpos="SouthEast";
# static footer text to write
footer="Copyright 2011 Me";

# write the header caption from text file
convert -background black -font "${font}" -pointsize "${pointsize}" -fill white -gravity "${toppos}" -size "${width}x${height}" caption:" ${filename}: ${text}" ${cpath}${filename}${pext} +swap -gravity "${toppos}" -composite ${cpath}${filename}${pext}

#write the footer caption
convert -background black -font "${font}" -pointsize "${pointsize}" -fill white -gravity "${botpos}" -size "${width}x${height}" caption:"${footer} " ${cpath}${filename}${pext} +swap -gravity "${botpos}" -composite ${cpath}${filename}${pext}

done
The format of filelist.txt that is read in:

Code: Select all

20110924	Hello World
20110925	Goodbye World
20110926	etc, etc.
Sample of the output: http://postimage.org/image/2atmlbbxg/
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Is there a better way?

Post by fmw42 »

I believe you can put two -annotate commands in the same command line with different -gravity settings and write right on the image. But the text won't auto wrap like caption:, but your example does not show any need for wrapping.

convert inputimage .... -gravity north -annotate +0+0 "some text" -gravity south -annotate +0+0 "some other text" resultimage

see
http://www.imagemagick.org/Usage/text/#annotate
dkw
Posts: 3
Joined: 2011-09-25T19:03:19-07:00
Authentication code: 8675308

Re: Is there a better way?

Post by dkw »

Thanks fmw42, that seems to do what I'd like it to - I'll need to work with it a bit more tonight and see if I can get it adjusted some. Initially I'm seeing it create a new image instead of writing on top of the original, which is great for preserving data but undesirable behavior if it works correctly. My original script will write over the top of existing text with a new black box and white text, effectively erasing it - while the version with annotate just writes the white text on top of the current text.

Reading through the notes on annotate now and trying to see if I can duplicate my previous results - thanks for pointing me at a different way to do this, once I figure it out I can strip a couple lines out of my script ;) and will have learned a bit more too. Much appreciated.
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Is there a better way?

Post by anthony »

There is a IM examples section that is devoted to image annotation.
http://www.imagemagick.org/Usage/annotating/

The biggest problem is making sure the overlaid test remains visible.

Also while PNG is a non-lossy format, I still recommend merging multiple into a single command if possible.

Also use the shell link continuation techniques to divide up the operations to make it easier to read, follow and modify. The examples do this in just about every example.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
dkw
Posts: 3
Joined: 2011-09-25T19:03:19-07:00
Authentication code: 8675308

Re: Is there a better way?

Post by dkw »

Thank you as well for the help and advice anthony, it is appreciated!
Post Reply