简体   繁体   中英

How to remove extra transparent pixels from an image

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM