-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathDocPB.py
547 lines (485 loc) · 18.1 KB
/
DocPB.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
import sys, pygame
import picamera
import time
from time import sleep
import os
import ConfigParser
import Image
from PIL import ImageFont
from PIL import ImageDraw
import eztext
import cups
from threading import Thread
import math
import shutil
import driveFuncs
import httplib2
import os
from apiclient import discovery
from apiclient.http import MediaFileUpload
#read settings file
config = ConfigParser.ConfigParser()
config.read('settings.txt')
#check internet connection
if ('False' == config.get('settings','internetEnabled')):
internetOn = False
else: internetOn = driveFuncs.internet_on()
print 'internetOn? ' + str(internetOn)
#Settings:
printCollage = True
imagedir = 'photos_temp/'
archiveDir='prints/'
toUploadDir = 'notUploaded/'
countdown = int(config.get('settings','countdown'))#seconds for countdown
inDev = False
if 'True' == config.get('settings','showCaption'): showCaption = True
else: showCaption = False
aspectRatio = 1.0*4/3
#Google Drive Settings
Gfolder = config.get('folders','driveFolder')
Gsub= config.get('folders','driveExt') + time.strftime("%Y_%m_%d")
#initialize camera
camera = picamera.PiCamera()
camera.preview_alpha = 130
camera.resolution = (1296,972)
camera.hflip = True
camera.sensor_mode = 4
#initialize background
pygame.init()
pygame.mouse.set_visible(False)
green = 0, 204, 0
red= 204,0,0
blue= 51,51,255
grey1=188,190,192
grey2=109,110,113
grey3=88,89,91
black = 55,57,58
white= 250,250,250
screen = pygame.display.set_mode((0,0),pygame.FULLSCREEN)
(mon_w, mon_h) = screen.get_size()
def scaled(number):
scaler = mon_h/1000.0
value = int(number*scaler)
return value
background = pygame.Surface((scaled(aspectRatio*1000),scaled(1000))) #Create the background object
background = background.convert() #Convert it to a background
#Create Folders if necessary
if not os.path.exists(imagedir):
os.makedirs(imagedir)
if internetOn:
if driveFuncs.folderSearch(Gfolder)=='':
driveFuncs.insertFolder(Gfolder, '' )
if driveFuncs.folderSearch(Gsub)=='':
driveFolder = driveFuncs.insertFolder(Gsub, driveFuncs.folderSearch(Gfolder))
else: driveFolder = driveFuncs.folderSearch(Gsub)
#Upload toBeUploaded files
if os.path.exists(toUploadDir):
if driveFuncs.containsPrints(toUploadDir):
for file in os.listdir(toUploadDir):
if file.endswith(".jpg"):
driveFuncs.save2drive(os.path.join(toUploadDir,file), file, driveFolder)
shutil.rmtree(toUploadDir, ignore_errors=True)
#Initialize variables
exitFlag=0
images=[]
lastPhoto=""
lastCollage=""
showLastPhoto=False
showLastCollage=False
state = 0 #statemachine
groupcounter = 1
imagecounter = 1
groupName=""
groupDescription=""
numPrints=""
smallfont = pygame.font.Font(None, scaled(50))
mediumfont = pygame.font.Font(None, scaled(100))
largefont = pygame.font.Font(None, scaled(250))
hugefont =pygame.font.Font(None, scaled(400))
####Functions
def startPreview():
camera.start_preview()
#initialize/clear global variables
def clearScreen():
global Message1, Message2, Message3, Message4
global Numeral
global textBox1, textBox2, textBox3
global Title, oldTitle
global updateRect
global showLastPhoto, showLastCollage, showHorses
Message1=Message2=Message3=Numeral=textBox1=textBox2=textBox3=Title=Message4= ""
oldTitle=""
updateRect= ()
showLastPhoto = False
showHorses = False
showLastCollage = False
return
#Update background display
def UpdateDisplay():
global background, updateRect, oldTitle
background.unlock()
background.fill(black)
if inDev:
stateIcon = smallfont.render(str(state),1, (0, 255,0))
background.blit(stateIcon,(10,10))
#if messages exist, print them to background
if (Title != ""):
tTitle = pygame.font.Font(None, scaled(80)).render(Title,1, green)
textRect = tTitle.get_rect()
textRect.centerx = background.get_rect().centerx
textRect = textRect.move(0,scaled(200))
background.blit(tTitle,textRect)
if (textBox1 != ""):
text = smallfont.render(textBox1,1, grey1)
background.blit(text,(scaled(100),scaled(450)))
if (textBox2 != ""):
text = smallfont.render(textBox2,1, grey1)
background.blit(text,(scaled(100),scaled(550)))
if (textBox3 != ""):
text = smallfont.render(textBox3,1, grey1)
background.blit(text,(scaled(100),scaled(700)))
if (Message1 != ""):
text = smallfont.render(Message1,1, grey2)
background.blit(text,(scaled(200),scaled(200)))
if (Message2 != ""):
text = smallfont.render(Message2,1, grey2)
background.blit(text,(scaled(200),scaled(300)))
if (Message3 != ""):
text = smallfont.render(Message3,1, grey2)
background.blit(text,(scaled(200),scaled(600)))
if (Message4 != ""):
text = smallfont.render(Message4,1, grey2)
textRect = text.get_rect()
textRect.centerx = background.get_rect().centerx
textRect = textRect.move(0,scaled(850))
background.blit(text,textRect)
if (Numeral != ""):
text = hugefont.render(Numeral,1, (255,0,0))
textRect = text.get_rect()
textRect.centerx = background.get_rect().centerx
textRect = textRect.move(0,scaled(400))
background.blit(text,textRect)
if (showLastPhoto):
scale=0.4*mon_h/1000.0
groupPic = pygame.image.load(lastPhoto)
picSize = groupPic.get_rect()
newWidth=int(picSize.size[0]*scale)
newHeight=int(picSize.size[1]*scale)
groupPic = pygame.transform.smoothscale(groupPic,(newWidth,newHeight))
background.blit(groupPic,(scaled(800),scaled(50)))
if (showLastCollage):
scale=0.5*mon_h/1000.0
collage = pygame.image.load(lastCollage)
picRect = collage.get_rect()
newWidth=int(picRect.size[0]*scale)
newHeight=int(picRect.size[1]*scale)
collage = pygame.transform.smoothscale(collage,(newWidth,newHeight))
collageRect = collage.get_rect()
collageRect.center = background.get_rect().center
collageRect =collageRect.move(0,scaled(-200))
background.blit(collage,collageRect)
if (showHorses):
scale=0.5*mon_h/1000.0
collage = pygame.image.load('assets/holdhorses.png')
picRect = collage.get_rect()
newWidth=int(picRect.size[0]*scale)
newHeight=int(picRect.size[1]*scale)
collage = pygame.transform.smoothscale(collage,(newWidth,newHeight))
collageRect = collage.get_rect()
collageRect.center = background.get_rect().center
collageRect =collageRect.move(0,scaled(200))
background.blit(collage,collageRect)
if (state==0):
scale=0.5*mon_h/1000.0
collage = pygame.image.load('assets/homepage.png')
picRect = collage.get_rect()
newWidth=int(picRect.size[0]*scale)
newHeight=int(picRect.size[1]*scale)
collage = pygame.transform.smoothscale(collage,(scaled(picRect.size[0]),scaled(picRect.size[1])))
collageRect = collage.get_rect()
collageRect.center = background.get_rect().center
# collageRect =collageRect.move(0,scaled(300))
background.blit(collage,collageRect)
#screen.blit(fit2screen(background), (0,0)) #print backgroud
blit2screen(background)
if updateRect == ():
pygame.display.update()
else: pygame.display.update(updateRect) #update screen
return
def blit2screen(surface):
global mon_w, mon_h, aspectRatio
wzero=0
hzero=0
if mon_w > aspectRatio*mon_h:
wzero=(mon_w-int(aspectRatio*mon_h))/2
else:
hzero=(mon_h-int(mon_w/aspectRatio))/2
screen.blit(surface, (wzero,hzero))
return
def takePicture():
global images, lastPhoto
global imagecounter
filename = 'image'+ `imagecounter` + '.jpg'
camera.capture(os.path.join(imagedir,filename)) #take/save photo
camera.stop_preview()
images.append(Image.open(os.path.join(imagedir,filename)).transpose(Image.FLIP_LEFT_RIGHT)) #save image object for compiling
lastPhoto = os.path.join(imagedir,filename)
imagecounter +=1
return
def AssAndPrint(): #assembles collage and prints it
# global Message1, Message2
global images, lastCollage, showCaption
thumbs= []
UpdateDisplay()
#new grid to print
pheight=1000
pwidth = 1500 #int(round(pheight*1.5))
if showCaption:
if groupName == "" and groupDescription == "":
captionOn = False
else: captionOn = True
if groupName == "" or groupDescription == "":
dash = ""
else: dash = " - "
else: captionOn=False
if captionOn:
iheight = 450
iwidth = int(iheight*640/480)
offset = 640-iwidth
border = 10
forPrint = Image.new("RGB", (pwidth, pheight), "white")
title = Image.open('assets/general.jpg')
title=title.rotate(90)
title.thumbnail((1000,1000))
for i in range (1,5):
images[i].thumbnail((iwidth,iheight))
forPrint.paste(images[1],(offset+20,10))
forPrint.paste(images[2],(offset+20,30+iheight))
forPrint.paste(images[3],(offset+40+iwidth,10))
forPrint.paste(images[4],(offset+40+iwidth,30+iheight))
forPrint.paste(title,(offset+50+iwidth*2,10))
font = ImageFont.truetype('assets/Verdana.ttf', 35)
draw = ImageDraw.Draw(forPrint)
draw.text((offset+20, 930),groupName + dash + groupDescription + " "+ time.strftime("%D"),(0,0,0),font=font )
else:
iwidth = 400
iheight = 480
border = 10
forPrint = Image.new("RGB", (pwidth, pheight), "white")
title = Image.open('assets/general.jpg')
title.thumbnail((1000,1000))
title=title.rotate(90)
#thumbnail the 4 images
if showCaption: j=1
else: j=0
for i in range (j,j+4):
images[i].thumbnail((640,480))
forPrint.paste(images[j],(20,10))
forPrint.paste(images[j+1],(20,30+480))
forPrint.paste(images[j+2],(40+640,10))
forPrint.paste(images[j+3],(40+640,30+480))
forPrint.paste(title,(50+640*2,10))
if not os.path.exists(archiveDir):
os.makedirs(archiveDir)
archivename = 'PB'+time.strftime("%Y%m%d_%H%M") + '.jpg'
forPrint.save(archiveDir+archivename) #save to Archive folder
lastCollage = os.path.join(archiveDir,archivename)
if internetOn:
driveFuncs.save2drive(archiveDir+archivename, archivename, driveFolder)
else:
if not os.path.exists(toUploadDir):
os.makedirs(toUploadDir)
forPrint.save(toUploadDir+archivename)
#Dumb printer crops a bit fix
extra = 30
fheight=int(pheight+extra)
fwidth=int(fheight*1.5)
cropPrint = Image.new("RGB", (fwidth, fheight), "white")
cropPrint.paste(forPrint,(int(.75*extra),int(.5*extra)))
cropPrint.save(archiveDir+'temp.jpg')
return
def sendToPrinter():
global numPrints
conn = cups.Connection()
printers = conn.getPrinters()
# printer_name = printers.keys()[0]
for name in printers.keys():
if ('selphy' in name.lower()):
printer_name = name
if numPrints != 0:
conn.printFile(printer_name,archiveDir+'temp.jpg',"PhotoBooth",{"copies": str(numPrints)})
return
print "I can't find the printer..."
return
def timer():
global Numeral
for j in range (countdown, 0, -1):
Numeral = str(j)
UpdateDisplay()
sleep(1)
Numeral = '0'
UpdateDisplay()
sleep(.1)
Numeral=""
UpdateDisplay()
return
def capture():
startPreview()
timer()
clearScreen()
UpdateDisplay()
takePicture()
camera.stop_preview()
return
#Main Script
def main(threadName, *args):
#call globals
global Title, Message1, Message2, Message3, Message4, state, showLastPhoto, showLastCollage, updateRect, showHorses, showCEEO
global numPrints, groupName, groupDescription
clock = pygame.time.Clock()
clearScreen()
while not exitFlag:
clock.tick(30)
# ms=clock.get_time()
# if ms > 500: print str(ms) + ' ' + str(state)
if state == 0: #Welcome screen
sleep(.05)
elif state == 1: #Take sample pic
clearScreen()
Message1=config.get('prompts','prompt0')
UpdateDisplay()
sleep(1)
capture()
state = 2.1; clearScreen()
elif int(state)==2: #Get user info
if state == 2.1:
Message1="Tell me about about you!"
showLastPhoto= True
UpdateDisplay()
showLastPhoto= False
updateRect = pygame.Rect(0,scaled(400),scaled(2000),scaled(800))
state =2.2
if state==2.4:
Message3 = "Great!"
print(groupName, groupDescription)
UpdateDisplay()
sleep(2)
clearScreen()
state=3.1
elif int(state) ==3: #take more photos
if state == 3.1:
Message1="Now Lets Take Some Pictures of You and Your Project!"
UpdateDisplay()
sleep(3)
Message1=config.get('prompts','prompt1')
UpdateDisplay()
sleep(2)
capture()
state = 3.2
elif state ==3.2:
Message1=config.get('prompts','prompt2')
UpdateDisplay()
sleep(2)
capture()
state=3.3
elif state ==3.3:
Message1=config.get('prompts','prompt3')
UpdateDisplay()
sleep(2)
capture()
state=3.4;
elif state ==3.4:
Message1=config.get('prompts','prompt4')
UpdateDisplay()
sleep(2)
capture()
state=3.5;
elif state==3.5: state = 4.1; clearScreen()
elif int(state) == 4:
if state == 4.1:
Message1="Compiling Photos"
Message2="So, hold your horses"
showHorses = True
UpdateDisplay()
AssAndPrint()
clearScreen()
if 'False' == config.get('settings','send2printer'):
state = 4.3
else: state = 4.2 #Message4="Sent to Printer. Click SpaceBar to restart"
showLastCollage= True
UpdateDisplay()
showLastCollage= False
updateRect=(0,scaled(650),scaled(1300),scaled(500))
if state == 4.3:
if 'True' == config.get('settings','send2printer'):
sendToPrinter()
if numPrints ==0:
Message4="Picture Saved, Click SpaceBar to restart"
else: Message4="Sent to Printer. Click SpaceBar to restart"
else: Message4="Picture Saved, Click SpaceBar to restart"
state=4.5
UpdateDisplay()
def watchInput(threadName, *args):
global Message3, exitFlag, state, textBox1, textBox2,textBox3, groupcounter, imagecounter, images
global groupName, groupDescription, numPrints
txtbx = eztext.Input(maxlength=45, prompt='title')
clock = pygame.time.Clock()
while not exitFlag:
clock.tick(30)
ms=clock.get_time()
# if ms > 500: print str(ms) + ' ' + str(state)
events = pygame.event.get()
for event in events:
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
exitFlag=1
if event.key == pygame.K_SPACE:
if state ==0:
if config.get('settings','showCaption') == 'False':
state = 3.1
else:
state = 1
clearScreen()
if state==4.5:
clearScreen()
sleep(.1)
groupcounter+=1
imagecounter=1
images=[]
state =0
if event.key == pygame.K_RETURN:
if state ==2.2:
state = 2.3
groupName = txtbx.value
txtbx.value=""
elif state ==2.3:
state = 2.4
groupDescription = txtbx.value
txtbx.value=""
elif state ==4.2:
state = 4.3
if '1' in txtbx.value:
numPrints = 1
elif '2' in txtbx.value:
numPrints = 2
elif '3' in txtbx.value:
numPrints = 3
elif '0' in txtbx.value:
numPrints = 0
else: numPrints=1
txtbx.value=""
textBox3=""
print str(numPrints) + ' prints'
UpdateDisplay()
if state ==2.2:
txtbx.update(events)
textBox1='Your Name(s): '+txtbx.value
elif state == 2.3:
txtbx.update(events)
textBox2='Description of Project: '+txtbx.value
elif state == 4.2:
txtbx.update(events)
textBox3='Quantity of Prints? (type 0, 1, 2, 3): '+txtbx.value
#start Threads
Thread(target=watchInput, args=('WatchInput', 1)).start()
Thread(target=main, args=('Main',1)).start()