填充具有透明度的图像,因此它的大小在两个方向上都是 300 的倍数 (ImageMagick 7)

Pad image with transparency so it is sized as multiplies of 300 in both directions (ImageMagick 7)

我的问题有点类似于这个问题:,但我不想对图像进行平方,只是将其向上缩放到每个方向最接近的乘数 300,并填充透明度。原始图像应在此新填充内居中。

示例:

输入图像:宽度 1004 像素,高度 250 像素 输出图像:宽度 1200 像素,高度 300 像素,原始图像居中。

如果重要的话,我正在尝试通过 Mac 终端实现这一点。

我已经设法完成上面 link 中的转换,以及其他必要转换的列表,但是我很难使用提供的 IM 变量,以及数学用于舍入浮点数的函数,以及 distort:viewport 函数,这似乎是我应该使用的函数?

我想你想要这个:

magick -gravity center start.png -background yellow -extent "%[fx:int((w+299)/300)*300]x%[fx:int((h+299)/300)*300]" result.png

因此,如果我们从您的 1004x250 尺寸开始:

你会得到这个:

显然,您希望将 yellow 替换为 none 以获得透明边框,但我希望在 Whosebug 上可见该范围。

如果您使用不同的倍数,我公式中的 299 就是 multiple - 1。因此,您可以将答案重写为:

MULT=300
magick -gravity center start.png -background yellow -extent "%[fx:int((w+$MULT-1)/$MULT)*$MULT]x%[fx:int((h+$MULT-1)/$MULT)*$MULT]" result.png

或者,如果您不喜欢丑陋的 %[fx:...] 表达式,您可以在 shell:

中完成所有数学运算
# Establish the multiple
MULT=300
# Get existing image width and height
read w h < <(magick -format "%w %h" start.png info:)
# Calculate new width and new height
((NW=((w+MULT-1)/MULT)*MULT))
((NH=((h+MULT-1)/MULT)*MULT))
magick -gravity center start.png -background yellow -extent "$NWx$NH" result.png