Python将图片背景透明化 您所在的位置:网站首页 pymol怎么导出图片背景为透明 Python将图片背景透明化

Python将图片背景透明化

2024-05-11 17:56| 来源: 网络整理| 查看: 265

def transparent_background(path): try: img = Image.open(path) img = img.convert("RGBA") # 转换获取信息 pixdata = img.load() color_no = get_convert_middle(path) + 30 # 抠图的容错值 for y in range(img.size[1]): for x in range(img.size[0]): if pixdata[x, y][0] > color_no and pixdata[x, y][1] > color_no and pixdata[x, y][2] > color_no and \ pixdata[x, y][3] > color_no: pixdata[x, y] = (255, 255, 255, 0) if not path.endswith('png'): os.remove(path) replace_path_list = path.split('.') replace_path_list = replace_path_list[:-1] path = '.'.join(replace_path_list) + '.png' img.save(path) img.close() except Exception as e: return Falsereturn path def get_convert_middle(img_path): I = Image.open(img_path) L = I.convert('L') im = numpy.array(L) im4 = 255.0 * (im / 255.0) ** 2 # 对图像的像素值求平方后得到的图像 middle = (int(im4.min()) + int(im4.max())) / 2 return middle

# 调用 transparent_background, 传入图片路径, 该方法把图片修改后替换了源文件



【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有