File: pripalpng

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 (123 lines) | stat: -rwxr-xr-x 2,809 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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
#!/usr/bin/env python
# pripalpng
# Convert to Palette PNG

from __future__ import print_function

import argparse
import collections

# https://docs.python.org/2.7/library/io.html
import io
import string
import struct
import sys
import warnings
import zlib

# Local module.
import png


def make_inverse_palette(rows, channels):
    """
    The inverse palette maps from tuple to palette index.
    """

    palette = {}

    for row in rows:
        for pixel in png.group(row, channels):
            if pixel in palette:
                continue
            palette[pixel] = len(palette)
    return palette


def palette_convert(inp, out, palette_file):
    """
    Convert PNG image in `inp` to use a palette, colour type 3,
    and write converted image to `out`.

    `palette_file` is a file descriptor for the palette to use.

    If `palette_file` is None, then `inp` is used as the palette.
    """

    if palette_file is None:
        inp, palette_file = palette_file, inp

    reader = png.Reader(file=palette_file)
    w, h, rows, info = asRGBorA8(reader)
    channels = info["planes"]
    if not inp:
        rows = list(rows)

    palette_map = make_inverse_palette(rows, channels)

    if inp:
        reader = png.Reader(file=inp)
        w, h, rows, info = asRGBorA8(reader)
        channels = info["planes"]

    # Default for colours not in palette is to use last entry.
    last = len(palette_map) - 1

    def map_pixel(p):
        return palette_map.get(p, last)

    def convert_rows():
        for row in rows:
            yield [map_pixel(p) for p in png.group(row, channels)]

    # Make a palette by sorting the pixels according to their index.
    palette = sorted(palette_map.keys(), key=palette_map.get)
    pal_info = dict(size=info["size"], palette=palette)

    w = png.Writer(**pal_info)
    w.write(out, convert_rows())


def asRGBorA8(reader):
    """
    Return (width, height, rows, info) converting to RGB,
    or RGBA if original has an alpha channel.
    """
    _, _, _, info = reader.read()
    if info["alpha"]:
        return reader.asRGBA8()
    else:
        return reader.asRGB8()


def main(argv=None):
    import re

    if argv is None:
        argv = sys.argv

    argv = argv[1:]

    parser = argparse.ArgumentParser()
    parser.add_argument("--palette", type=argparse.FileType("rb"))
    parser.add_argument("input", type=argparse.FileType("rb"))

    args = parser.parse_args(argv)

    # Fix for binary stdin on Python 3
    try:
        args.input = args.input.buffer
    except AttributeError:
        pass

    # Fix for binary stdin on Python 3
    try:
        args.palette = args.palette.buffer
    except AttributeError:
        pass

    palette_convert(args.input, png.binary_stdout(), args.palette)


if __name__ == "__main__":
    main()