Created
January 29, 2018 13:25
-
-
Save shikachii/a0c12bfecc3370e576f2e72d135058fc to your computer and use it in GitHub Desktop.
画像をRGB565へ変換. 最初の2byte:幅, 次の2byte:高さ, それ移行:1ピクセルごとのRGB565値
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from PIL import Image | |
from argparse import ArgumentParser | |
import struct | |
usage = 'Usage: python {} FILE [--output <file>]'\ | |
.format(__file__) | |
argparser = ArgumentParser(usage=usage) | |
argparser.add_argument('fname', type=str, help='echo fname') | |
argparser.add_argument('-o', '--output', type=str, dest='output_file', help='Place the output into <file>') | |
args = argparser.parse_args() | |
im = Image.open(args.fname, "r") | |
if args.output_file: | |
f = open(args.output_file, 'wb') | |
else: | |
f = open('temp.tft', 'wb') | |
rgb_im = im.convert('RGB') | |
size = rgb_im.size | |
f.write(struct.pack("BB", size[0] >> 8, size[0] & 0x00FF)) | |
f.write(struct.pack("BB", size[1] >> 8, size[1] & 0x00FF)) | |
for y in range(size[1]): | |
for x in range(size[0]): | |
r,g,b = rgb_im.getpixel((x,y)) | |
r_tmp = r >> 3 | |
g_tmp = g >> 2 | |
b_tmp = b >> 3 | |
rgb565 = (r_tmp << 11) | (g_tmp << 5) | b_tmp | |
f.write(struct.pack("BB", rgb565 >> 8, rgb565 & 0x00FF)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment