如何使用Python截掉PNG图片的空白边框区域并将其缩小到最小尺寸?
注意:边框大小不是固定值,可能因图片而异。
最佳答案
im.getbbox() => 4-tuple or None
Calculates the bounding box of the non-zero regions in the image. The bounding box is returned as a 4-tuple defining the left, upper, right, and lower pixel coordinate. If the image is completely empty, this method returns None.
我试过的代码示例,我已经用 bmp 测试过,但它也应该适用于 png。
import Image
im = Image.open("test.bmp")
im.size # (364, 471)
im.getbbox() # (64, 89, 278, 267)
im2 = im.crop(im.getbbox())
im2.size # (214, 178)
im2.save("test2.bmp")
关于python - 将 PNG 图像裁剪为其最小尺寸,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1905421/