Python 如何在枕头中用颜色代替透明
我需要用白色替换png图像的透明层。我试过这个Python 如何在枕头中用颜色代替透明,python,python-3.x,image,pillow,Python,Python 3.x,Image,Pillow,我需要用白色替换png图像的透明层。我试过这个 from PIL import Image image = Image.open('test.png') new_image = image.convert('RGB', colors=255) new_image.save('test.jpg', quality=75) 但是透明层变黑了。有人能帮我吗?将图像粘贴在完全白色的rgba背景上,然后将其转换为jpeg from PIL import Image image = Image.open
from PIL import Image
image = Image.open('test.png')
new_image = image.convert('RGB', colors=255)
new_image.save('test.jpg', quality=75)
但是透明层变黑了。有人能帮我吗?将图像粘贴在完全白色的rgba背景上,然后将其转换为jpeg
from PIL import Image
image = Image.open('test.png')
new_image = Image.new("RGBA", image.size, "WHITE") # Create a white rgba background
new_image.paste(image, (0, 0), image) # Paste the image on the background. Go to the links given below for details.
new_image.convert('RGB').save('test.jpg', "JPEG") # Save as JPEG
查看并。基于@Alperen的答案,如果您想消除透明度,可以将图像粘贴到新的非透明(RGB)图像上:
透明层没有颜色-它只指示像素的透明程度。输入尺寸一定有错误,因为它抛出了不好的透明遮罩,上面使用RGBA通道的解决方案是有效的
from PIL import Image
input = Image.open('image.png')
image = Image.new("RGB", input.size, "WHITE")
image.paste(input, (0, 0), input)
image.save('image_out.png')