"ValueError: bad transparency mask" when pasting one image onto another with Python Imaging Library?

Question:

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"

What did I do wrong?

Asked By: plv

||

Answers:

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”).

You might try this:

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")
Answered By: Jeremy