comparison 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
comparison
equal deleted inserted replaced
10:eddc4ba6b0c3 11:19226d79f839
1 #!/usr/bin/env python
2 from PIL import Image
3
4 def gchannel(Val):
5 return (Val >> 4) & 0xE
6
7 def gcolor(color):
8 if len(color) == 4:
9 (R, G, B, A) = color
10 else:
11 (R, G, B) = color
12 return (gchannel(R), gchannel(G), gchannel(B))
13
14 def trans_image(im):
15 pixels = im.getdata()
16 (width, height) = im.size
17 gpixels = bytearray()
18 A = 255
19 x = 0
20 y = 0
21 for pixel in pixels:
22 if x == width:
23 x = 0
24 y += 1
25 if x == 0:
26 pad = 198-width/2
27 print 'pad', pad
28 for i in xrange(0, pad*2):
29 gpixels.append(0)
30 if not (x & 1):
31 (R,G,B) = gcolor(pixel)
32 gpixels.append(B)
33 gpixels.append(G << 4 | R)
34 x += 1
35 return gpixels
36
37 def appendword(b, word):
38 b.append(word >> 8)
39 b.append(word & 0xff)
40
41
42
43 def main(argv):
44 if len(argv) < 3:
45 print "Not enough arguments"
46 return
47 fname = argv[1]
48 im = Image.open(fname)
49 pix = trans_image(im)
50 out = open(argv[2], 'wb')
51 out.write(pix)
52
53 if __name__ == '__main__':
54 import sys
55 main(sys.argv)