-
Notifications
You must be signed in to change notification settings - Fork 1
/
mote_light_painting.py
executable file
·588 lines (479 loc) · 21.9 KB
/
mote_light_painting.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
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
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
#!/usr/bin/python
from __future__ import print_function
try:
import thread
except:
from threading import Thread
import os
try:
import tkinter as tk
import tkinter.ttk as ttk
except:
import Tkinter as tk
import pygubu
from PIL import Image #PILLOW
from tkColorChooser import askcolor
from tkFileDialog import askopenfilename
import webcolors
import sys
import time
import functools
import random
import logging
import os
import subprocess
import sys
import gtk
import re
import gphoto2 as gp
CURRENT_DIR = os.path.abspath(os.path.dirname(__file__))
MODE_IMAGE = 1
MODE_COLOR = 2
MODE_GRADIENT = 3
MODE_RANDOM = 4
#gphoto2_command = 'gphoto2 --capture-image --filename "../LightPaintings/%Y%m%d-%H%M%S-%03n.%C"'
gphoto2_command = '''gphoto2 --set-config capturetarget=1 --keep --capture-image-and-download --filename """../LightPaintings/%Y%m%d-%H%M%S-%03n.%C"""'''
class MyApplication:
def __init__(self):
#1: Create a builder
self.builder = builder = pygubu.Builder()
self.doQuick = False
self.timeSlices = 320
self.motePixelInSceenPixels = 1
self.graphWidth = self.timeSlices
self.cameraLag = 3
self.currentColumn = 0
self.simulate = False
self.animate = False
self.completeRepeats = 0
self.motePixelInCm = 1
self.mode = MODE_IMAGE
self.color = (255, 0, 0)
self.colorend = (0, 0, 255)
self.bCameraBusy = False
self.bControlCamera = False
self.bPaintFromLeft = True
self.bFacingScreen = True
self.bReverseImage = False
self.bFlipVertical = False
self.bGradient = False
self.bPaintWhite = False
self.bPaintBlack = False
self.bTween = False
self.bLines = False
self.bSpeckles = False
self.bRandomAcrossRange = False
self.iDuration = 5
self.iRepeats = 1
self.iDelay = 0
screen_width = gtk.gdk.screen_width()
screen_height = gtk.gdk.screen_height()
#2: Load an ui file
builder.add_from_file(os.path.join(CURRENT_DIR, 'main_repeats.ui'))
self.motePixelInSceenPixels = 2
#3: Create the toplevel widget.
self.mainwindow = builder.get_object('mainwindow')
self.canPreview = builder.get_object('canPreview')
self.msgMessage = builder.get_object('msgMessage')
self.scaDelay = builder.get_object('scaDelay')
self.scaDuration = builder.get_object('scaDuration')
self.connectToMote()
self.iPixels = len(self.yToStick)
self.aColourGrid = []
self.aRandomGrid = []
self.updateControls()
self.filename = "images/Spectrum Vertical.png"
self.loadImage()
self.makeRandom()
builder.connect_callbacks(self)
def beep(self, duration = 0.1):
freq = 440 # Hz
os.system('play --no-show-progress --null --channels 1 synth %s sine %f' % (duration, freq))
def addStick(self, channel, up=True, length=16, gammacorrect=True): #from top
if not self.simulate :
self.mote.configure_channel(channel, length, gammacorrect)
for i in range(length):
if up:
offset = length - 1 - i
else:
offset = i
self.yToStick.append((channel, offset))
def connectToMote(self):
self.showMessage("Connecting")
try:
from mote import Mote
self.mote = Mote()
self.simulate = False
self.showMessage("Connected")
if not self.simulate :
self.mote.clear()
self.mote.show()
except IOError:
self.simulate = True
self.showMessage("Simulating")
self.yToStick = []
self.addStick(1)
self.addStick(2)
self.addStick(3)
self.addStick(4)
def takePhoto2(self):
self.bCameraBusy = True
os.system(gphoto2_command)
self.bCameraBusy = False
return 0
def takePhoto(self):
return self.takePhoto2()
logging.basicConfig(
format='%(levelname)s: %(name)s: %(message)s', level=logging.WARNING)
self.bCameraBusy = True
gp.check_result(gp.use_python_logging())
camera = gp.check_result(gp.gp_camera_new())
gp.check_result(gp.gp_camera_init(camera))
print('Capturing image')
file_path = gp.check_result(gp.gp_camera_capture(
camera, gp.GP_CAPTURE_IMAGE))
print('Camera file path: {0}/{1}'.format(file_path.folder, file_path.name))
target = os.path.join('~/tmp', file_path.name)
target = os.path.join(CURRENT_DIR,'..','LightPaintings', file_path.name)
print('Copying image to', target)
camera_file = gp.check_result(gp.gp_camera_file_get(
camera, file_path.folder, file_path.name, gp.GP_FILE_TYPE_NORMAL))
gp.check_result(gp.gp_file_save(camera_file, target))
if(self.completeRepeats == self.iRepeats):
subprocess.call(['xdg-open', target])
gp.check_result(gp.gp_camera_exit(camera))
self.bCameraBusy = False
return 0
def quit(self, event=None):
if not self.simulate :
self.mote.clear()
self.mote.show()
self.mainwindow.quit()
def reloadImage(self):
self.mode = MODE_IMAGE
self.loadImage()
def loadImage(self):
filename = self.filename
self.im = Image.open(filename)
iOriginalWidth, iOriginalHeight = self.im.size
fAspect = float(iOriginalWidth) / float(iOriginalHeight)
#targetWidth = int(fAspect * len(self.yToStick) * self.motePixelInCm)
targetWidth = int(fAspect * self.iPixels * self.motePixelInCm)
message = "Opened \""+os.path.basename(filename) + "\" "+str(targetWidth)+"cm wide"
self.showMessage(message)
self.rgb_im = self.im.convert('RGB').resize((self.timeSlices, self.iPixels), Image.NEAREST)
self.width, self.height = self.rgb_im.size
self.aImageValues = [[self.rgb_im.getpixel((x, y)) for x in range(self.width)] for y in range(self.height)]
self.aRandomGrid = [[1 for x in range(self.width)] for y in range(self.height)]
if(self.mode == MODE_IMAGE):
self.drawPreview()
def onFileChoose(self, event=None):
self.filename = askopenfilename(initialdir = "images/",title = "Select file",filetypes = (("gif files","*.gif"),("png files","*.png"),("jpeg files","*.jpg"),("all files","*.*")))
self.mode = MODE_IMAGE
self.loadImage()
def getColor(self, event=None):
color = askcolor(self.color)
if(color[0] == None):
return
if self.bPaintBlack == False:
self.mode = MODE_COLOR
self.color = color[0]
self.drawPreview()
def gradientChanged(self, event=None):
self.mode = MODE_COLOR
self.drawPreview()
def getColorEnd(self, event=None):
color = askcolor(self.colorend)
if(color[0] == None):
return
self.bGradient = True
self.updateControls()
self.mode = MODE_COLOR
self.colorend = color[0]
self.drawPreview()
def getImageX(self, processX):
direction = (1 if self.bPaintFromLeft else -1)
x = (0 if (direction == 1) else (self.timeSlices-1)) + (direction * processX)
return x
def getPreviewX(self, processX):
direction = (1 if self.bPaintFromLeft else -1) * (1 if self.bFacingScreen else -1)
x = (0 if (direction == 1) else (self.timeSlices-1)) + (direction * processX)
return x
def showColumn(self, x):
iImageX = self.getImageX(x)
if not self.simulate :
try:
for y in range(0, self.iPixels):
try:
if(self.bFlipVertical):
colour = self.aPixels[(self.iPixels-y)-1][iImageX]
else:
colour = self.aPixels[y][iImageX]
except:
colour = (0,0,0)
channel, pixel= self.yToStick[y % len(self.yToStick)]
if colour != (0, 0, 0):
r,g,b = colour
self.mote.set_pixel(channel, pixel, r, g, b)
else:
self.mote.set_pixel(channel, pixel, 0, 0, 0)
self.mote.show()
except IOError:
self.simulate = True
self.showMessage("Connection Failed. Simulating")
def updateControls(self):
self.builder.tkvariables.__getitem__('bGradient').set(self.bGradient)
self.builder.tkvariables.__getitem__('bReverseImage').set(self.bReverseImage)
self.builder.tkvariables.__getitem__('bFacingScreen').set(self.bFacingScreen)
self.builder.tkvariables.__getitem__('bPaintFromLeft').set(self.bPaintFromLeft)
self.builder.tkvariables.__getitem__('bPaintWhite').set(self.bPaintWhite)
self.builder.tkvariables.__getitem__('iPixels').set(self.iPixels)
self.builder.tkvariables.__getitem__('iDuration').set(self.iDuration)
self.builder.tkvariables.__getitem__('iRepeats').set(self.iRepeats)
self.builder.tkvariables.__getitem__('bControlCamera').set(self.bControlCamera)
self.builder.tkvariables.__getitem__('bPaintBlack').set(self.bPaintBlack)
self.builder.tkvariables.__getitem__('bTween').set(self.bTween)
self.builder.tkvariables.__getitem__('bLines').set(self.bLines)
self.builder.tkvariables.__getitem__('bSpeckles').set(self.bSpeckles)
self.builder.tkvariables.__getitem__('bRandomAcrossRange').set(self.bRandomAcrossRange)
self.builder.tkvariables.__getitem__('bFlipVertical').set(self.bFlipVertical)
self.scaDelay.set(self.iDelay)
def updateParams(self):
self.bGradient = self.builder.tkvariables.__getitem__('bGradient').get()
self.bReverseImage = self.builder.tkvariables.__getitem__('bReverseImage').get()
self.bFacingScreen = self.builder.tkvariables.__getitem__('bFacingScreen').get()
self.bPaintFromLeft = self.builder.tkvariables.__getitem__('bPaintFromLeft').get()
self.bPaintWhite = self.builder.tkvariables.__getitem__('bPaintWhite').get()
self.iPixels = self.builder.tkvariables.__getitem__('iPixels').get()
self.iDuration = self.builder.tkvariables.__getitem__('iDuration').get()
self.iRepeats = self.builder.tkvariables.__getitem__('iRepeats').get()
self.bControlCamera = self.builder.tkvariables.__getitem__('bControlCamera').get()
self.bPaintBlack = self.builder.tkvariables.__getitem__('bPaintBlack').get()
self.bTween = self.builder.tkvariables.__getitem__('bTween').get()
self.bLines = self.builder.tkvariables.__getitem__('bLines').get()
self.bSpeckles = self.builder.tkvariables.__getitem__('bSpeckles').get()
self.bRandomAcrossRange = self.builder.tkvariables.__getitem__('bRandomAcrossRange').get()
self.bFlipVertical = self.builder.tkvariables.__getitem__('bFlipVertical').get()
self.iDelay = self.scaDelay.get()
def getColColour(self, px):
if(self.bGradient):
r = int(round(self.color[0] + float(self.colorend[0] - self.color[0])*(float(px) / float(self.graphWidth))))
g = int(round(self.color[1] + float(self.colorend[1] - self.color[1])*(float(px) / float(self.graphWidth))))
b = int(round(self.color[2] + float(self.colorend[2] - self.color[2])*(float(px) / float(self.graphWidth))))
return (r,g,b)
return self.color
def getFrameColour(self):
if(self.iRepeats > 1):
j = self.completeRepeats
r = int(round(self.color[0] + float(self.colorend[0] - self.color[0])*(float(j) / float(self.iRepeats-1))))
g = int(round(self.color[1] + float(self.colorend[1] - self.color[1])*(float(j) / float(self.iRepeats-1))))
b = int(round(self.color[2] + float(self.colorend[2] - self.color[2])*(float(j) / float(self.iRepeats-1))))
return (r,g,b)
return self.color
def transformThroughRandom(self, color, x, y, base=(0,0,0)):
try:
fMultiply = self.aRandomGrid[y][x]
except:
fMultiply == False
if(fMultiply == False):
return (0,0,0)
else:
return (
int(round(fMultiply * float(color[0]-base[0])))+base[0],
int(round(fMultiply * float(color[1]-base[1])))+base[1],
int(round(fMultiply * float(color[2]-base[2])))+base[2]
)
def dontPaintWhite(self, colour):
if colour == (255, 255, 255) :
return (0,0,0)
return colour
def makeInvertedPixel(self, color,x,y,):
if color == (0, 0, 0):
return self.aColorPixels[y][x]
else:
return (0, 0, 0)
def makePixels(self):
if self.bTween:
self.aColorPixels = [[self.getFrameColour() for x in range(self.width)] for y in range(self.height)]
else:
if self.bRandomAcrossRange:
self.aColorPixels = [[self.transformThroughRandom(self.colorend, x, y, self.color) for x in range(self.width)] for y in range(self.height)]
else:
self.aColorPixels = [[self.getColColour(x) for x in range(self.width)] for y in range(self.height)]
if (self.mode == MODE_IMAGE):
if self.bPaintBlack :
self.aPixels = [[self.makeInvertedPixel(self.aImageValues[y][x],x,y) for x in range(self.width)] for y in range(self.height)]
else:
self.aRawPixels = self.aImageValues
else:
self.aRawPixels = self.aColorPixels
if self.bPaintBlack == False :
self.aPixels = [[self.transformThroughRandom(self.aRawPixels[y][x], x, y) for x in range(self.width)] for y in range(self.height)]
if self.bPaintWhite == False:
self.aPixels = [[self.dontPaintWhite(self.aPixels[y][x]) for x in range(self.width)] for y in range(self.height)]
if self.bReverseImage == True:
try:
self.aPixels = [[self.aPixels[y][(self.width - x) - 1] for x in range(self.width)] for y in range(self.height)]
except:
print("failing", len(self.aPixels), self.width , x)
def drawColumn(self, x):
iImageX = self.getImageX(x)
iPreviewX = self.getPreviewX(x)
iPreviewXend = self.getPreviewX(x+1)
for y in range(0, self.iPixels):
try :
colour = self.aPixels[y][iImageX]
except :
colour = (0, 0, 0)
if colour != (0, 0, 0):
color = str(webcolors.rgb_to_hex(colour))
self.canPreview.create_rectangle( iPreviewX, (y * self.motePixelInSceenPixels), iPreviewXend, (y * self.motePixelInSceenPixels) + self.motePixelInSceenPixels, width=0, fill=color)
if self.bPaintBlack & (self.currentColumn > 0 and self.currentColumn < self.graphWidth):
self.canPreview.create_rectangle( iPreviewX, (0), iPreviewXend, self.motePixelInSceenPixels *5, width=0, fill="#0F0")
def doPreview(self, event=None):
self.drawPreview()
def changeRandom(self):
self.updateParams()
self.makeRandom()
self.makePixels()
self.drawPreview()
def drawPreview(self):
self.updateParams()
self.makePixels()
self.canPreview.create_rectangle(0, 0, self.graphWidth, (len(self.yToStick) * self.motePixelInSceenPixels), fill="black")
for x in range(0, self.graphWidth):
self.drawColumn(x)
def makeRandom(self):
self.aRandomGrid = [[False for x in range(self.width)] for y in range(self.height)]
for y in range(self.iPixels):
if(self.bLines):
line = [random.randint(0, self.timeSlices), random.randint(0, self.timeSlices)]
line.sort()
start = line[0]
end = line[1]
else:
start = 0
end = self.timeSlices
for x in range(0, self.timeSlices):
if(x >= start and x <= end):
if(self.bSpeckles or self.bRandomAcrossRange):
level = random.random()
else:
level = 1
self.aRandomGrid[y][x]= level
def refreshPixels(self):
self.aImageValues = [[self.rgb_im.getpixel((x, y)) for x in range(self.width)] for y in range(self.height)]
self.makePixels()
def onShowEnd(self):
if not self.simulate :
self.mote.clear()
self.mote.show()
self.showMessage("Lights Finished")
self.completeRepeats += 1
if self.bControlCamera:
while self.bCameraBusy:
pass
message = "Show "+str(self.completeRepeats) + "/"+str(self.iRepeats)+" Complete in "+str(time.time() - self.startTime)+"s"
self.showMessage(message)
try:
self.im.seek(self.im.tell()+1)
self.rgb_im = self.im.convert('RGB').resize((self.timeSlices, self.iPixels))
self.refreshPixels()
self.showMessage("Loaded next frame")
self.drawPreview()
except EOFError:
try:
if (self.im.tell() > 0):
self.im.seek(0)
self.rgb_im = self.im.convert('RGB').resize((self.timeSlices, self.iPixels))
self.refreshPixels()
print("Animation Looped")
except:
if (self.im.tell() > 0):
self.im = Image.open(self.filename)
self.rgb_im = self.im.convert('RGB').resize((self.timeSlices, self.iPixels))
self.refreshPixels()
print("Could not loop - Reloaded")
if(self.completeRepeats < self.iRepeats):
self.singleShow()
else:
self.completeRepeats = 0
self.makeRandom()
self.drawPreview()
def startPhoto(self):
if self.bControlCamera:
thread.start_new_thread ( self.takePhoto , ())
def show(self):
self.canPreview.create_rectangle(0, 0, self.graphWidth, (len(self.yToStick) * self.motePixelInSceenPixels), fill="#000")
if self.simulate:
message = "Simulating"
else:
message = "Show "+str(self.completeRepeats+1)+"/"+str(self.iRepeats)+" started"
self.showMessage(message)
duration = float(self.scaDuration.get())
self.stepTime = int(1000 * duration / float(self.timeSlices))
self.stepTimeS = duration / float(self.timeSlices)
self.currentColumn = 0
self.startTime = time.time()
self.targetTime = time.time() + self.stepTimeS
self.doColumn()
def doColumn(self):
if time.time() < self.targetTime:
self.timer = self.mainwindow.after(1, self.doColumn)
return
thisColumn = self.currentColumn
if self.currentColumn < self.graphWidth:
self.targetTime += self.stepTimeS
self.timer = self.mainwindow.after(1, self.doColumn)
self.drawColumn(thisColumn)
self.showColumn(thisColumn)
else:
self.onShowEnd()
self.currentColumn += 1
def showMessage(self, message):
self.builder.tkvariables.__getitem__('messageText').set(message)
def on_btnDraw_clicked(self, event=None):
if (self.currentColumn > 0 and self.currentColumn < self.graphWidth) or self.bCameraBusy:
self.showMessage("Cannot start now, still running")
return False
self.updateParams()
self.completeRepeats = 0
self.singleShow()
def drawCountdown(self):
i = 1 + self.iFullDelay - self.delayRemaining
width = self.graphWidth
if self.iFullDelay > 0:
width = i * self.graphWidth/self.iFullDelay
self.canPreview.create_rectangle(0, 0, width, (len(self.yToStick) * self.motePixelInSceenPixels), fill="#000")
message = "Show "+str(self.completeRepeats+1)+"/"+str(self.iRepeats)+" starts in "+str(self.delayRemaining)
self.canPreview.itemconfigure(self.countdown_id, text=message)
self.showMessage(message)
if self.delayRemaining <= 4 and self.delayRemaining > 1:
self.beep(0.1)
self.delayRemaining -= 1
if self.delayRemaining > 0:
self.mainwindow.after(1000, self.drawCountdown)
else:
self.beep(0.3)
def singleShow(self):
if self.simulate:
self.connectToMote()
self.canPreview.create_rectangle(0, 0, self.graphWidth, (len(self.yToStick) * self.motePixelInSceenPixels), fill="#AAA")
self.countdown_id = self.canPreview.create_text(100, self.graphWidth -100, fill="#F00", text=".....")
delayTime = self.scaDelay.get()
lag = 0
if self.bControlCamera:
lag = self.cameraLag
#Start the countdown including the lag
self.iFullDelay = delayTime + lag
self.delayRemaining = self.iFullDelay
self.drawCountdown()
#start the camera shot after the delay without the lag
self.mainwindow.after(delayTime * 1000, self.startPhoto)
#start the show after waiting for delay & lag
self.mainwindow.after((delayTime + lag) * 1000, self.show)
def run(self):
self.mainwindow.mainloop()
if __name__ == '__main__':
app = MyApplication()
app.run()