LoginSignup
1
2

More than 3 years have passed since last update.

【iOS】Pythonista3でGIFアニメ。でハマったこと。

Last updated at Posted at 2020-08-12

はじめに

GIFアニメを作るアプリはたくさんありますが、せっかくPythonista3があるので、多くの先輩方の記事を参考にしながら、自分でも作ってみようと思いました。

すぐにハマる

以前にWin10上のPythonでGIFアニメを作ったことがあったので、その記憶を頼りにPILで書いたのですが、1枚目の画像しか保存されませんでした。

testGIF.py
w,h = 100,100
images = []

for c in range(0,256,8):
  img = Image.new('RGB',(w,h),(c,c,c))
  images.append(img)

images += reversed(images)

SaveName = 'test.gif'
images[0].save(SaveName,
             save_all=True,
             append_images=images[1:],
             optimize=False,
             duration=20,
             loop=0)

いろいろ調べたのですが、結局win10上では動作確認ができたのですが、iOS の Pillow ではバージョンが違うためうまくいかないという結論にしました。
Saving PIL images to an animated GIF | omz:forum

その名も「images2gif」

調べてるうちに同様の質問があり、「images2gif」の存在を知りました。
僕のやりたかったことがそのまま名前になったようなモジュール名です。
※しかしこれはWindowsのpython3環境では動作確認できませんでした。

  • win10のPython3環境での使い方
    1. インストール済みのimage2gifを削除します。
    2. こちらの image2gif.py をダウンロードしてPATHの通ってる場所に置く。

images2gif は公式のドキュメントにも紹介されていて、初めからPythonista 3にインストールされてるものでした。
Pythonistaモジュール— Python 3.6.1ドキュメント

「images2gif」の使い方

基本的には次の記述でいいみたいです。

writeGif( SaveName, ImageList, duration=0.1,repeat=True)

testGIF2.py
from PIL import Image
from images2gif import writeGif

w,h = 100,100
images = []

for c in range(0,256,8):
  img = Image.new('RGB',(w,h),(c,c,c))
  images.append(img)

images += reversed(images)

SaveName = 'test.gif'
writeGif( SaveName, images, duration=0.02,repeat=True)

test.gif

PythonのGIFアート

日本語ヘルプ

最後にビックリしたのは、探してたどり着いた日本語ヘルプの記事が、「以前、僕が自分で投稿したもの」で、いよいよヤバいと思いました。

1
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
2