【问题标题】:Appending images using ImageMagick convert使用 ImageMagick convert 附加图像
【发布时间】:2023-04-02 05:21:01
【问题描述】:

所以我有一堆我想垂直附加的图像:

convert *.png -append output.png

但是我遇到了两个问题:

  1. 并非所有图像的宽度都相同。因此,宽度小于最宽图像的图像在其后有空白区域,因为它们是左对齐的。
  2. 图像之间没有间距。

如何将所有图像居中对齐并指定它们之间的间距?

【问题讨论】:

  • 你想要什么颜色的间距?还是应该是透明的?
  • 最好是透明的。
  • 您使用的是 Unix/Linux/OSX 还是 Windows?
  • 我在 Ubuntu 14.04 上工作。因此是 Linux。

标签: graphics bitmap imagemagick imagemagick-convert


【解决方案1】:

只需使用 ImageMagick 的 montage 实用程序。使用-gravity-extent 选项对齐图像,并使用-geometry 调整间距。

montage fishscales.png circles.png verticalsaw.png \
        -tile 1x -geometry +10+10 \
        -gravity Center -extent 120 \
        out.png

【讨论】:

  • 这看起来很简单,但给了我这个错误:蒙太奇:无法读取字体 `(null)' @error/annotate.c/RenderFreetype/1127。有什么想法吗?
  • 可能是您如何安装 freetype 或安装字体库的问题。你能告诉我们你是如何调用文本/注释的吗?
  • 它可以工作,但是之间的空间比到外部的空间(边距)宽 2 倍,因为它为每个图像增加了空间(边距)。
【解决方案2】:

我的方法是如下的 shell 脚本:

1. Run a loop over all your images and find the width of the widest (using ImageMagick `identify`) - say 8 to 10 lines of shell script

2. Create a transparent "spacer image" the same width as the widest image and the height you want for vertically spacing images - one line of shell script

3. Run a loop over all your images first adding the transparent "spacer" to the bottom of the existing output image then compositing images that are narrower than your widest image onto a transparent background the width of your widest image - then appending that to the output image - maybe 15 lines of shell script.

这是三张图片的输出:

红色=80px 宽

绿色=180 像素宽

蓝色=190px 宽

这种方法对你有用吗?我可能会在一两天内编写代码!

这就是我的意思:

#!/bin/bash
# User-editable vertical spacing between images
SPACING=10

function centre() {
   echo DEBUG: Centering $1
   TEMP=$$tmp.png
   w=$(convert "$1" -ping -format "%w" info:)
   h=$(convert "$1" -ping -format "%h" info:)
   convert -size ${MAXW}x${h} xc:"rgba(0,0,0,0)" PNG32:$TEMP
   composite -resize '1x1<' -gravity center "$1" $TEMP $TEMP
   if [ $2 -eq 0 ]; then
      mv $TEMP output.png
   else
      convert output.png $TEMP -append output.png
      rm $TEMP 
   fi
}

# Step 1 - determine widest image and save width in MAXW
MAXW=0
for i in *.jpg; do
   w=$(convert "$i" -ping -format "%w" info:)
   [[ $w -gt $MAXW ]] && MAXW=$w
   echo DEBUG: Image $i width is $w
done
echo DEBUG: Widest image is $MAXW

# Step 2 - Create transparent spacer image, with width MAXW
convert -size ${MAXW}x${SPACING} xc:"rgba(0,0,0,0)" PNG32:spacer.png

# Step 3 - Build output image
n=0
for i in *.jpg; do

   # Add a vertical spacer if not first image
   [[ $n -ne 0 ]] && convert output.png spacer.png -append output.png

   # Centre image horizontally and append to output
   centre "$i" $n

   ((n++))
done

另一个完全不同的选择是添加 N 个图像的所有高度,并创建一个透明画布,其中最宽图像的宽度和所有图像的高度加起来加上 (N-1)* 垂直间距。然后在正确的位置将图像叠加到画布上——这涉及更多的数学运算,更少的图像处理和再处理,这可能意味着这种方法的质量损失低于我建议的方法。我会用 PerlMagick 做这种方法,因为它比 bash 更适合数学。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-06-05
    • 2011-09-28
    • 1970-01-01
    • 1970-01-01
    • 2011-10-13
    • 1970-01-01
    • 2018-05-05
    相关资源
    最近更新 更多