view img2dcolor.py @ 12:580b64af2241

Add support for images that have already had their width cut down in direct color converter. Remove debug print statement.
author Mike Pavone <pavone@retrodev.com>
date Fri, 06 Sep 2013 19:13:46 -0700
parents 19226d79f839
children
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
	out_width = width if width <= 198 else width/2
	for pixel in pixels:
		if x == width:
			x = 0
			y += 1
		if x == 0:
			pad = 198-out_width
			for i in xrange(0, pad*2):
				gpixels.append(0)
		if width == out_width or 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)