2011-07-11 3 views
3

私はPythonでPILとTkinterを使ってチャットプログラム内の画像を自動的にアニメーション化しようとしています。時々彼らは働くが、ほとんどの場合、彼らはしない。PIL-アニメーションGIFですが、いくつかのフレームは黒です

基本的には、GIFシーケンスの各画像でImageTk.PhotoImageを呼び出して画像をアニメートしています。アニメーションは、ラベルウィジェットをroot.after呼び出しで更新することによって実行されます。アニメーションはかなりスムーズに動作します

私の問題は、私が実行しようとしているほとんどのアニメーションGIFが壊れている、または何かです。通常、最初のフレームは問題ありませんが、残りの部分は完全に黒色かアーティファクトでいっぱいです。

これらはイメージです。

この1つはそれが動作するいくつかのいずれかです。 http://fc02.deviantart.net/fs42/f/2009/061/d/8/d856ba6223c66ce347c6c814f67d307b.gif

これらのものは黒の点滅:

http://fc02.deviantart.net/fs70/f/2010/007/c/3/_mekolai__by_Pyritie.gif http://e.deviantart.com/emoticons/s/shakefist.gif

編集:私はノー見ます私を助けたいと思っています。多分それはtlだからです; dr。私はそれ

にいくつかのコードを短くしてみましょう:

def ExtHandler(self, widget, url): # sorts out what to do with each file extensions 
    name = url.split('/')[-1].split('?')[0] 
    path = './Storage/Temp/Images/'+name 
    try: 
     if name.endswith('.gif'): 
      img = Image.open(path) 
      animation = [] 
      x = 0 
      print name 
      while True: 
       try: 
        img.seek(x) 

        newpath = './Storage/Temp/Images/{0}__{1}.png'.format(x, name.split('.', 1)[0]) 
        img.save(newpath, transparency = img.info['transparency'], format='PNG') 

        newimg = Image.open(path) 
        newimg.load() 
        newimg = newimg.convert('RGB').convert('P', palette=Image.ADAPTIVE, colors=255) 

        dur = img.info['duration'] 
        if dur < 50: dur = 50 
        newimg = Image.open(newpath) 

        animation.append((PhotoImage2(newimg), dur)) 
        x += 1 
       except EOFError: 
        break #no more images in the animation! 
       except Exception as e: 
        print traceback.format_exc() 
        break 
      if len(animation) > 1: 
       self.animations[name] = animation 

     elif name.endswith('.jpg'): 
      img = Image.open(path) 
      img = img.convert('RGB').convert('P', palette=Image.ADAPTIVE, colors=255) 

     elif name.endswith('.png'): 
      img = Image.open(path) 
      img.load() 
      try: 
       alpha = img.split()[3] 
       img = img.convert('RGB').convert('P', palette=Image.ADAPTIVE, colors=255) 
       mask = Image.eval(alpha, lambda a: 255 if a <=128 else 0) 
       img.paste(255, mask) 
      except: 
       img = img.convert('RGB').convert('P', palette=Image.ADAPTIVE, colors=255) 

     height, width = img.size[0], img.size[1] 
     if width > 100 or height > 100: 
      img = maxSize(img, (100, 100), Image.ANTIALIAS) # resize thumbnails 

     self.images[name] = PhotoImage2(img) 
     if name in self.animations: 
      self.animation(name) 
     else: 
      self.update_frames(name, self.images[name]) 
    except: 
     print name 
     traceback.print_exc() 

def animation(self, name): 
    if name not in self.animations: 
     return 
    x = self.animations[name].pop(0) 
    self.animations[name].append(x) 
    img, time = x 

    self.images[name] = img 
    self.update_frames(name, self.images[name]) 
    self.root.after(time, lambda: self.animation(name)) 

答えて

0

興味深い..私はすべてのアニメーションそれらを参照してください。

私はこの回答にあなたのリンクを埋め込んだので、人々はあなたのリンクをクリックしてアニメーションを見る必要はありません。

Works

Works

enter image description here

