I have been hitting my head against the wall for a while with this, so maybe someone out there can help.
I'm using PIL to open a PNG with transparent background and some random black scribbles, and trying to put it on top of another PNG (with no transparency), then save it to a third file.
It comes out all black at the end, which is irritating, because I didn't tell it to be black.
I've tested this with multiple proposed fixes from other posts. The image opens in RGBA format, and it's still messed up.
Also, this program is supposed to deal with all sorts of file formats, which is why I'm using PIL. Ironic that the first format I tried is all screwy.
Any help would be appreciated. Here's the code:
from PIL import Image img = Image.open(basefile) layer = Image.open(layerfile) # this file is the transparent one print layer.mode # RGBA img.paste(layer, (xoff, yoff)) # xoff and yoff are 0 in my tests img.save(outfile)
Because when you download the PNG from the Previewer Window, the PNG LOSES the transparency information.
You can confirm that your file has been converted by pressing edit on the image on your device from the Apple Photos app (see below) If the background turns black then transparency has been maintained. If it stays white then the image has been converted to a JPG file.
You may be used to saving image files for web use as JPEGs, but JPEGs don't support transparent backgrounds. So, instead, you'll need to use a format such as GIF, TIF or, ideally, PNG. The PNG file is small enough for use online but still delivers high quality with transparency as well.
I think what you want to use is the paste mask argument. see the docs, (scroll down to paste
)
from PIL import Image img = Image.open(basefile) layer = Image.open(layerfile) # this file is the transparent one print layer.mode # RGBA img.paste(layer, (xoff, yoff), mask=layer) # the transparancy layer will be used as the mask img.save(outfile)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With