How to cut off the blank border area of a PNG image and shrink it to its minimum size using Python?
+1
A:
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.
Frank Krueger
2009-12-15 06:05:42
+7
A:
PIL's getbbox is working for me
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.
Code Sample that I tried, I have tested with bmp, but it should work for png too.
>>> 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")
S.Mark
2009-12-15 06:09:32