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

后端 未结 4 1611
悲哀的现实
悲哀的现实 2021-02-05 12:58

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\

相关标签:
4条回答
  • 2021-02-05 13:20

    It's a very old entry but I find it each time I search for this topic, and it doesn't work (for me at least). Here something that works for me:

    convert input.jpg -gravity South -annotate 0 '%f' output.jpg
    

    Hope this helps someone...

    0 讨论(0)
  • 2021-02-05 13:22

    You can also use mogrify to add text to bunch of images at once.

    mogrify -gravity South -annotate 0 '%f' -pointsize 24 -fill white  *.png
    

    This will overwrite existing images, so make sure you have a backup before you execute this.

    0 讨论(0)
  • 2021-02-05 13:25

    Building on Steve Czetty's solution, it looks like you can set the text size and color of the annotation, using -pointsize and -fill, respectively.

    Here's an example:

    convert input.jpg -gravity south -pointsize 24 -fill yellow -annotate 0 '%f' output.jpg
    

    Obviously, you can change the text size from 24 points to something else, as well the color, from 'yellow' to some other color, as per your preference.

    0 讨论(0)
  • 2021-02-05 13:36

    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

    0 讨论(0)
提交回复
热议问题