PIL Convert PNG or GIF with Transparency to JPG without

前端 未结 3 953
既然无缘
既然无缘 2020-11-28 13:35

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

相关标签:
3条回答
  • 2020-11-28 13:57

    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')
    
    0 讨论(0)
  • 2020-11-28 13:59
    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)

    0 讨论(0)
  • 2020-11-28 14:14

    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")
    
    0 讨论(0)
提交回复
热议问题