If you resize or make the transparent image transparent with pillow, the background will not be transparent.

Asked 2 years ago, Updated 2 years ago, 142 views

If you erase the thumbnail to the putalpha process, it will be transparent, but if you apply this process, the transparent part of the image will look black.
How do I maintain the transparency of the processed image?

 from PIL import Image

back=Image.open.convert('RGBA')
image=Image.open.convert('RGBA')

back.resize((1000,1000))
image.thumbnail(100,100))
image.putalpha(100)

image_clear=Image.new('RGBA', back.size, (255, 255, 255, 0))
back.paste(image,(50,50), image)
back=Image.alpha_composite(back, image_clear)

back.show()

python pillow

2022-09-29 21:25

1 Answers

This is probably because image after changing the transmittance is used as a mask for paste().
If you copy the data before changing the transmittance for mask and specify it as a mask of paste(), it will be fine.
You can do the following.

 from PIL import Image

back = Image.open('./bg.png') .convert('RGBA')
image=Image.open('./glogo.png') .convert('RGBA')


back.resize((1000,1000))
image.thumbnail(100,100))
mask=image.copy()## Copy the original image as a mask after resizing it
image.putalpha(100)

image_clear=Image.new('RGBA', back.size, (255, 255, 255, 0))
back.paste(image,(50,50),mask)## Specify the alpha channel for the original image without changing the transmittance as mask
back=Image.alpha_composite(back, image_clear)

back.show()


2022-09-29 21:25

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.