Help us understand the problem. What is going on with this article?

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/

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

お粗末さまでした!

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.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  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
ユーザーは見つかりませんでした