Place GIF In Front Of A PNG With Python PIL Library - Stack Overflow

    1. Home
    2. Questions
    3. Tags
    4. Users
    5. Companies
    6. Labs
    7. Jobs
    8. Discussions
    9. Collectives
    10. Communities for your favorite technologies. Explore all Collectives

  1. Teams

    Ask questions, find answers and collaborate at work with Stack Overflow for Teams.

    Try Teams for free Explore Teams
  2. Teams
  3. Ask questions, find answers and collaborate at work with Stack Overflow for Teams. Explore Teams

Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

Get early access and see previews of new features.

Learn more about Labs Place GIF in front of a PNG with Python PIL library Ask Question Asked 2 years, 11 months ago Modified 2 years, 11 months ago Viewed 617 times 0

I am trying to merge a PNG file with a GIF file using Python 3 and PIL library.

I want to place the PNG file in the background, and the GIF file in the foreground.I was able to do the opposite, placing a GIF file in the background and a PNG file in the foreground. Thanks to the person who answered here: How to merge a image and a Gif in Python Pillow However, when I try to do what I need, I get only the transparent GIF and no PNG on the background of the image.

I use this transparent GIF: https://i.sstatic.net/IHPQG.gif

I use this PNG: https://i.sstatic.net/GZl9O.png

I get this output: https://i.sstatic.net/U1i2H.gif

My code:

from PIL import Image foreground = Image.open('image.gif') background = Image.open('image.png').convert("RGBA") img_w, img_h = background.size foreground_w, foreground_w = foreground.size if foreground.is_animated: frames = [] for num in range(foreground.n_frames): foreground.seek(num) layer = Image.new('RGBA', (foreground_w, foreground_w), (0, 0, 0, 0)).resize((img_w, img_h)) layer.paste(background, (0,0), mask=background) layer.paste(foreground.resize((img_w, img_h)), (0,0)) frames.append(layer) frames[0].save('1.gif', save_all=True, append_images=frames[1:], duration=100,loop=0) Share Improve this question Follow asked Jan 1, 2022 at 20:42 Mahmoud Embaby's user avatar Mahmoud EmbabyMahmoud Embaby 3173 silver badges12 bronze badges Add a comment |

1 Answer 1

Sorted by: Reset to default Highest score (default) Trending (recent votes count more) Date modified (newest first) Date created (oldest first) 1

Even though you call it a "Transparent GIF", there's not a single transparent pixel in it. The palette contains 16 colors, but 99.99% of each frame consists of the first 2 palette entries. The first 3 frames use 4 colors and the 4th uses 6.

If the GIF contained proper transparency, a simple over = foreground.convert('RGBA') would give you an image with alpha transparency.

Share Improve this answer Follow answered Jan 3, 2022 at 2:12 Mark Ransom's user avatar Mark RansomMark Ransom 308k44 gold badges416 silver badges647 bronze badges Add a comment |

Your Answer

Reminder: Answers generated by artificial intelligence tools are not allowed on Stack Overflow. Learn more

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid …

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.

Draft saved Draft discarded

Sign up or log in

Sign up using Google Sign up using Email and Password Submit

Post as a guest

Name Email

Required, but never shown

Post Your Answer Discard

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.

  • The Overflow Blog
  • How developer jobs (and the job market) changed in 2024
  • You should keep a developer’s journal
  • Featured on Meta
  • The December 2024 Community Asks Sprint has been moved to March 2025 (and...
  • Stack Overflow Jobs is expanding to more countries

Linked

1 How to merge a image and a Gif in Python Pillow 1 How to change gif file to png file using python pil 4 python tkinter display animated GIF using PIL 47 How to CREATE a transparent gif (or png) with PIL (python-imaging) 4 Displaying animated GIFs in Tkinter using PIL 1 Watermark GIF with Python PIL 2 Writing text over GIF using PIL not working 0 Opening a GIF picture with PIL in Python 0 How to add image on gif using Pillow? 2 PIL Png With Gif Background 2 Merging images in Python PIL to produce animated gifs

Hot Network Questions

  • Are these two circuits equivalent? How to prove it?
  • VSCode highlight .tikz file similar to .tex
  • What ranks did the French Garde National have in 1848?
  • Tournament of Towns 2012 Spring Senior O5
  • Will a PC complain if a USB 2 flash drive is powered externally?
  • How can I replace the anode rod with this in the way?
  • How to differentiate coyote vs wolf tracks
  • Why is the air pressure different between the inside and outside of my house?
  • Does the universe not being locally real mean anything for our daily lives?
  • Is it possible to do multiple substitions in Visual select mode?
  • "Largest snake to ever live discovered in an Indian mine." VS "Largest snake to have ever lived ......"?
  • Why did Gru have to adopt the girls?
  • LM358 low output in simulation
  • Can we use "some" to replace "any" in "...and does not represent any Amazon employees despite any claims..."
  • What makes a good motivator?
  • Could a solar farm work at night?
  • Can I use copyleft-licensed library in MIT-licensed project?
  • Space Shuttle HUD use outside of landing?
  • Does adding a reflector to a patio heater cause a safety hazard?
  • Book where protagonist who repairs vases for a living is contacted by alien race
  • Elementary consequence of non-abelian class field theory
  • Is it usual for instructors to adapt their accent to seem more "professional"?
  • Is it a good idea to immerse the circuit in an engineered fluid in order to minimize circuit drift
  • Is the second-quantized density-density interaction positive-definite?
more hot questions Question feed Subscribe to RSS Question feed

To subscribe to this RSS feed, copy and paste this URL into your RSS reader.

lang-py

Từ khóa » X Png Gif