I have a PNG image with transparent background and I want to resize it to another image, but with a white background instead of a transparent one. How can I do that with PIL?
Here is my code:
basewidth = 200
img = Image.open("old.png")
wpercent = (basewidth/float(img.size[0]))
hsize = int((float(img.size[1]) * float(wpercent)))
img = img.resize((basewidth, hsize), PIL.Image.ANTIALIAS)hsize = int((float(img.size[1]) * float(wpercent)))
img.save("new.png")
You can check whether the alpha channel is set to less than 255 on each pixel (which means that it is not opaque) and then set it to white and opaque.
It might not be an ideal solution if you have transparency in other parts of your image, besides the background.