1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

python3を用いたgifアニメの解像度変更

Last updated at Posted at 2022-04-13

python3を用いたgifアニメの解像度変更

先日、pixivに作成したgifアニメをアップロードしようとしたら、ファイルサイズの制限に引っかかってしまった。そこで、画像の解像度を落とし、ファイルサイズを小さくしようと考えた。
ブラウザ上でgifアニメの解像度を縮小するサービスはいくつか見つかったが、どれもファイル容量の制限などにより使いにくかった為、自分で作成した。

追記

  • 2022-04-14 解像度変えるだけならffmpegで十分みたいです。

開発条件

  • python3
  • scikit-video
  • opencv
    pip経由でscikit-videoとcv2を入れる

必要なライブラリをインポート

from skvideo.io import vread
import cv2
from PIL import Image,ImageDraw

gifを読み込む

from skvideo.io import vread
gif = vread("test.gif") # ここを好きなファイル名に変更

解像度を変更してcv2形式をPillowのImageに変換

size = 512 # 圧縮後のサイズ
imgs = []
for img in gif:
    new_img = cv2.resize(img,(size,size)) # サイズを変更
    imgs.append(Image.fromarray(new_img)) # pillowの形式に変換して配列に入れる

cv2のresizeで解像度を変更。PillowのImageに変換しているのは、gifとして書き込む際に、Pillow.Imageの書き込み関数を利用するため。

gifとして書き込む

imgs[0].save('test_comp.gif',save_all=True,append_images=imgs[1:],optimize=True,duration=50,loop=0)

終わりに

以上で、gifアニメの解像度を落とす事ができる。
pixivに画像が上げられなかった方はぜひ。

参考

1
0
2

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
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?