Python:将GIF框架转换为PNG

前端之家收集整理的这篇文章主要介绍了Python:将GIF框架转换为PNG前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我对 python非常新鲜,试图用它将GIF的框架分割成PNG图像.
  1. # Using this GIF: http://www.videogamesprites.net/FinalFantasy1/Party/Before/Fighter-Front.gif
  2.  
  3. from PIL import Image
  4.  
  5. im = Image.open('Fighter-Front.gif')
  6. transparency = im.info['transparency']
  7. im.save('test1.png',transparency=transparency)
  8.  
  9. im.seek(im.tell()+1)
  10. transparency = im.info['transparency']
  11. im.save('test2.png',transparency=transparency)
  12.  
  13. # First frame comes out perfect,second frame (test2.png) comes out black,# but in the "right shape",i.e.
  14. # http://i.stack.imgur.com/5GvzC.png

这是与我正在合作的形象有关,还是我做错了?

谢谢!

解决方法

我不认为你做错了什么看到类似的问题在这里: animated GIF problem.看起来好像调色板信息未被正确处理的后期帧.以下为我工作:
  1. def iter_frames(im):
  2. try:
  3. i= 0
  4. while 1:
  5. im.seek(i)
  6. imframe = im.copy()
  7. if i == 0:
  8. palette = imframe.getpalette()
  9. else:
  10. imframe.putpalette(palette)
  11. yield imframe
  12. i += 1
  13. except EOFError:
  14. pass
  15.  
  16. for i,frame in enumerate(iter_frames(im)):
  17. frame.save('test%d.png' % i,**frame.info)

猜你在找的Python相关文章