“ValueError:坏的透明掩码"使用 Python Imaging Library 将一个图像粘贴到另一个图像时? [英] "ValueError: bad transparency mask" when pasting one image onto another with Python Imaging Library?

查看:60
本文介绍了“ValueError:坏的透明掩码"使用 Python Imaging Library 将一个图像粘贴到另一个图像时?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 Python Imaging Library 将图像粘贴到背景上,如下所示:

I'm trying to paste an image onto a backgorund with Python Imaging Library like this:

card = Image.new("RGB", (220, 220), (255, 255, 255))
img = Image.open("/Users/paulvorobyev/test.png")

...

x, y = img.size
card.paste(img, (0, 0, x, y), img)

card.save("test.png")

当我运行这段代码时,我得到:

When I run this code, I get:

 "ValueError: bad transparency mask"

我做错了什么?

推荐答案

这里的游戏晚了,但我刚刚遇到了同样的问题.经过一些谷歌搜索,我能够通过确保使用的所有图像都是相同的模式(特别是RGBA")来让我的面具工作.

Late to the game here, but I just ran into the same issue. After some googling I was able to get my mask to work by making sure all of the images being used were the same mode (specifically "RGBA").

你可以试试这个:

card = Image.new("RGBA", (220, 220), (255, 255, 255))
img = Image.open("/Users/paulvorobyev/test.png").convert("RGBA")
x, y = img.size
card.paste(img, (0, 0, x, y), img)
card.save("test.png", format="png")

这篇关于“ValueError:坏的透明掩码"使用 Python Imaging Library 将一个图像粘贴到另一个图像时?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