Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

get ValueError: bad transparency mask when trying to paste, image and mask are the same

import numpy as np

from PIL import Image



img_orig = Image.open("me.jpg")

# convert pic to 3-D array
array_orig = np.array(img_orig)

# create R image
array_r = np.copy(array_orig)
array_r[:, :, 1:3] = 0
img_r = Image.fromarray(array_r)

# create GB image
array_gb = np.copy(array_orig)
array_gb[:, :, 0] = 0
img_gb = Image.fromarray(array_gb)

canvas_r = Image.new("RGB", img_orig.size, color=(0,0,0))
canvas_r.paste(img_r, (5, 5), img_r) #error line

canvas_gb = Image.new("RGB", img_orig.size, color=(0,0,0))
canvas_gb.paste(img_gb, (0, 0), img_gb)

result_array = np.array(canvas_r) + np.array(canvas_gb)
result = Image.fromarray(result_array)
result.show()

The line of code gives error is given in the code. I don't quite understand why PIL gives transparency error for same image. I believe there is no need to specify mode because the mask and image are the same.

like image 845
Gabriel.S Avatar asked Oct 24 '25 18:10

Gabriel.S


1 Answers

I solved a similiar problem with:

Replace img_orig = Image.open("me.jpg")

with img_orig = Image.open("me.jpg").convert('RGBA')

And replace canvas_gb = Image.new("RGB", img_orig.size, color=(0,0,0))

with canvas_gb = Image.new("RGBA", img_orig.size, color=(0,0,0))

like image 185
natielle Avatar answered Oct 26 '25 11:10

natielle



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!