Created
March 26, 2018 09:04
-
-
Save stek29/acf591ccc884be2e5047f7756d44ac9a to your computer and use it in GitHub Desktop.
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 | |
import struct | |
# https://msdn.microsoft.com/en-us/library/windows/desktop/dd390989(v=vs.85).aspx | |
red_mask = 0xF800; | |
green_mask = 0x7E0; | |
blue_mask = 0x1F; | |
def rgb565pixunpack(pixel): | |
r = (pixel & red_mask) >> 11; | |
g = (pixel & green_mask) >> 5; | |
b = (pixel & blue_mask); | |
return r, g, b | |
# https://github.com/iPodLinux/hotdog/blob/master/hotdog_ipg.c | |
def unpack(fc): | |
width = struct.unpack_from('<i', fc[0:4])[0] | |
height = struct.unpack_from('<i', fc[4:8])[0] | |
id = struct.unpack_from('<i', fc[8:12])[0] | |
other = struct.unpack_from('<i', fc[12:16])[0] | |
if id != 4097: | |
raise Exception("not rgb565 but %d"%id) | |
img = Image.new('RGB', (width, height), "black") | |
pixels = img.load() | |
fc = fc[16:] | |
for i in range(width): | |
for j in range(height): | |
pixel = struct.unpack_from('<H', fc)[0] | |
pixels[i,j] = rgb565pixunpack(pixel) | |
fc = fc[2:] | |
return img | |
if __name__ == '__main__': | |
import sys | |
f = open(sys.argv[1], 'rb') | |
img = unpack(f.read()) | |
f.close() | |
img.show() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment