view img2dcolor.py @ 11:19226d79f839

Add direct color DMA demo and converter
author Mike Pavone <pavone@retrodev.com>
date Fri, 06 Sep 2013 19:09:19 -0700
parents
children 580b64af2241
line wrap: on
line source

#!/usr/bin/env python
from PIL import Image

def gchannel(Val):
	return (Val >> 4) & 0xE

def gcolor(color):
	if len(color) == 4:
		(R, G, B, A) = color
	else:
		(R, G, B) = color
	return (gchannel(R), gchannel(G), gchannel(B))

def trans_image(im):
	pixels = im.getdata()
	(width, height) = im.size
	gpixels = bytearray()
	A = 255
	x = 0
	y = 0
	for pixel in pixels:
		if x == width:
			x = 0
			y += 1
		if x == 0:
			pad = 198-width/2
			print 'pad', pad
			for i in xrange(0, pad*2):
				gpixels.append(0)
		if not (x & 1):
			(R,G,B) = gcolor(pixel)
			gpixels.append(B)
			gpixels.append(G << 4 | R)
		x += 1
	return gpixels

def appendword(b, word):
	b.append(word >> 8)
	b.append(word & 0xff)



def main(argv):
	if len(argv) < 3:
		print "Not enough arguments"
		return
	fname = argv[1]
	im = Image.open(fname)
	pix = trans_image(im)
	out = open(argv[2], 'wb')
	out.write(pix)

if __name__ == '__main__':
	import sys
	main(sys.argv)