Skip to content

Instantly share code, notes, and snippets.

@hidsh
Last active March 31, 2023 14:40
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hidsh/7065820 to your computer and use it in GitHub Desktop.
Save hidsh/7065820 to your computer and use it in GitHub Desktop.
convert image: png --> rgb565
#! /usr/bin/env python
# -*- coding: utf-8 -*-
from PIL import Image
import struct, os, sys
def usage():
print './png2rgb565.py HOGE.png'
sys.exit(1)
def error(msg):
print msg
sys.exit(-1)
def write_bin(f, pixel_list):
for pix in pixel_list:
r = (pix[0] >> 3) & 0x1F
g = (pix[1] >> 2) & 0x3F
b = (pix[2] >> 3) & 0x1F
f.write(struct.pack('H', (r << 11) + (g << 5) + b))
##
if __name__ == '__main__':
args = sys.argv
if len(args) != 2: usage()
in_path = args[1]
if os.path.exists(in_path) == False: error('not exists: ' + in_path)
body, _ = os.path.splitext(in_path)
out_path = body + '.bin'
img = Image.open(in_path).convert('RGB')
pixels = list(img.getdata())
# print pixels
with open(out_path, 'wb') as f:
write_bin(f, pixels)
@dinesh2212
Copy link

poggers

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment