annotate img2tiles.py @ 8:a049de420cc1

Make number of static and dynamic colors command line arguments to img2tiles.py
author Mike Pavone <pavone@retrodev.com>
date Tue, 03 Sep 2013 09:49:29 -0700
parents a74776d80f95
children 997690aa0507
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
3
97ec271a513f Bunch of changes
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1 #!/usr/bin/env python
97ec271a513f Bunch of changes
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
2 from PIL import Image
97ec271a513f Bunch of changes
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
3
97ec271a513f Bunch of changes
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
4 def gchannel(Val):
97ec271a513f Bunch of changes
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
5 return (Val >> 4) & 0xE
97ec271a513f Bunch of changes
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
6
97ec271a513f Bunch of changes
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
7 threshold = 127
97ec271a513f Bunch of changes
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
8
97ec271a513f Bunch of changes
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
9 def get_color_info(pixels, rng, threshold, exclude={}):
97ec271a513f Bunch of changes
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
10 gencolors = {}
97ec271a513f Bunch of changes
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
11 A = 255
97ec271a513f Bunch of changes
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
12 for idx in rng:
97ec271a513f Bunch of changes
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
13 color = pixels[idx]
97ec271a513f Bunch of changes
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
14 if len(color) == 4:
97ec271a513f Bunch of changes
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
15 (R, G, B, A) = color
97ec271a513f Bunch of changes
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
16 else:
97ec271a513f Bunch of changes
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
17 (R, G, B) = color
97ec271a513f Bunch of changes
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
18 if A > threshold:
97ec271a513f Bunch of changes
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
19 gcolor = (gchannel(R), gchannel(G), gchannel(B))
97ec271a513f Bunch of changes
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
20 if not gcolor in exclude:
97ec271a513f Bunch of changes
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
21 if gcolor in gencolors:
97ec271a513f Bunch of changes
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
22 gencolors[gcolor] += 1
97ec271a513f Bunch of changes
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
23 else:
97ec271a513f Bunch of changes
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
24 gencolors[gcolor] = 1
97ec271a513f Bunch of changes
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
25 glist = [(gencolors[color], color) for color in gencolors]
97ec271a513f Bunch of changes
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
26 glist.sort()
97ec271a513f Bunch of changes
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
27 glist.reverse()
97ec271a513f Bunch of changes
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
28 return glist
97ec271a513f Bunch of changes
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
29
5
a74776d80f95 Fix a bug in selecting the best match color when an exact match is unavailable. Add some more options for choosing a line palette
Mike Pavone <pavone@retrodev.com>
parents: 3
diff changeset
30 def get_color_info_error(pixels, rng, threshold, exclude={}):
a74776d80f95 Fix a bug in selecting the best match color when an exact match is unavailable. Add some more options for choosing a line palette
Mike Pavone <pavone@retrodev.com>
parents: 3
diff changeset
31 gencolors = {}
a74776d80f95 Fix a bug in selecting the best match color when an exact match is unavailable. Add some more options for choosing a line palette
Mike Pavone <pavone@retrodev.com>
parents: 3
diff changeset
32 A = 255
a74776d80f95 Fix a bug in selecting the best match color when an exact match is unavailable. Add some more options for choosing a line palette
Mike Pavone <pavone@retrodev.com>
parents: 3
diff changeset
33 for idx in rng:
a74776d80f95 Fix a bug in selecting the best match color when an exact match is unavailable. Add some more options for choosing a line palette
Mike Pavone <pavone@retrodev.com>
parents: 3
diff changeset
34 color = pixels[idx]
a74776d80f95 Fix a bug in selecting the best match color when an exact match is unavailable. Add some more options for choosing a line palette
Mike Pavone <pavone@retrodev.com>
parents: 3
diff changeset
35 if len(color) == 4:
a74776d80f95 Fix a bug in selecting the best match color when an exact match is unavailable. Add some more options for choosing a line palette
Mike Pavone <pavone@retrodev.com>
parents: 3
diff changeset
36 (R, G, B, A) = color
a74776d80f95 Fix a bug in selecting the best match color when an exact match is unavailable. Add some more options for choosing a line palette
Mike Pavone <pavone@retrodev.com>
parents: 3
diff changeset
37 else:
a74776d80f95 Fix a bug in selecting the best match color when an exact match is unavailable. Add some more options for choosing a line palette
Mike Pavone <pavone@retrodev.com>
parents: 3
diff changeset
38 (R, G, B) = color
a74776d80f95 Fix a bug in selecting the best match color when an exact match is unavailable. Add some more options for choosing a line palette
Mike Pavone <pavone@retrodev.com>
parents: 3
diff changeset
39 if A > threshold:
a74776d80f95 Fix a bug in selecting the best match color when an exact match is unavailable. Add some more options for choosing a line palette
Mike Pavone <pavone@retrodev.com>
parents: 3
diff changeset
40 gcolor = (gchannel(R), gchannel(G), gchannel(B))
a74776d80f95 Fix a bug in selecting the best match color when an exact match is unavailable. Add some more options for choosing a line palette
Mike Pavone <pavone@retrodev.com>
parents: 3
diff changeset
41 if not gcolor in exclude:
a74776d80f95 Fix a bug in selecting the best match color when an exact match is unavailable. Add some more options for choosing a line palette
Mike Pavone <pavone@retrodev.com>
parents: 3
diff changeset
42 if not gcolor in gencolors:
a74776d80f95 Fix a bug in selecting the best match color when an exact match is unavailable. Add some more options for choosing a line palette
Mike Pavone <pavone@retrodev.com>
parents: 3
diff changeset
43 _,best = best_match(gcolor, (exclude,))
a74776d80f95 Fix a bug in selecting the best match color when an exact match is unavailable. Add some more options for choosing a line palette
Mike Pavone <pavone@retrodev.com>
parents: 3
diff changeset
44 gencolors[gcolor] = color_dist(gcolor, best)
a74776d80f95 Fix a bug in selecting the best match color when an exact match is unavailable. Add some more options for choosing a line palette
Mike Pavone <pavone@retrodev.com>
parents: 3
diff changeset
45 glist = [(gencolors[color], color) for color in gencolors]
a74776d80f95 Fix a bug in selecting the best match color when an exact match is unavailable. Add some more options for choosing a line palette
Mike Pavone <pavone@retrodev.com>
parents: 3
diff changeset
46 glist.sort()
a74776d80f95 Fix a bug in selecting the best match color when an exact match is unavailable. Add some more options for choosing a line palette
Mike Pavone <pavone@retrodev.com>
parents: 3
diff changeset
47 glist.reverse()
a74776d80f95 Fix a bug in selecting the best match color when an exact match is unavailable. Add some more options for choosing a line palette
Mike Pavone <pavone@retrodev.com>
parents: 3
diff changeset
48 return glist
a74776d80f95 Fix a bug in selecting the best match color when an exact match is unavailable. Add some more options for choosing a line palette
Mike Pavone <pavone@retrodev.com>
parents: 3
diff changeset
49
a74776d80f95 Fix a bug in selecting the best match color when an exact match is unavailable. Add some more options for choosing a line palette
Mike Pavone <pavone@retrodev.com>
parents: 3
diff changeset
50 def get_color_info_both(pixels, rng, threshold, exclude={}):
a74776d80f95 Fix a bug in selecting the best match color when an exact match is unavailable. Add some more options for choosing a line palette
Mike Pavone <pavone@retrodev.com>
parents: 3
diff changeset
51 gencolors = {}
a74776d80f95 Fix a bug in selecting the best match color when an exact match is unavailable. Add some more options for choosing a line palette
Mike Pavone <pavone@retrodev.com>
parents: 3
diff changeset
52 A = 255
a74776d80f95 Fix a bug in selecting the best match color when an exact match is unavailable. Add some more options for choosing a line palette
Mike Pavone <pavone@retrodev.com>
parents: 3
diff changeset
53 for idx in rng:
a74776d80f95 Fix a bug in selecting the best match color when an exact match is unavailable. Add some more options for choosing a line palette
Mike Pavone <pavone@retrodev.com>
parents: 3
diff changeset
54 color = pixels[idx]
a74776d80f95 Fix a bug in selecting the best match color when an exact match is unavailable. Add some more options for choosing a line palette
Mike Pavone <pavone@retrodev.com>
parents: 3
diff changeset
55 if len(color) == 4:
a74776d80f95 Fix a bug in selecting the best match color when an exact match is unavailable. Add some more options for choosing a line palette
Mike Pavone <pavone@retrodev.com>
parents: 3
diff changeset
56 (R, G, B, A) = color
a74776d80f95 Fix a bug in selecting the best match color when an exact match is unavailable. Add some more options for choosing a line palette
Mike Pavone <pavone@retrodev.com>
parents: 3
diff changeset
57 else:
a74776d80f95 Fix a bug in selecting the best match color when an exact match is unavailable. Add some more options for choosing a line palette
Mike Pavone <pavone@retrodev.com>
parents: 3
diff changeset
58 (R, G, B) = color
a74776d80f95 Fix a bug in selecting the best match color when an exact match is unavailable. Add some more options for choosing a line palette
Mike Pavone <pavone@retrodev.com>
parents: 3
diff changeset
59 if A > threshold:
a74776d80f95 Fix a bug in selecting the best match color when an exact match is unavailable. Add some more options for choosing a line palette
Mike Pavone <pavone@retrodev.com>
parents: 3
diff changeset
60 gcolor = (gchannel(R), gchannel(G), gchannel(B))
a74776d80f95 Fix a bug in selecting the best match color when an exact match is unavailable. Add some more options for choosing a line palette
Mike Pavone <pavone@retrodev.com>
parents: 3
diff changeset
61 if not gcolor in exclude:
a74776d80f95 Fix a bug in selecting the best match color when an exact match is unavailable. Add some more options for choosing a line palette
Mike Pavone <pavone@retrodev.com>
parents: 3
diff changeset
62 if not gcolor in gencolors:
a74776d80f95 Fix a bug in selecting the best match color when an exact match is unavailable. Add some more options for choosing a line palette
Mike Pavone <pavone@retrodev.com>
parents: 3
diff changeset
63 _,best = best_match(gcolor, (exclude,))
a74776d80f95 Fix a bug in selecting the best match color when an exact match is unavailable. Add some more options for choosing a line palette
Mike Pavone <pavone@retrodev.com>
parents: 3
diff changeset
64 gencolors[gcolor] = (color_dist(gcolor, best), 1)
a74776d80f95 Fix a bug in selecting the best match color when an exact match is unavailable. Add some more options for choosing a line palette
Mike Pavone <pavone@retrodev.com>
parents: 3
diff changeset
65 else:
a74776d80f95 Fix a bug in selecting the best match color when an exact match is unavailable. Add some more options for choosing a line palette
Mike Pavone <pavone@retrodev.com>
parents: 3
diff changeset
66 (dist, count) = gencolors[gcolor]
a74776d80f95 Fix a bug in selecting the best match color when an exact match is unavailable. Add some more options for choosing a line palette
Mike Pavone <pavone@retrodev.com>
parents: 3
diff changeset
67 gencolors[gcolor] = (dist, count+1)
a74776d80f95 Fix a bug in selecting the best match color when an exact match is unavailable. Add some more options for choosing a line palette
Mike Pavone <pavone@retrodev.com>
parents: 3
diff changeset
68 glist = [(gencolors[color][0] * gencolors[color][1], color) for color in gencolors]
a74776d80f95 Fix a bug in selecting the best match color when an exact match is unavailable. Add some more options for choosing a line palette
Mike Pavone <pavone@retrodev.com>
parents: 3
diff changeset
69 glist.sort()
a74776d80f95 Fix a bug in selecting the best match color when an exact match is unavailable. Add some more options for choosing a line palette
Mike Pavone <pavone@retrodev.com>
parents: 3
diff changeset
70 glist.reverse()
a74776d80f95 Fix a bug in selecting the best match color when an exact match is unavailable. Add some more options for choosing a line palette
Mike Pavone <pavone@retrodev.com>
parents: 3
diff changeset
71 return glist
a74776d80f95 Fix a bug in selecting the best match color when an exact match is unavailable. Add some more options for choosing a line palette
Mike Pavone <pavone@retrodev.com>
parents: 3
diff changeset
72
3
97ec271a513f Bunch of changes
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
73 def totiles(im, palette):
97ec271a513f Bunch of changes
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
74 pass
97ec271a513f Bunch of changes
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
75
97ec271a513f Bunch of changes
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
76 def make_palette(im, trans_thresh, max_global, max_line):
97ec271a513f Bunch of changes
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
77 pixels = im.getdata()
97ec271a513f Bunch of changes
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
78 (width, height) = im.size
97ec271a513f Bunch of changes
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
79 colors = get_color_info(pixels, xrange(0, height * width), trans_thresh)
5
a74776d80f95 Fix a bug in selecting the best match color when an exact match is unavailable. Add some more options for choosing a line palette
Mike Pavone <pavone@retrodev.com>
parents: 3
diff changeset
80 print len(colors), 'distinct 9-bit colors in image'
3
97ec271a513f Bunch of changes
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
81 glob_pal = {}
5
a74776d80f95 Fix a bug in selecting the best match color when an exact match is unavailable. Add some more options for choosing a line palette
Mike Pavone <pavone@retrodev.com>
parents: 3
diff changeset
82 print 'Static Palette:'
3
97ec271a513f Bunch of changes
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
83 for idx in xrange(0, min(max_global, len(colors))):
97ec271a513f Bunch of changes
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
84 (count, color) = colors[idx]
5
a74776d80f95 Fix a bug in selecting the best match color when an exact match is unavailable. Add some more options for choosing a line palette
Mike Pavone <pavone@retrodev.com>
parents: 3
diff changeset
85 print str(idx) + ':', color, '(used', count, 'times)'
3
97ec271a513f Bunch of changes
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
86 glob_pal[color] = idx
97ec271a513f Bunch of changes
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
87 line_pals = []
97ec271a513f Bunch of changes
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
88 if max_global < len(colors):
97ec271a513f Bunch of changes
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
89 for line in xrange(0, height):
97ec271a513f Bunch of changes
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
90 linestart = line * width
5
a74776d80f95 Fix a bug in selecting the best match color when an exact match is unavailable. Add some more options for choosing a line palette
Mike Pavone <pavone@retrodev.com>
parents: 3
diff changeset
91 #linecolors = get_color_info(pixels, xrange(linestart, linestart+width), trans_thresh, glob_pal)
a74776d80f95 Fix a bug in selecting the best match color when an exact match is unavailable. Add some more options for choosing a line palette
Mike Pavone <pavone@retrodev.com>
parents: 3
diff changeset
92 #linecolors = get_color_info_error(pixels, xrange(linestart, linestart+width), trans_thresh, glob_pal)
a74776d80f95 Fix a bug in selecting the best match color when an exact match is unavailable. Add some more options for choosing a line palette
Mike Pavone <pavone@retrodev.com>
parents: 3
diff changeset
93 linecolors = get_color_info_both(pixels, xrange(linestart, linestart+width), trans_thresh, glob_pal)
3
97ec271a513f Bunch of changes
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
94 line_pal = {}
97ec271a513f Bunch of changes
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
95 for idx in xrange(0, min(max_line, len(linecolors))):
97ec271a513f Bunch of changes
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
96 (count, color) = linecolors[idx]
5
a74776d80f95 Fix a bug in selecting the best match color when an exact match is unavailable. Add some more options for choosing a line palette
Mike Pavone <pavone@retrodev.com>
parents: 3
diff changeset
97 line_pal[color] = idx + max_global
3
97ec271a513f Bunch of changes
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
98 line_pals.append(line_pal)
97ec271a513f Bunch of changes
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
99 return (glob_pal, line_pals, max_global, max_line)
97ec271a513f Bunch of changes
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
100
97ec271a513f Bunch of changes
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
101 def color_dist(a, b):
97ec271a513f Bunch of changes
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
102 (ra, ga, ba) = a
97ec271a513f Bunch of changes
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
103 (rb, gb, bb) = b
97ec271a513f Bunch of changes
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
104 return (ra-rb)**2 + (ga-gb)**2 + (ba-bb)**2
97ec271a513f Bunch of changes
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
105
5
a74776d80f95 Fix a bug in selecting the best match color when an exact match is unavailable. Add some more options for choosing a line palette
Mike Pavone <pavone@retrodev.com>
parents: 3
diff changeset
106 def best_match(gpixel, pals):
a74776d80f95 Fix a bug in selecting the best match color when an exact match is unavailable. Add some more options for choosing a line palette
Mike Pavone <pavone@retrodev.com>
parents: 3
diff changeset
107 bestdist = color_dist((0,0,0), (15, 15, 15))
a74776d80f95 Fix a bug in selecting the best match color when an exact match is unavailable. Add some more options for choosing a line palette
Mike Pavone <pavone@retrodev.com>
parents: 3
diff changeset
108 bestpalidx = 0
a74776d80f95 Fix a bug in selecting the best match color when an exact match is unavailable. Add some more options for choosing a line palette
Mike Pavone <pavone@retrodev.com>
parents: 3
diff changeset
109 bestcolor = (0,0,0)
a74776d80f95 Fix a bug in selecting the best match color when an exact match is unavailable. Add some more options for choosing a line palette
Mike Pavone <pavone@retrodev.com>
parents: 3
diff changeset
110 for i in xrange(0, len(pals)):
a74776d80f95 Fix a bug in selecting the best match color when an exact match is unavailable. Add some more options for choosing a line palette
Mike Pavone <pavone@retrodev.com>
parents: 3
diff changeset
111 pal = pals[i]
a74776d80f95 Fix a bug in selecting the best match color when an exact match is unavailable. Add some more options for choosing a line palette
Mike Pavone <pavone@retrodev.com>
parents: 3
diff changeset
112 for cur in pal:
a74776d80f95 Fix a bug in selecting the best match color when an exact match is unavailable. Add some more options for choosing a line palette
Mike Pavone <pavone@retrodev.com>
parents: 3
diff changeset
113 curdist = color_dist(gpixel, cur)
a74776d80f95 Fix a bug in selecting the best match color when an exact match is unavailable. Add some more options for choosing a line palette
Mike Pavone <pavone@retrodev.com>
parents: 3
diff changeset
114 if curdist < bestdist:
a74776d80f95 Fix a bug in selecting the best match color when an exact match is unavailable. Add some more options for choosing a line palette
Mike Pavone <pavone@retrodev.com>
parents: 3
diff changeset
115 bestdist = curdist
a74776d80f95 Fix a bug in selecting the best match color when an exact match is unavailable. Add some more options for choosing a line palette
Mike Pavone <pavone@retrodev.com>
parents: 3
diff changeset
116 bestpalidx = pal[cur]
a74776d80f95 Fix a bug in selecting the best match color when an exact match is unavailable. Add some more options for choosing a line palette
Mike Pavone <pavone@retrodev.com>
parents: 3
diff changeset
117 bestcolor = cur
a74776d80f95 Fix a bug in selecting the best match color when an exact match is unavailable. Add some more options for choosing a line palette
Mike Pavone <pavone@retrodev.com>
parents: 3
diff changeset
118 return (bestpalidx, bestcolor)
a74776d80f95 Fix a bug in selecting the best match color when an exact match is unavailable. Add some more options for choosing a line palette
Mike Pavone <pavone@retrodev.com>
parents: 3
diff changeset
119
3
97ec271a513f Bunch of changes
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
120 def trans_image(im, trans_thresh, pal):
97ec271a513f Bunch of changes
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
121 (global_pal, line_pals, _, _) = pal
97ec271a513f Bunch of changes
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
122 pixels = im.getdata()
97ec271a513f Bunch of changes
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
123 (width, height) = im.size
97ec271a513f Bunch of changes
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
124 gpixels = []
97ec271a513f Bunch of changes
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
125 A = 255
97ec271a513f Bunch of changes
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
126 x = 0
97ec271a513f Bunch of changes
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
127 y = 0
97ec271a513f Bunch of changes
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
128 for pixel in pixels:
97ec271a513f Bunch of changes
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
129 if x == width:
97ec271a513f Bunch of changes
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
130 x = 0
97ec271a513f Bunch of changes
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
131 y += 1
97ec271a513f Bunch of changes
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
132 if width % 8:
97ec271a513f Bunch of changes
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
133 for i in xrange(0, 8-(width%8)):
97ec271a513f Bunch of changes
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
134 gpixels.append(0)
97ec271a513f Bunch of changes
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
135 if len(pixel) == 4:
97ec271a513f Bunch of changes
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
136 (R, G, B, A) = pixel
97ec271a513f Bunch of changes
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
137 else:
97ec271a513f Bunch of changes
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
138 (R, G, B) = pixel
97ec271a513f Bunch of changes
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
139 if A > trans_thresh:
97ec271a513f Bunch of changes
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
140 gpixel = (gchannel(R), gchannel(G), gchannel(B))
97ec271a513f Bunch of changes
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
141 if gpixel in global_pal:
97ec271a513f Bunch of changes
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
142 gpixels.append(global_pal[gpixel])
97ec271a513f Bunch of changes
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
143 elif gpixel in line_pals[y]:
5
a74776d80f95 Fix a bug in selecting the best match color when an exact match is unavailable. Add some more options for choosing a line palette
Mike Pavone <pavone@retrodev.com>
parents: 3
diff changeset
144 gpixels.append(line_pals[y][gpixel])
3
97ec271a513f Bunch of changes
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
145 else:
5
a74776d80f95 Fix a bug in selecting the best match color when an exact match is unavailable. Add some more options for choosing a line palette
Mike Pavone <pavone@retrodev.com>
parents: 3
diff changeset
146 bestpal,color = best_match(gpixel, (global_pal, line_pals[y]))
3
97ec271a513f Bunch of changes
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
147 gpixels.append(bestpal)
97ec271a513f Bunch of changes
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
148 else:
97ec271a513f Bunch of changes
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
149 gpixels.append(0)
97ec271a513f Bunch of changes
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
150 x += 1
97ec271a513f Bunch of changes
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
151 if width % 8:
97ec271a513f Bunch of changes
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
152 for i in xrange(0, 8-(width%8)):
97ec271a513f Bunch of changes
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
153 gpixels.append(0)
97ec271a513f Bunch of changes
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
154 width += 8-(width%8)
97ec271a513f Bunch of changes
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
155 if height % 8:
97ec271a513f Bunch of changes
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
156 for y in xrange(0, 8-(height%8)):
97ec271a513f Bunch of changes
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
157 for x in xrange(0, width):
97ec271a513f Bunch of changes
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
158 gpixels.append(0)
97ec271a513f Bunch of changes
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
159 height += 8-(height%8)
97ec271a513f Bunch of changes
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
160
97ec271a513f Bunch of changes
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
161 return (width, height, gpixels)
97ec271a513f Bunch of changes
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
162
97ec271a513f Bunch of changes
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
163 def appendword(b, word):
97ec271a513f Bunch of changes
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
164 b.append(word >> 8)
97ec271a513f Bunch of changes
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
165 b.append(word & 0xff)
97ec271a513f Bunch of changes
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
166
97ec271a513f Bunch of changes
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
167 def to_tiles(palpix):
97ec271a513f Bunch of changes
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
168 (width, height, pixels) = palpix
97ec271a513f Bunch of changes
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
169 b = bytearray()
97ec271a513f Bunch of changes
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
170 cwidth = width/8
97ec271a513f Bunch of changes
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
171 cheight = height/8
97ec271a513f Bunch of changes
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
172 words = len(pixels)/4
97ec271a513f Bunch of changes
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
173 appendword(b, words)
97ec271a513f Bunch of changes
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
174 appendword(b, cwidth)
97ec271a513f Bunch of changes
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
175 appendword(b, cheight)
97ec271a513f Bunch of changes
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
176
97ec271a513f Bunch of changes
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
177 for cy in xrange(0, cheight):
97ec271a513f Bunch of changes
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
178 ystart = cy*8*width
97ec271a513f Bunch of changes
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
179 for cx in xrange(0, cwidth):
97ec271a513f Bunch of changes
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
180 startoff = (cx*8) + ystart
97ec271a513f Bunch of changes
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
181 for row in xrange(0, 8):
97ec271a513f Bunch of changes
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
182 rowoff = startoff + row*width
97ec271a513f Bunch of changes
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
183 for bytecol in xrange(0, 4):
97ec271a513f Bunch of changes
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
184 boff = bytecol * 2 + rowoff
97ec271a513f Bunch of changes
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
185 #print 'boff:', boff, 'len(pixels)', len(pixels), 'cx', cx, 'cy', cy, 'cwidth', cwidth, 'cheight', cheight
97ec271a513f Bunch of changes
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
186 #print 'pixels[boff]:', pixels[boff]
97ec271a513f Bunch of changes
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
187 b.append(pixels[boff] << 4 | pixels[boff+1])
97ec271a513f Bunch of changes
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
188 return b
97ec271a513f Bunch of changes
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
189
97ec271a513f Bunch of changes
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
190 def add_pal_entries(tiles, pal):
97ec271a513f Bunch of changes
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
191 (global_pal, line_pals, max_global, max_line) = pal
97ec271a513f Bunch of changes
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
192 pal_list = [(0, 0, 0)] * max_global
97ec271a513f Bunch of changes
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
193 for entry in global_pal:
97ec271a513f Bunch of changes
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
194 pal_list[global_pal[entry]] = entry
97ec271a513f Bunch of changes
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
195 for entry in pal_list:
97ec271a513f Bunch of changes
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
196 (R, G, B) = entry
97ec271a513f Bunch of changes
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
197 tiles.append(B)
97ec271a513f Bunch of changes
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
198 tiles.append(G << 4 | R)
97ec271a513f Bunch of changes
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
199 for line in line_pals:
97ec271a513f Bunch of changes
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
200 pal_list = [(0, 0, 0)] * max_line
97ec271a513f Bunch of changes
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
201 for entry in line:
5
a74776d80f95 Fix a bug in selecting the best match color when an exact match is unavailable. Add some more options for choosing a line palette
Mike Pavone <pavone@retrodev.com>
parents: 3
diff changeset
202 pal_list[line[entry]-max_global] = entry
3
97ec271a513f Bunch of changes
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
203 for entry in pal_list:
97ec271a513f Bunch of changes
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
204 (R, G, B) = entry
97ec271a513f Bunch of changes
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
205 tiles.append(B)
97ec271a513f Bunch of changes
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
206 tiles.append(G << 4 | R)
97ec271a513f Bunch of changes
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
207
97ec271a513f Bunch of changes
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
208
97ec271a513f Bunch of changes
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
209
97ec271a513f Bunch of changes
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
210 def main(argv):
97ec271a513f Bunch of changes
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
211 if len(argv) < 3:
97ec271a513f Bunch of changes
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
212 print "Not enough arguments"
97ec271a513f Bunch of changes
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
213 return
97ec271a513f Bunch of changes
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
214 fname = argv[1]
8
a049de420cc1 Make number of static and dynamic colors command line arguments to img2tiles.py
Mike Pavone <pavone@retrodev.com>
parents: 5
diff changeset
215 static_colors = 8
a049de420cc1 Make number of static and dynamic colors command line arguments to img2tiles.py
Mike Pavone <pavone@retrodev.com>
parents: 5
diff changeset
216 dynamic_colors = 8
a049de420cc1 Make number of static and dynamic colors command line arguments to img2tiles.py
Mike Pavone <pavone@retrodev.com>
parents: 5
diff changeset
217 if len(argv) > 3:
a049de420cc1 Make number of static and dynamic colors command line arguments to img2tiles.py
Mike Pavone <pavone@retrodev.com>
parents: 5
diff changeset
218 static_colors = int(argv[3])
a049de420cc1 Make number of static and dynamic colors command line arguments to img2tiles.py
Mike Pavone <pavone@retrodev.com>
parents: 5
diff changeset
219 dynamic_colors = min(dynamic_colors, 16-static_colors)
a049de420cc1 Make number of static and dynamic colors command line arguments to img2tiles.py
Mike Pavone <pavone@retrodev.com>
parents: 5
diff changeset
220 if len(argv) > 4:
a049de420cc1 Make number of static and dynamic colors command line arguments to img2tiles.py
Mike Pavone <pavone@retrodev.com>
parents: 5
diff changeset
221 dynamic_colors = int(argv[4])
a049de420cc1 Make number of static and dynamic colors command line arguments to img2tiles.py
Mike Pavone <pavone@retrodev.com>
parents: 5
diff changeset
222 if dynamic_colors + static_colors > 16:
a049de420cc1 Make number of static and dynamic colors command line arguments to img2tiles.py
Mike Pavone <pavone@retrodev.com>
parents: 5
diff changeset
223 print "No more than 16 combined dynamic and static colors are allowed"
a049de420cc1 Make number of static and dynamic colors command line arguments to img2tiles.py
Mike Pavone <pavone@retrodev.com>
parents: 5
diff changeset
224 return
3
97ec271a513f Bunch of changes
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
225 im = Image.open(fname)
8
a049de420cc1 Make number of static and dynamic colors command line arguments to img2tiles.py
Mike Pavone <pavone@retrodev.com>
parents: 5
diff changeset
226 pal = make_palette(im, threshold, static_colors, dynamic_colors)
3
97ec271a513f Bunch of changes
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
227 palpix = trans_image(im, threshold, pal)
97ec271a513f Bunch of changes
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
228 tiles = to_tiles(palpix)
97ec271a513f Bunch of changes
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
229 bits = add_pal_entries(tiles, pal)
97ec271a513f Bunch of changes
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
230 out = open(argv[2], 'wb')
97ec271a513f Bunch of changes
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
231 out.write(tiles)
97ec271a513f Bunch of changes
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
232
97ec271a513f Bunch of changes
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
233 if __name__ == '__main__':
97ec271a513f Bunch of changes
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
234 import sys
97ec271a513f Bunch of changes
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
235 main(sys.argv)