MikeTheReader
MikeTheReader

Reputation: 4190

PIL paste image with alpha appears faded

I am working with multiple images that I would like to stack on top of each other to create a single image. However, in working with them, I'm noticing that if the image already has transparency (alpha != 255), that part of the image appears faded. If there is no transparency, all is good.

I saved one of the images I was working with to a PNG and created a small bit of code that duplicates the problem. Essentially, I'm creating a new image with a transparent background and then pasting the image on top:

from PIL import Image

img=Image.new('RGBA', (946,627), (0,0,0,0))
overlayImage = Image.open('drawing.png')
img.paste(overlayImage, (0,0), overlayImage)
img.save('drawing-pasted.png')

When this completes, drawing-pasted.png looks like this:

drawing-pasted.png

But the original drawing (drawing.png) looked like this:

drawing.png

(Images cropped manually to show detail.) The original image circles fill color has an alpha value of 179.

Has anyone else encountered this, and what am I doing wrong?

Thanks much.

Upvotes: 2

Views: 3029

Answers (2)

iqbal
iqbal

Reputation: 97

image = Image.open(file_path)
img1 = Image.open(file_path)
cords = (233.22)
image.paste(img1, cords, mask=img1)
image.save(path_where_you_want_to_save_final_image)

simply use this block of code

Upvotes: 1

Steve Barnes
Steve Barnes

Reputation: 28405

The background you are creating is black and fully transparent the original is blue but with an alpha of 179 so you have 2 pixels (0,0,0,0) and (0,0,255,179) assuming 100% blue - since you are pasting the image in it will be over the background so will use the alpha of the new image allowing (255-179)/255 or about 30% black. (N.B. The alpha of the background makes no difference since it is behind the new image)

You could use overlayImage.putalpha to set the alpha to 255 start from your image rather than a black background.

Upvotes: 0

Related Questions