Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Overlaying an image's filename using ImageMagick (or similar)

I know ImageMagick's annotate command can superimpose some text over an image, but can it use the image's filename as this text? I would've assumed so, but can't seem to find direct documentation that confirms this.

No doubt some combination of parameters can manage this, or is there a better way of doing this in a script?

like image 759
Lunatik Avatar asked Nov 05 '10 13:11

Lunatik


People also ask

How to resize image using ImageMagick?

To resize an image to specific dimensions, use the convert command with an input file, the -resize parameter, your preferred dimensions, and an output filename: convert original. png -resize 100x100 new. png.

What is the use of ImageMagick?

ImageMagick is open-source software that is used to create and edit images. It is used to edit bitmap images of 200 formats. It's available for free software that can be used as source code with a ready-run binary distribution feature.

What does append images mean?

Basically it means that you don't have to worry about clipping, offset, or other aspects when merging layer images together. All images will be merged relative to each others location.


1 Answers

Eric L.'s answer is correct -- +1 from me for it! -- but -annotate doesn't give you much control over the appearance of the text.

If you look for prettyness, then rather go for something that uses -composite. You can use an IM command to construct an overlay image first (which uses a semi-transparent background) and then overlay it over the original image.

Here is an example how to do it with -composite instead of -annotate, using a scripted approach that processes every PNG file in the current directory. This one even automatically adapts the font size and fits it into the available "width * 90%" -- it is a Bash script (see comments for Win equivalent):

for img in *.png; do

   width=$(identify -format %W ${img})
   width=$(( ${width} * 9 / 10 ))

   convert                  \
     -background '#0008'    \
     -gravity center        \
     -fill white            \
     -size ${width}x100     \
      caption:"${img}"      \
      "${img}"              \
     +swap                  \
     -gravity south         \
     -composite             \
      "with-caption-${img}"

done

An example illustration for one original and the respective output are below:

original image image with caption!

Here is a command that uses -annotate, trying to set a few things beyond the default parameters:

for img in so#12231624-right.png; do

   convert                   \
      "${img}"               \
     -fill red               \
     -undercolor '#0008'     \
     -pointsize 24           \
     -gravity south          \
     -annotate +0+5 "${img}" \
      "with-annotate-${img}"

done

original image resulting image

like image 61
Kurt Pfeifle Avatar answered Oct 10 '22 22:10

Kurt Pfeifle