-
Notifications
You must be signed in to change notification settings - Fork 60
/
image_converter.py
executable file
·158 lines (116 loc) · 4.33 KB
/
image_converter.py
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
#!/usr/bin/env python3
"""
Convert an image file to a python module for use with the bitmap method. Use redirection to save the
output to a file. The image is converted to a bitmap using the number of bits per pixel you specify.
The bitmap is saved as a python module that can be imported and used with the bitmap method.
.. seealso::
- :ref:`alien.py<alien>`.
Example
^^^^^^^
.. code-block:: console
./create_png_examples.py cat.png 4 > cat_bitmap.py
The python file can be imported and displayed with the bitmap method. For example:
.. code-block:: python
import tft_config
import cat_bitmap
tft = tft_config.config(1)
tft.bitmap(cat_bitmap, 0, 0)
Usage
^^^^^
.. code-block:: console
usage: image_converter.py [-h] image_file bits_per_pixel
Convert image file to python module for use with bitmap method.
positional arguments: image_file Name of file containing image to convert bits_per_pixel
The number of bits to use per pixel (1..8)
optional arguments: -h, --help show this help message and exit
"""
import sys
import argparse
from PIL import Image
def rgb_to_color565(r, g, b):
"""
Convert RGB color to the 16-bit color format (565).
Args:
r (int): Red component of the RGB color (0-255).
g (int): Green component of the RGB color (0-255).
b (int): Blue component of the RGB color (0-255).
Returns:
int: Converted color value in the 16-bit color format (565).
"""
return ((r & 0xF8) << 8) | ((g & 0xFC) << 3) | (b & 0xF8)
def convert_to_bitmap(image_file, bits_requested):
"""
Convert image file to python module for use with bitmap method.
Args:
image_file (str): Name of file containing image to convert.
bits (int): The number of bits to use per pixel (1..8).
"""
colors_requested = 1 << bits_requested
img = Image.open(image_file).convert("RGB")
img = img.convert("P", palette=Image.Palette.ADAPTIVE, colors=colors_requested)
palette = img.getpalette()
palette_colors = len(palette) // 3
actual_colors = min(palette_colors, colors_requested)
bits_required = actual_colors.bit_length()
if bits_required < bits_requested:
print(
f"\nNOTE: Quantization reduced colors to {palette_colors} from the {bits_requested} "
f"requested, reconverting using {bits_required} bit per pixel could save memory.\n",
file=sys.stderr,
)
colors = [
f"{rgb_to_color565(palette[color * 3], palette[color * 3 + 1], palette[color * 3 + 2]):04x}"
for color in range(actual_colors)
]
image_bitstring = "".join(
"".join(
"1" if (img.getpixel((x, y)) & (1 << bit - 1)) else "0"
for bit in range(bits_required, 0, -1)
)
for y in range(img.height)
for x in range(img.width)
)
bitmap_bits = len(image_bitstring)
print(f"HEIGHT = {img.height}")
print(f"WIDTH = {img.width}")
print(f"COLORS = {actual_colors}")
print(f"BITS = {bitmap_bits}")
print(f"BPP = {bits_required}")
print("PALETTE = [", end="")
for i, rgb in enumerate(colors):
if i > 0:
print(",", end="")
print(f"0x{rgb}", end="")
print("]")
print("_bitmap =\\\nb'", end="")
for i in range(0, bitmap_bits, 8):
if i and i % (16 * 8) == 0:
print("'\\\nb'", end="")
value = image_bitstring[i : i + 8]
color = int(value, 2)
print(f"\\x{color:02x}", end="")
print("'\nBITMAP = memoryview(_bitmap)")
def main():
"""
Convert image file to python module for use with bitmap method.
Args:
image_file (str): Name of file containing image to convert.
bits_per_pixel (int): The number of bits to use per pixel (1..8).
"""
parser = argparse.ArgumentParser(
description="Convert image file to python module for use with bitmap method.",
)
parser.add_argument("image_file", help="Name of file containing image to convert")
parser.add_argument(
"bits_per_pixel",
type=int,
choices=range(1, 9),
default=1,
metavar="bits_per_pixel",
help="The number of bits to use per pixel (1..8)",
)
args = parser.parse_args()
bits = args.bits_per_pixel
convert_to_bitmap(args.image_file, bits)
if __name__ == "__main__":
main()