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()
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()
© 2024 OneMinuteCode. All rights reserved.