私はブラウザとしてChromeを使用しています注意してください。私はこのメッセージを保存し、Internet ExplorerとFireFoxでどのように見えるかを見て、結果を報告します。

...

更新

[OK]を、ここでの結果は以下のとおりです。

私はWindows XP上でです。

  • 作品は、Firefox 5.0だから、

  • 作品クローム14.0.810.0と
  • 作品クローム10.0.648.205とIE8
  • 作品で、私はここにあなたの問題を再現することはできません。これらのアニメーションは、人々のブラウザでは期待どおりに動作すると想定するのはかなり安全だと思います。

    アニメーションの表示方法に関する詳細を追加する必要があります。

    技術的な観点からは問題はありませんが、アニメーションは安価に見えるので、サイトに追加する前に2度考えていますが、それは趣味の問題です。

    更新2 ああ、あなたの質問が再読されました。あなたはブラウザを使用していませんが、アプリケーションを作成しています。問題はあなたが使っているライブラリにあると確信しています。

    多分GifアニメーションワークショップやそのようなプログラムのようなものでGIFを開くことができます。違いを見るには多分それらをすべて保存するような方法で保存してください。

    これは私が恐れている今あなたを助けるために言うことができるすべてです。

  • +0

    私はPythonでPILとTkinterを使用しています。申し訳ありませんが、私は私がその情報を削除した私の質問を再編集したことを認識しませんでした – Blazer

    0

    これは(PNGファイルのシリーズにGIFを変換するための)私の作業バージョンです:

    import traceback 
    import Image 
    
    class Test: 
    
        def __init__(self): 
         self.ExtHandler("", "http://fc02.deviantart.net/fs42/f/2009/061/d/8/d856ba6223c66ce347c6c814f67d307b.gif") 
         self.ExtHandler("", "http://fc02.deviantart.net/fs70/f/2010/007/c/3/_mekolai__by_Pyritie.gif") 
         self.ExtHandler("", "http://e.deviantart.com/emoticons/s/shakefist.gif") 
    
        def ExtHandler(self, widget, url): # sorts out what to do with each file extensions 
         name = url.split('/')[-1].split('?')[0] 
         path = 'C:\\Temp\\'+name 
         try: 
          if name.endswith('.gif'): 
           img = Image.open(path) 
           print name 
           while True: 
            try: 
             x = img.tell() + 1 
             img.seek(x) 
    
             newpath = 'C:\\Temp\\{1}_{0}.png'.format(x, name.split('.', 1)[0]) 
             img.save(newpath, transparency = img.info['transparency']) 
            except EOFError: 
             break #no more images in the animation! 
            except Exception as e: 
             print traceback.format_exc() 
             break 
    
          elif name.endswith('.jpg'): 
           img = Image.open(path) 
           img = img.convert('RGB').convert('P', palette=Image.ADAPTIVE, colors=255) 
    
          elif name.endswith('.png'): 
           img = Image.open(path) 
           img.load() 
           try: 
            alpha = img.split()[3] 
            img = img.convert('RGB').convert('P', palette=Image.ADAPTIVE, colors=255) 
            mask = Image.eval(alpha, lambda a: 255 if a <=128 else 0) 
            img.paste(255, mask) 
           except: 
            img = img.convert('RGB').convert('P', palette=Image.ADAPTIVE, colors=255) 
         except: 
          print name 
          traceback.print_exc() 
    
    Test() 
    

    私は解決策が A)(img.tellを求めていると思います)+ 1 b)にセーブなしフォーマットパラメータ

    実際のコードでは、おそらくgifフレームをpngとしてキャッシュする必要はありません。 img.tell()+ 1でシークしてください。

    +0

    私は問題を発見したと思います。私はそれらを保存した直後にイメージを開いていたので、I/Oレベルで物事を盗んだかもしれないと思います。私はそれを2回繰り返すようにしました。一度すべてを保存し、すべてのパスとアニメーションの継続時間をリストに追加し、2番目のループはパスのリストを反復して開きます。これはうまくいった。 – Blazer

    関連する問題