#! /usr/bin/env python # -*- coding: utf-8 -*- # Based on https://gist.github.com/hidsh/7065820 from PIL import Image import struct, os, sys def write_bin(f, pixel_list): for pix in pixel_list: # RGB565: RRRR RGGG GGGB BBBB r = (pix[0] >> 3) & 0x1F g = (pix[1] >> 2) & 0x3F b = (pix[2] >> 3) & 0x1F # big endian u16 f.write(struct.pack('>H', (r << 11) + (g << 5) + b)) args = sys.argv if len(args) != 2: print('./png2rgb565.py HOGE.png') exit(1) in_path = args[1] body, _ = os.path.splitext(in_path) out_path = body + '.rgb' img = Image.open(in_path).convert('RGB') with open(out_path, 'wb') as f: write_bin(f, img.getdata())