I have a series of transparent logos. I paste them on a canvas using PIL. Some of these logos have extra transparent pixels that make the bounding box too wide, like this:
However, I need these logos and the bounding boxes to be like this:
How can I remove these extra, unnecessary transparent pixels so the bounding box wraps the logo properly?
Here are some of the logos:
From this answer , you can calculate the bounding box of the non-zero regions (transparent/alpha) and then programmatically crop it.
Snippet from answer:
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")
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.