PIL smudges image when I overlay another image on it
I'm using the following code to overlay an image onto another image using PIL
path = "/home/blah.png"
background = Image.open(path)
product = Image.open("/home/51tmBaivYhL._SL75_.jpg")
background.paste(product, (20,40))
background.save("/home/newImage.jpg")
These are the images I'm using as the background http://i.imgur.com/bwPFT.p开发者_StackOverflow社区ng , product http://i.imgur.com/Ig6qV.jpg and this is what the result is showing up as http://i.imgur.com/JfGoS.jpg
Would appreciate any help I can get.
Thanks.
EDIT: Also, when I try background.paste(product, (20,40), product)
, I get the error "ValueError: bad transparency mask"
The problem has nothing to do with PIL. The problem is the background is a .png, and you are composing it, and then outputting it as a .jpg.
.png is lossless, very good for synthetic images like your background. .jpg is lossy, very good for natural images like photographs. When you output your background as .jpg, it will contain compression artifacts, what you call "smudges".
Output your final image as a .png. It will be more bytes than a .jpg, but will look better.
You're opening a background PNG image with transparency and saving the result as a .JPG without transparency. The colors in the transparent area are probably leftovers from the image editor you used to create the PNG.
精彩评论