PIL - Convert GIF Frames to JPG

后端 未结 3 1186
无人共我
无人共我 2020-12-03 05:13

I tried to convert an gif to single images with Python Image Library, but it results in weird frames

The Input gif is:

Source Image http://longcat.de/gif_exa

相关标签:
3条回答
  • 2020-12-03 05:58

    When viewing an image on an image viewer, even when transparency is set to zero, it tends to display the image as black. One way to be sure that your image is truly transparent is to merge it over another. The 'emoticon' should be seen whilst not obstructing the other image.Try:

    background = Image.open('someimage.jpg') #an existing image
    foreground = Image.open('foo.jpg') #one of the above images
    background.paste(foreground, (0,0), foreground)
    background.save('trial.jpg') #the composite image
    

    Theoretically, if you open 'trial.jpg' in the image viewer and the content of the initial image is preserved and on top of it lies the foo image then you'll know for sure if it's just the image viewer and your images are fine...

    0 讨论(0)
  • 2020-12-03 06:01

    source here

    Image.open('image.gif').convert('RGB').save('image.jpg')
    
    0 讨论(0)
  • 2020-12-03 06:05

    First of all, JPEG doesn't support transparency! But that's not the only problem.. As you move to the next frame of the GIF the palette information is lost (problem witn PIL?) - so PIL is unable to correctly convert to the RGBA framework (Hence the first frame is okish, but all the others are screwy). So the work-around is to add the palette back in for every frame, (which is what you were doing in your last code example, but your trouble was that you were saving as RGB not RGBA so you had no alpha/ transparency channel. Also you were doing a few unnecessary things..). Anyhow, here are the .png's with transparency and the corrected code, hope its of some use :)

    enter image description hereenter image description hereenter image description hereenter image description hereenter image description hereenter image description hereenter image description hereenter image description hereenter image description hereenter image description here

    import Image
    import sys
    
    def processImage(infile):
        try:
            im = Image.open(infile)
        except IOError:
            print "Cant load", infile
            sys.exit(1)
        i = 0
        mypalette = im.getpalette()
    
        try:
            while 1:
                im.putpalette(mypalette)
                new_im = Image.new("RGBA", im.size)
                new_im.paste(im)
                new_im.save('foo'+str(i)+'.png')
    
                i += 1
                im.seek(im.tell() + 1)
    
        except EOFError:
            pass # end of sequence
    
    processImage('gif_example.gif')
    
    0 讨论(0)
提交回复
热议问题