forked from pret/pokered
-
Notifications
You must be signed in to change notification settings - Fork 2
/
rgb555.py
executable file
·38 lines (30 loc) · 893 Bytes
/
rgb555.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Usage: python rgb555.py image.png
Round all colors of the input image to RGB555.
"""
import sys
import png
def rgb8_to_rgb5(c):
return (c & 0b11111000) | (c >> 5)
def round_pal(filename):
with open(filename, 'rb') as file:
width, height, rows = png.Reader(file).asRGBA8()[:3]
rows = list(rows)
for row in rows:
del row[3::4]
rows = [[rgb8_to_rgb5(c) for c in row] for row in rows]
writer = png.Writer(width, height, greyscale=False, bitdepth=8, compression=9)
with open(filename, 'wb') as file:
writer.write(file, rows)
def main():
if len(sys.argv) < 2:
print(f'Usage: {sys.argv[0]} pic.png', file=sys.stderr)
sys.exit(1)
for filename in sys.argv[1:]:
if not filename.lower().endswith('.png'):
print(f'{filename} is not a .png file!', file=sys.stderr)
round_pal(filename)
if __name__ == '__main__':
main()