使用ImageMagick(或类似文件)覆盖图像的文件名

问题描述 投票:12回答:4

我知道ImageMagick的annotate命令可以在图像上叠加一些文字,但是它可以使用图像的文件名作为这个文本吗?我会这样认为,但似乎无法找到确认这一点的直接文件。

毫无疑问,某些参数组合可以管理这个,或者在脚本中有更好的方法吗?

image-processing imagemagick filenames
4个回答
12
投票

Eric L.的回答是正确的 - 来自我的+1! - 但-annotate并没有给你很多控制文本的外观。

如果你寻找漂亮,那就去寻找使用-composite的东西。您可以使用IM命令首先构建叠加图像(使用半透明背景),然后将其覆盖在原始图像上。

下面是一个使用-composite而不是-annotate的示例,使用脚本方法处理当前目录中的每个PNG文件。这个甚至会自动调整字体大小并使其适合可用的“宽度* 90%” - 它是一个Bash脚本(请参阅Win等效的注释):

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

下面是一个原件和相应输出的示例说明:

!

这是一个使用-annotate的命令,试图设置一些超出默认参数的东西:

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


18
投票

这是一个非常古老的条目,但我每次搜索这个主题时都会发现它,但它不起作用(至少对我而言)。这里有一些对我有用的东西:

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

希望这有助于某人......


1
投票

您还可以使用mogrify一次向一堆图像添加文本。

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

这将覆盖现有图像,因此请确保在执行此操作之前进行备份。


0
投票

在Steve Czetty的解决方案的基础上,看起来您可以分别使用-pointsize和-fill设置注释的文本大小和颜色。

这是一个例子:

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

显然,您可以根据您的喜好将文本大小从24点更改为其他颜色,以及从“黄色”到其他颜色的颜色。

© www.soinside.com 2019 - 2024. All rights reserved.