如何使用ImageMagick转换工具修剪png,并保持与原始中心相同的距离?

时间:2018-05-08 08:28:44

标签: imagemagick imagemagick-convert

我有一个png在板上有一些透明度,我想修剪透明度,并保持每个东西与原始中心的距离相同。 我使用ImageMagick的转换工具 例如,

原始的。

The original

我想要的结果。

The correct result

我与convert original.png -trim +repage out.png

一起使用的结果

The wrong result

原始文件在这里。

enter image description here

3 个答案:

答案 0 :(得分:1)

我可以在Windows 10的ubuntu bash shell中使用IM 6.8.9-9来运行此命令来获得您描述的结果......

convert test.png -write mpr:input -duplicate 3 \
   -distort SRT %[fx:t*90] -background none -layers merge -trim \
   -set option:pagewide %[w] -set option:pagehigh %[h] -set option:pageoffset %[fx:page.x] \
   -delete 0 mpr:input -set page %[pagewide]x%[pagehigh]-%[pageoffset]-%[pageoffset] \
   -coalesce result.png

读取输入,将一个副本存储在临时存储器中,再制作3个副本,并将它们旋转0度,90度,180度和270度。然后它展平它们并使用其中的修剪结果来计算最终图像尺寸和偏移。接下来,它删除修改后的图像,将原始图像从临时存储器中取回,并通过将这些计算的页面设置应用于原始图像来创建输出。

这适用于方形图像。如果宽度和高度尺寸不相同,计算将变得更加复杂。

编辑添加:我在下面添加的命令应该采用任何输入图像并尽可能透明地修剪,同时保持原始中心像素位于结果的中心...

convert test.png -write mpr:input -background none \
   -rotate 180 mpr:input -composite -set page %[@] \
   -set option:bounds %[fx:page.width]x%[fx:page.height]-%[fx:page.x]-%[fx:page.y] \
   -delete 0 mpr:input -set page %[bounds] -coalesce result.png

它从顶部和底部修剪相同的数量,并从左侧和右侧修剪相同的数量,但顶部/底部数量可能与左/右数量不同。

答案 1 :(得分:0)

我建议运行-trim操作,并在临时变量中记录页面偏移量。然后通过应用-extent来计算最终图像。

例如......

# Capture original size.
read -r WIDTH HEIGHT <<<$(convert lBW9I.png -format '%[fx:page.width] %[fx:page.height]' info:-)
# Capture offset after applying trim.
read -r LEFT TOP <<<$(convert lBW9I.png -trim -format '%[fx:page.x] %[fx:page.y]' info:-)
# Use the local variables to calculate finial paging...
convert lBW9I.png \
        -background transparent \
        -extent "$(($WIDTH - $LEFT * 2))x$(($HEIGHT - $TOP * 2))+$LEFT+$TOP" \
        output.png

With original center

答案 2 :(得分:0)

感谢所有人。你的所有命令都有效,因为我的一些图片不是方形的,所以我终于使用这个命令从imagemagick论坛学习,它可以在Windows 7中使用IM V7.0.7-31

magick lBW9I.png ( +clone -trim -set option:NTRIM %[fx:min(min(page.x,page.width-w-page.x),min(page.y,page.height-h-page.y)*page.width/page.height)]x%[fx:min(min(page.y,page.height-h-page.y),min(page.x,page.width-w-page.x)*page.height/page.width)] +delete ) -shave %[NTRIM] x.png
相关问题