from PIL import Image
import numpy as np
def remove_alpha(img_path):
# Open image and ensure not palettised, make into Numpy array and select alpha channel
im = Image.open(img_path).convert('RGBA')
na = np.array(im)
alpha = na[:, :, 3]
# Find non-empty rows and columns
non_empty_rows = np.where(alpha.max(axis=1) > 0)[0]
non_empty_columns = np.where(alpha.max(axis=0) > 0)[0]
# Copy them to new image
opaque = na[non_empty_rows, :, :][:, non_empty_columns, :]
# Create new image with non-empty rows and columns
new_image = Image.fromarray(opaque)
# Save the new image
new_image.save(f'{tmp_img_name}.png')
remove_alpha(r'C:\Users\Administrator\Desktop\c.png')
删除图片透明区域
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。
推荐阅读更多精彩内容
- 最近项目中遇到这么一个需求,需要裁剪掉图片的透明区域。找了很久,最后确定,只能通过自己读取Bitmap的像素点来读...
- 注意:1.CGImageCreateWithImageInRect 使用的坐标都是像素点2.iOS使用的都是点坐标...
- 项目中遇到了截图指定区域图片的功能, 比如一张全车图, 用户可以在手机上自由的画圈, 画完后要截取到画圈区域的图片...