Qiita Teams that are logged in
You are not logged in to any team

Log in to Qiita Team
Community
OrganizationAdvent CalendarQiitadon (β)
Service
Qiita JobsQiita ZineQiita Blog
0
Help us understand the problem. What is going on with this article?
@kanayannet

apng を python で見つける

More than 1 year has passed since last update.

python でバイナリーを解析する勉強がてらに書いてみました。

crc32 というライブラリが バイナリデータと ASCII データ を変換するのに便利そうだったため、これを使う。

apng.py

from binascii import crc32

with open('anime.png', 'rb') as f:
  if f.read(8) != b'\x89PNG\r\n\x1a\n':
      raise Exception('image is not png')
  f.read(25)
  buf = f.read(4)
  while buf != b'':
      length = int.from_bytes(buf, 'big')
      data = f.read(4 + length)

      if data[:4] == b'acTL':
        num_frames = int.from_bytes(data[4:8], 'big')
        if num_frames > 1:
          print('is apng')
        break

      if data[:4] == b'IDAT':
        break

      f.read(4)
      buf = f.read(4)

ちなみに apng のデータ構造の説明はこちら
https://developer.mozilla.org/ja/docs/Animated_PNG_graphics

と思ったら 、きちんとしたライブラリもあったね汗
https://pypi.org/project/apng/

今度はこっちを使ってみるか!

お粗末さまでした!

0
Help us understand the problem. What is going on with this article?
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away

Comments

No comments
Sign up for free and join this conversation.
Sign Up
If you already have a Qiita account Login
0
Help us understand the problem. What is going on with this article?