forked from kentfield/knitting_machine
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathinsertpatternMultiColour.py
More file actions
469 lines (374 loc) · 12.6 KB
/
insertpatternMultiColour.py
File metadata and controls
469 lines (374 loc) · 12.6 KB
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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
#!/usr/bin/env python
# Copyright 2009 Steve Conklin
# steve at conklinhouse dot com
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
import sys
import brother
from PIL import Image
import array
TheImage = None
##################
def roundeven(val):
return (val+(val%2))
def roundeight(val):
if val % 8:
return val + (8-(val%8))
else:
return val
def roundfour(val):
if val % 4:
return val + (4-(val%4))
else:
return val
def nibblesPerRow(stitches):
# there are four stitches per nibble
# each row is nibble aligned
return(roundfour(stitches)/4)
def bytesPerPattern(stitches, rows):
nibbs = rows * nibblesPerRow(stitches)
bytes = roundeven(nibbs)/2
return bytes
def bytesForMemo(rows):
bytes = roundeven(rows)/2
return bytes
##############
version = '1.0'
if len(sys.argv) < 6:
print 'Usage: %s oldbrotherfile pattern# image.png numberofcolors newbrotherfile doubleHeight|offsetRows|blankSecondPass|ditheredRows' % sys.argv[0]
sys.exit()
bf = brother.brotherFile(sys.argv[1])
pattnum = sys.argv[2]
imagefile = sys.argv[3]
maxcolors = sys.argv[4]
multifile = sys.argv[3]+'-multi.txt'
patternalgorithm = 'offsetRows'
if(len(sys.argv) == 7):
patternalgorithm = sys.argv[6]
skippatgen = False
if(len(sys.argv) == 8):
skippatgen = True
allPatterns = bf.getPatterns()
TheImage = Image.open(imagefile)
TheImage.load()
im_size = TheImage.size
width = im_size[0]
print "width:",width
height = im_size[1]
print "height:", height
# Check the image has the correct number of colors we're expecting
# It's so easy when preparing the image to miss a shade off here and there
x = 0
y = 0
z = 0
hasColor = False
colors = []
colorSymbols = [' ','*','^','#','&','%','@','$','-','+','=']
symbol = ' '
imageConvertLines = []
imageConvertRow = []
while x < width:
value = TheImage.getpixel((x,y))
hasColor = False
z = 0
while z < len(colors):
if value == colors[z]:
hasColor = True
break
z = z+1
if hasColor == False:
colors.append(value)
symbol = colorSymbols[z]
imageConvertRow.append(z+1)
sys.stdout.write(str(symbol))
sys.stdout.write(' ')
x = x+1
if x == width: #did we hit the end of the line?
imageConvertLines.append(imageConvertRow)
imageConvertRow = []
y = y+1
x = 0
print " "
if y == height:
break
if len(colors) != int(maxcolors):
print 'ERROR: Found',len(colors),'colors when there should be',int(maxcolors),'!\nAborting.\n\n'
sys.exit()
# Now we have a int for each color, convert imagefile to multifile.
# Create this interum state because it allows you to alter the order of colors and lines by hand if you wish
# These algorithms are benchmarked in my blog post xxxxx
# Original algorithm - double height
# Always do a line of knitting for every colour even if it's not present in the row (ensures the thickness of the knitting is consistent)
def doubleHeight():
y = height-1
writeLine = ''
multiOutfile = open(multifile, 'wb')
while y > -1:
#print imageConvertLines[y]
z = 0
while z < len(colors):
writeLine = str(z+1)+'#'
x = 0
while x < width:
if imageConvertLines[y][x] == (z+1):
writeLine = writeLine+'1'
else:
writeLine = writeLine+'0'
x = x+1
multiOutfile.write(writeLine+'\n')
z = z+1
y = y-1
multiOutfile.close()
# Alternative algorithm - offset
# This uses the second pass of the last colour in a row as an opportunity to start the next row
# just like the KRC variation switch 7 on the knitting machine board
def offsetRows():
y = height-1
writeLine = ''
write2ndLine = ''
r2l = False
knittedRow = 0
multiOutfile = open(multifile, 'wb')
currentColour = 0
while y > -1:
#print imageConvertLines[y]
z = 0
knittedRow = 0
while True:
writeLine = str(currentColour+1)+'#'
write2ndLine = writeLine
x = 0
while x < width:
if imageConvertLines[y][x] == (currentColour+1):
writeLine = writeLine+'1'
knittedRow = knittedRow+1
else:
writeLine = writeLine+'0'
write2ndLine = write2ndLine+'0'
x = x+1
multiOutfile.write(writeLine+'\n')
z = z+1
if knittedRow == width: # finished this row, break before we go to the next colour and focus on the next row
r2l = not r2l
if not r2l:
currentColour = currentColour+1
if currentColour == len(colors):
currentColour = 0
#multiOutfile.write('0# width! '+str(r2l)+'\n')
break
if not r2l:
multiOutfile.write(write2ndLine+'\n')
else:
r2l = False
currentColour = currentColour+1
if currentColour == len(colors):
currentColour = 0
if z == len(colors):
#multiOutfile.write('0# colors!\n')
break
y = y-1
if r2l:
multiOutfile.write(write2ndLine+'\n')
multiOutfile.close()
# Alternative algorithm - blank second pass
# Don't knit the main bed when returning to the colour changer
def blankSecondPass():
y = height-1
writeLine = ''
write2ndLine = ''
multiOutfile = open(multifile, 'wb')
while y > -1:
#print imageConvertLines[y]
z = 0
while z < len(colors):
writeLine = str(z+1)+'#'
write2ndLine = writeLine
x = 0
while x < width:
if imageConvertLines[y][x] == (z+1):
writeLine = writeLine+'1'
else:
writeLine = writeLine+'0'
write2ndLine = write2ndLine+'0'
x = x+1
multiOutfile.write(writeLine+'\n')
multiOutfile.write(write2ndLine+'\n')
z = z+1
y = y-1
multiOutfile.close()
# Alternative algorithm - dithered
# This breaks up the allocation of the colours to every second instance of the colour in the first pass then every other instance of the colour in the second pass
def ditheredRows():
y = height-1
writeLine = ''
write2ndLine = ''
multiOutfile = open(multifile, 'wb')
while y > -1:
#print imageConvertLines[y]
z = 0
while z < len(colors):
writeLine = str(z+1)+'#'
write2ndLine = writeLine
x = 0
while x < width:
if imageConvertLines[y][x] == (z+1) and x%2 == 0:
writeLine = writeLine+'1'
else:
writeLine = writeLine+'0'
if imageConvertLines[y][x] == (z+1) and x%2 != 0:
write2ndLine = write2ndLine+'1'
else:
write2ndLine = write2ndLine+'0'
x = x+1
multiOutfile.write(writeLine+'\n')
multiOutfile.write(write2ndLine+'\n')
z = z+1
y = y-1
multiOutfile.close()
if not skippatgen:
if patternalgorithm == 'doubleHeight':
doubleHeight()
elif patternalgorithm == 'offsetRows':
offsetRows()
elif patternalgorithm == 'blankSecondPass':
blankSecondPass()
elif patternalgorithm == 'ditheredRows':
ditheredRows()
# find the program entry
thePattern = None
for pat in allPatterns:
if (int(pat["number"]) == int(pattnum)):
#print "found it!"
thePattern = pat
if (thePattern == None):
print "Pattern #",pattnum,"not found!"
exit(0)
# debugging stuff here
x = 0
y = 0
# load multi colour file
lines = [line.strip() for line in open(multifile)]
colours = []
# ok got a bank, now lets figure out how big this thing we want to insert is
width = len(lines[0])-2 #2 chars used for memo data, that doesn't count toward the width of the pattern
#print "width:",width
height = len(lines)
print "Pattern height:", height
while x < height:
colours.append(int(lines[x][0]))
lines[x] = lines[x][2:]
sys.stdout.write(str(colours[x]))
sys.stdout.write(' ')
sys.stdout.write(str(lines[x]))
sys.stdout.write('\n\r')
x = x+1
x = 0
# debugging stuff done
# now to make the actual, yknow memo+pattern data
# append colours to the memo data
memoentry = []
r = 0
for r in range(bytesForMemo(height)):
if(r*2+1 < len(colours)):
#print hex(colours[r*2 + 1] << 4 | colours[r*2])
memoentry.append(colours[r*2 + 1] << 4 | colours[r*2])
r = r+1
#pad for odd number of rows
if(len(colours) % 2 != 0):
memoentry.append(colours[len(colours)-1])
# now for actual real live pattern data!
pattmemnibs = []
allrows = []
for r in range(height):
row = [] # we'll chunk in bits and then put em into nibbles
for s in range(width):
value = lines[r][width-s-1]
row.append(int(value))
allrows.append(row)
#print row
# turn it into nibz
for r in range(height):
row = allrows[r]
for s in range(roundfour(width) / 4):
n = 0
for nibs in range(4):
#print "row size = ", len(row), "index = ",s*4+nibs
if (len(row) == (s*4+nibs)):
break # padding!
if (row[s*4 + nibs]):
n |= 1 << nibs
pattmemnibs.append(n)
#print hex(n),
if (len(pattmemnibs) % 2):
# odd nibbles, buffer to a byte
pattmemnibs.append(0x0)
print len(pattmemnibs), "nibbles of data"
# turn into bytes
pattmem = []
for i in range (len(pattmemnibs) / 2):
pattmem.append( pattmemnibs[i*2] | (pattmemnibs[i*2 + 1] << 4))
#print map(hex, pattmem)
# whew.
# now to insert this data into the file
# now we have to figure out the -end- of the last pattern is
endaddr = 0x6df
beginaddr = thePattern["pattend"]
endaddr = beginaddr + bytesForMemo(height) + len(pattmem)
print "beginning will be at ", hex(beginaddr), "end at", hex(endaddr)
#print "Current header data"
#for i in thePattern['header']:
# print '0x%02X' % ord(i)
for i in range(len(thePattern['header'])):
thePattern['header'][i] = ord(thePattern['header'][i])
# we need to change the mode from 4 to 8 (this will turn off reading the mylar sheet as we knit!)
thePattern['header'][5] = 8 << 4 | 9
# and while we're here we should change the width and height too :)
strHeight = "%03d" % height
strWidth = "%03d" % width
thePattern['header'][2] = int(strHeight[0]) << 4 | int(strHeight[1])
thePattern['header'][3] = int(strHeight[2]) << 4 | int(strWidth[0])
thePattern['header'][4] = int(strWidth[1]) << 4 | int(strWidth[2])
#print thePattern['header'][5]
#print '0x%02X' % (8 << 4 | 9)
# look at the header data
print "New header:",
for i in thePattern['header']:
print '0x%02X' % i,
# write back the header
seek = 0
for i in range(len(thePattern['header'])):
bf.setIndexedByte(seek, thePattern['header'][i])
seek = seek + 1
# Note - It's note certain that in all cases this collision test is needed. What's happening
# when you write below this address (as the pattern grows downward in memory) in that you begin
# to overwrite the pattern index data that starts at low memory. Since you overwrite the info
# for highest memory numbers first, you may be able to get away with it as long as you don't
# attempt to use higher memories.
# Steve
if beginaddr < 0x2BC:
print "sorry, this will collide with the pattern entry data since %s is < 0x2BC!" % hex(beginaddr)
exit
# write the memo and pattern entry from the -end- to the -beginning- (up!)
for i in range(len(memoentry)):
bf.setIndexedByte(endaddr, memoentry[i])
endaddr -= 1
for i in range(len(pattmem)):
bf.setIndexedByte(endaddr, pattmem[i])
endaddr -= 1
# push the data to a file
outfile = open(sys.argv[5], 'wb')
d = bf.getFullData()
outfile.write(d)
outfile.close()