简体   繁体   English

如何从图像中删除额外的透明像素

[英]How to remove extra transparent pixels from an image

I have a series of transparent logos.我有一系列透明标志。 I paste them on a canvas using PIL.我使用 PIL 将它们粘贴到 canvas 上。 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.这个答案中,您可以计算非零区域(透明/alpha)的边界框,然后以编程方式对其进行裁剪。

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")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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