将 PNG 图像裁剪为其最小尺寸
如何使用Python剪切PNG图像的空白边框区域并将其缩小到最小尺寸?
注意:边框大小不是固定值,但每个图像可能会有所不同。
How to cut off the blank border area of a PNG image and shrink it to its minimum size using Python?
NB: The border size is not a fixed value, but may vary per image.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
PIL 的 getbbox 为我工作
我尝试过的代码示例,我已经使用 bmp 进行了测试,但它也应该适用于 png。
PIL's getbbox is working for me
Code Sample that I tried, I have tested with bmp, but it should work for png too.
这是现成的解决方案:
示例输入(下载链接,如果您想尝试):
输出:
Here is ready-to-use solution:
Example input (download link if you want to try):
Output:
我今天也遇到了同样的问题。这是我裁剪透明边框的解决方案。只需将此脚本与批处理 .png 文件一起放入您的文件夹中即可:
I had the same problem today. Here is my solution to crop the transparent borders. Just throw this script in your folder with your batch .png files:
https://gist.github.com/3141140
https://gist.github.com/3141140
我认为有必要补充@Frank Krueger的答案。他提出了一个很好的观点,但它没有包括如何正确地从图像中裁剪出额外的边框颜色。我发现在这里。具体来说,我发现这很有用:
I think it's necessary to supplement @Frank Krueger's answer. He makes a good point, but it doesn't include how to properly crop extra border color out of an image. I found that here. Specifically, I found this useful:
您可以使用 PIL 查找完全由您的图像组成的行和列边框颜色。
使用此信息,您可以轻松确定镶嵌图像的范围。
然后,PIL 将再次允许您裁剪图像以删除边框。
You can use PIL to find rows and cols of your image that are made up purely of your border color.
Using this information, you can easily determine the extents of the inlaid image.
PIL again will then allow you to crop the image to remove the border.
在编写 Blender 脚本时(无法使用 PIL),其他答案对我不起作用,所以也许其他人会发现这很有用。
The other answers did not work for me while writing a Blender script (cannot use PIL), so maybe someone else will find this useful.