I\'m prototyping an image processor in Python 2.7 using PIL1.1.7 and I would like all images to end up in JPG. Input file types will include tiff,gif,png both with transpar
The following works for me on this image
f, e = os.path.splitext(infile)
print infile
outfile = f + ".jpg"
if infile != outfile:
im = Image.open(infile)
im.convert('RGB').save(outfile, 'JPEG')
image=Image.open('file.png')
non_transparent=Image.new('RGBA',image.size,(255,255,255,255))
non_transparent.paste(image,(0,0),image)
The key is to make the mask (for the paste) the image itself.
This should work on those images that have "soft edges" (where the alpha transparency is set to not be 0 or 255)
Make your background RGB, not RGBA. And remove the later conversion of the background to RGB, of course, since it's already in that mode. This worked for me with a test image I created:
from PIL import Image
im = Image.open(r"C:\jk.png")
bg = Image.new("RGB", im.size, (255,255,255))
bg.paste(im,im)
bg.save(r"C:\jk2.jpg")