如何使用 ImageMagick 转换工具 trim 一个 png,并使所有东西与原始中心保持相同的距离?
How to trim a png with ImageMagick convert tool, and keep all things same distance to original center?
我在棋盘上有一个带有一些透明度的 png,我想要 trim 透明度,并使所有东西与原始中心的距离相同。
我使用 ImageMagick 的转换工具
例如,
原来的
我想要的结果
我使用 convert original.png -trim +repage out.png
的结果
原文件在这里
我会推荐 运行 -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
我可以通过在 ubuntu bash shell 10 Windows 运行 中使用 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 度。然后它将它们展平并使用 trim 结果来计算最终图像尺寸和偏移量。接下来,它会删除修改后的图像,将原始图像从临时内存中取回,并通过将这些计算出的页面设置应用于原始图像来创建输出。
这应该适用于方形图像。如果宽高尺寸不一样,计算起来会比较复杂。
编辑添加:我在下面添加的命令应该使任何输入图像和 trim 尽可能透明,同时将原始中心像素保持在结果的中心。 .
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
trim从上到下的量是一样的,从左到右的是trim是一样的,但是top/bottom的量可能不一样left/right 数量。
感谢大家。你所有的命令都有效,因为我的一些图片不是正方形的,所以我最终使用这个命令从 imagemagick 论坛学习,它适用于 windows 7 with 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
我在棋盘上有一个带有一些透明度的 png,我想要 trim 透明度,并使所有东西与原始中心的距离相同。 我使用 ImageMagick 的转换工具 例如,
原来的
我想要的结果
我使用 convert original.png -trim +repage out.png
原文件在这里
我会推荐 运行 -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
我可以通过在 ubuntu bash shell 10 Windows 运行 中使用 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 度。然后它将它们展平并使用 trim 结果来计算最终图像尺寸和偏移量。接下来,它会删除修改后的图像,将原始图像从临时内存中取回,并通过将这些计算出的页面设置应用于原始图像来创建输出。
这应该适用于方形图像。如果宽高尺寸不一样,计算起来会比较复杂。
编辑添加:我在下面添加的命令应该使任何输入图像和 trim 尽可能透明,同时将原始中心像素保持在结果的中心。 .
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
trim从上到下的量是一样的,从左到右的是trim是一样的,但是top/bottom的量可能不一样left/right 数量。
感谢大家。你所有的命令都有效,因为我的一些图片不是正方形的,所以我最终使用这个命令从 imagemagick 论坛学习,它适用于 windows 7 with 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