Note that there are some explanatory texts on larger screens.

plurals
  1. POPIL - Convert GIF Frames to JPG
    text
    copied!<p>I tried to convert an gif to single images with Python Image Library, but it results in weird frames</p> <p>The Input gif is:</p> <p><a href="http://longcat.de/gif_example.gif">Source Image http://longcat.de/gif_example.gif</a></p> <p>In my first try, i tried to convert the image with Image.new to an RGB image, with 255,255,255 as white background - like in any other example i've found on the internet:</p> <pre><code>def processImage( infile ): try: im = Image.open( infile ) except IOError: print "Cant load", infile sys.exit(1) i = 0 try: while 1: background = Image.new("RGB", im.size, (255, 255, 255)) background.paste(im) background.save('foo'+str(i)+'.jpg', 'JPEG', quality=80) i += 1 im.seek( im.tell() + 1 ) except EOFError: pass # end of sequence </code></pre> <p>but it results in weird output files:</p> <p><a href="http://longcat.de/gif_example1.jpg">Example #1 http://longcat.de/gif_example1.jpg</a></p> <p>My second try was, to convert the gif in an RGBA first, and then use its transparency mask, to make the transparent pieces white:</p> <pre><code>def processImage( infile ): try: im = Image.open( infile ) except IOError: print "Cant load", infile sys.exit(1) i = 0 try: while 1: im2 = im.convert('RGBA') im2.load() background = Image.new("RGB", im2.size, (255, 255, 255)) background.paste(im2, mask = im2.split()[3] ) background.save('foo'+str(i)+'.jpg', 'JPEG', quality=80) i += 1 im.seek( im.tell() + 1 ) except EOFError: pass # end of sequence </code></pre> <p>which results in an output like this:</p> <p><a href="http://longcat.de/gif_example2.jpg">Example #2 http://longcat.de/gif_example2.jpg</a></p> <p>The advantage over the first try was, that the first frame looks pretty good But as you can see, the rest is broken</p> <p>What should i try next?</p> <p>Edit:</p> <p>I think i came a lot closer to the solution</p> <p><a href="http://longcat.de/gif_example3.png">Example #3 http://longcat.de/gif_example3.png</a></p> <p>I had to use the palette of the first image for the other images, and merge it with the previous frame (for gif animations which use diff-images)</p> <pre><code>def processImage( infile ): try: im = Image.open( infile ) except IOError: print "Cant load", infile sys.exit(1) i = 0 size = im.size lastframe = im.convert('RGBA') mypalette = im.getpalette() try: while 1: im2 = im.copy() im2.putpalette( mypalette ) background = Image.new("RGB", size, (255,255,255)) background.paste( lastframe ) background.paste( im2 ) background.save('foo'+str(i)+'.png', 'PNG', quality=80) lastframe = background i += 1 im.seek( im.tell() + 1 ) except EOFError: pass # end of sequence </code></pre> <p>But i actually dont know, why my transparency is black, instead of white Even if i modify the palette (change the transparency channel to white) or use the transparency mask, the background is still black</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload