File: pripnglsch

package info (click to toggle)
pypng 0.0.20%2Bds-3
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 992 kB
  • sloc: python: 4,506; sh: 186; makefile: 12
file content (40 lines) | stat: -rwxr-xr-x 825 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#!/usr/bin/env python
# pripnglsch
# PNG List Chunks

from __future__ import print_function

import argparse
import binascii
import sys

import png


def list_chunks(out, inp):
    r = png.Reader(file=inp)
    for t, v in r.chunks():
        add = ""
        if len(v) <= 28:
            add = " " + hex(v)
        else:
            add = " " + hex(v[:26]) + "..."
        t = t.decode("ascii")
        print("%s %10d%s" % (t, len(v), add), file=out)


def hex(bs):
    """Convert the bytes `bs` to a hex string."""
    return binascii.hexlify(bs).decode("ascii")


def main(argv=None):
    parser = argparse.ArgumentParser()
    parser.add_argument("png", nargs="?", default="-")
    args = parser.parse_args()
    inp = png.cli_open(args.png)
    return list_chunks(sys.stdout, inp)


if __name__ == "__main__":
    main()