Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7840147
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T15:40:30+00:00 2026-06-02T15:40:30+00:00

I tried to convert an gif to single images with Python Image Library, but

  • 0

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_example.gif

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:

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

but it results in weird output files:

Example #1 http://longcat.de/gif_example1.jpg

My second try was, to convert the gif in an RGBA first, and then use
its transparency mask, to make the transparent pieces white:

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

which results in an output like this:

Example #2 http://longcat.de/gif_example2.jpg

The advantage over the first try was, that the first frame looks pretty good
But as you can see, the rest is broken

What should i try next?

Edit:

I think i came a lot closer to the solution

Example #3 http://longcat.de/gif_example3.png

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)

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

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T15:40:32+00:00Added an answer on June 2, 2026 at 3:40 pm

    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
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've tried to convert bitrate mode from VBR to CBR with FFMPEG library,but bitrate
I tried many things but I always get cannot convert string to membershipuser from
How do you convert a System.Drawing.Bitmap image to another type of image? I tried
I was trying to convert a GIF image (Ajax waiting like): to AVI that
I have a particular set of URLs. Something like these.. http://abc.com/tb/theme/images/fbIcon.gif http://abc.com/tb/theme/css/main.css This structure
I tried to convert below SQL in a more optimized way but it is
I tried to convert all my buttons background to .9.png, but I have a
I tried to convert a large number of pdf files to images using the
I tried to convert snippet 1 to snippet 2 but this did not work
Possible Duplicate: Converting ereg expressions to preg I've tried to convert but I keep

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.