-
Notifications
You must be signed in to change notification settings - Fork 3
/
beads.py
executable file
·217 lines (180 loc) · 7.14 KB
/
beads.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
#!/usr/bin/env python
# ----------------------------------------------------------------------------
# "THE BEER-WARE LICENSE" (Revision 42):
# <kn@insecurity.dk> wrote this file. As long as you retain this notice you
# can do whatever you want with this stuff. If we meet some day, and you think
# this stuff is worth it, you can buy me a beer in return. Kim Nielsen
# ----------------------------------------------------------------------------
import sys, time, getopt, math
from PIL import Image, ImageFilter, ImageDraw
from colormath.color_diff import delta_e_cie2000
from colormath.color_objects import sRGBColor, LabColor
from colormath.color_conversions import convert_color
from datetime import timedelta
from multiprocessing import Pool
import multiprocessing
__version__ = 2.0
class grid():
def __init__(self, region, box, xgrid=16,ygrid=16,fastcolor=True, beadlist={}):
self.region = region
self.box = box
self.fastcolor=fastcolor
self.beadlist=beadlist
self.xgrid = xgrid
self.ygrid = ygrid
self.color = (0,0,0)
""" help """
def help():
print "Beads version %s" % __version__
print "Option:"
print "-h\t this help"
print "-q\t be quiet and do not display progress"
print "-f\t Use a fast way to find the best suited bead. Only used when using a list of beadcolors"
print "-i\t Specify a input file"
print "-x\t Set xgrid facter. Default set to 16"
print "-y\t Set ygrid facter. Default set to 16"
print "-b\t Specify a input file for beadcolors"
print "-o\t Output file for final product"
print "-m\t Mode. Default drawing is a box but if you add beads it will draw with beads"
print "-c\t background color. Default is 0,0,0 (Black)"
print "-n\t Number of CPUs to use. Default is all of them"
print "\nExample: ./beads.py -i donald.jpg -b hama.txt -f -o donald_hama.jpg"
sys.exit(0)
""" Fast but inaccurate way to find the distance between 2 colors """
def colordistance(color1, color2):
return abs( color1[0] - color2[0] ) + abs( color1[1] - color2[1] ) + abs ( color1[2] - color2[2] )
def worker(gridwork):
ret = []
predefinedcolors = False
if len(gridwork.beadlist):
predefinedcolors = True
ravg, gavg, bavg = 0, 0, 0
blockpixels = gridwork.xgrid*gridwork.ygrid
offset=0
for y in range(0,gridwork.ygrid):
for x in range(0,gridwork.xgrid):
travg, tgavg, tbavg = gridwork.region[offset]
ravg+=travg
gavg+=tgavg
bavg+=tbavg
offset+=1
nr, ng, nb = (ravg/blockpixels), (gavg/blockpixels), (bavg/blockpixels)
if predefinedcolors:
index = gridwork.beadlist.keys()[0]
if gridwork.fastcolor:
closest = colordistance((nr, ng, nb), (gridwork.beadlist[index][0], gridwork.beadlist[index][1], gridwork.beadlist[index][2]))
else:
rgblab = convert_color(sRGBColor( nr, ng, nb), LabColor)
closest = delta_e_cie2000(rgblab, convert_color(sRGBColor( gridwork.beadlist[index][0], gridwork.beadlist[index][1], gridwork.beadlist[index][2] ), LabColor))
for key in gridwork.beadlist:
hr = gridwork.beadlist[key][0]
hg = gridwork.beadlist[key][1]
hb = gridwork.beadlist[key][2]
if fastcolor:
delta = colordistance((nr, ng, nb), (hr, hg, hb))
else:
beadlab = convert_color(sRGBColor( hr, hg, hb ), LabColor)
delta = delta_e_cie2000(rgblab, beadlab)
if delta < closest:
index = key
closest = delta
nr, ng, nb = gridwork.beadlist[index][0], gridwork.beadlist[index][1], gridwork.beadlist[index][2]
gridwork.color = (nr, ng, nb)
ret.append((gridwork.color,gridwork.box))
return ret
def main(image,bgcolor,xgrid,ygrid,fastcolor,beadlist,numworkers=4,mode='box',output=''):
origimage = Image.open(image)
if origimage.mode!='RGB':
print "Mode: %s not supported" % (origimage.mode)
sys.exit(1)
""" Convert to 256 colors and back to 24bit to get the RGB values """
convertedimage = origimage.convert('P', palette=Image.ADAPTIVE).convert('RGB')
""" Image but be divideable with xgrid/ygrid """
optimalx = int(origimage.width/xgrid)*xgrid
optimaly = int(origimage.height/ygrid)*ygrid
pegboard = Image.new(origimage.mode, (optimalx,optimaly), bgcolor)
jobqueue = []
ystart = 0
blocks = 0
for y in xrange(0,optimaly,ygrid):
xstart = 0
for x in xrange(0,optimalx,xgrid):
box = (x, y, x+xgrid, y+ygrid)
region = convertedimage.crop(box)
job = grid(list(region.getdata()), box, xgrid, ygrid, fastcolor, beadlist)
jobqueue.append(job)
pool = Pool(processes=numworkers)
result = pool.map(worker, jobqueue)
for x in result:
color = x[0][0]
box = x[0][1]
regionout = Image.new(convertedimage.mode, (xgrid,ygrid), bgcolor)
if mode=='beads':
d = ImageDraw.Draw(regionout)
r=0
d.ellipse((0-r, 0-r, xgrid+r, ygrid+r), fill=color)
r=-1*(math.sqrt(xgrid*2))
d.ellipse((0-r, 0-r, xgrid+r, ygrid+r), fill=bgcolor)
else:
for yr in xrange(0,ygrid):
for xr in xrange(0,xgrid):
regionout.putpixel((xr,yr),color)
pegboard.paste(regionout, box)
if mode!='beads':
d = ImageDraw.Draw(pegboard)
for y in xrange(0,pegboard.height,ygrid):
for x in xrange(0,pegboard.width,xgrid):
d.line((x,y,pegboard.height,y),fill=bgcolor)
d.line((x,0,x,pegboard.height),fill=bgcolor)
d.line((pegboard.width-1,0,pegboard.width-1,pegboard.height),fill=bgcolor)
if output=='':
pegboard.show()
else:
if output!=image:
pegboard.save(output)
if __name__ == "__main__":
xgrid, ygrid = 16, 16
image = ''
output = ''
bgcolor = (0,0,0)
bgcolortmp = ''
progress = True
fastcolor = False
beadsfile = ''
mode = 'box'
numworkers = multiprocessing.cpu_count()
argv = sys.argv[1:]
opts, args = getopt.getopt(argv,"hqfi:x:y:b:o:m:c:n:",[])
for opt, arg in opts:
if opt =='-h':
help()
if opt in ('-q'):
progress = False
if opt in ('-x'):
xgrid = int(arg)
if opt in ('-y'):
ygrid = int(arg)
if opt in ('-f'):
fastcolor = True
if opt in ('-b'):
beadsfile = arg
if opt in ('-i'):
image = arg
if opt in ('-m'):
mode = arg
if opt in ('-n'):
numworkers = int(arg)
if opt in ('-c'):
bgcolortmp = arg
tmp = bgcolortmp.split(',')
bgcolor = (int(tmp[0]), int(tmp[1]), int(tmp[2]))
if opt in ('-o'):
output = arg
if beadsfile=='':
beadslist = {}
else:
beadslist = eval(open(beadsfile).read())
if image=='':
print "No input image specified\n"
help()
main(image,bgcolor,xgrid,ygrid,fastcolor,beadslist,numworkers,mode,output)