-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathphotobooth.py
311 lines (264 loc) · 9.37 KB
/
photobooth.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
import picamera
import pygame
import time
import os
import PIL.Image
import RPi.GPIO as GPIO
from threading import Thread
from pygame.locals import *
from time import sleep
from PIL import Image, ImageDraw
# initialise global variables
Numeral = "" # Numeral is the number display
Message = "" # Message is a fullscreen message
BackgroundColor = ""
CountDownPhoto = ""
SmallMessage = "" # SmallMessage is a lower banner message
TotalImageCount = 0 # Counter for Display and to monitor paper usage
imagefolder = 'Photos'
templatePath = os.path.join('Photos', 'Template', "template.png") #Path of template image
ImageShowed = False
BUTTON_PIN = 25
IMAGE_WIDTH = 550
IMAGE_HEIGHT = 360
# Load the background template
bgimage = PIL.Image.open(templatePath)
#Setup GPIO
GPIO.setmode(GPIO.BCM)
GPIO.setup(BUTTON_PIN, GPIO.IN, pull_up_down=GPIO.PUD_UP)
# initialise pygame
pygame.init() # Initialise pygame
pygame.mouse.set_visible(False) #hide the mouse cursor
infoObject = pygame.display.Info()
screen = pygame.display.set_mode((infoObject.current_w,infoObject.current_h), pygame.FULLSCREEN) # Full screen
background = pygame.Surface(screen.get_size()) # Create the background object
background = background.convert() # Convert it to a background
screenPicture = pygame.display.set_mode((infoObject.current_w,infoObject.current_h), pygame.FULLSCREEN) # Full screen
backgroundPicture = pygame.Surface(screenPicture.get_size()) # Create the background object
backgroundPicture = background.convert() # Convert it to a background
transform_x = infoObject.current_w # how wide to scale the jpg when replaying
transfrom_y = infoObject.current_h # how high to scale the jpg when replaying
camera = picamera.PiCamera()
# Initialise the camera object
camera.resolution = (infoObject.current_w, infoObject.current_h)
camera.rotation = 0
camera.hflip = True
camera.vflip = False
camera.brightness = 50
camera.preview_alpha = 120
camera.preview_fullscreen = True
# A function to handle keyboard/mouse/device input events
def input(events):
for event in events: # Hit the ESC key to quit the slideshow.
if (event.type == QUIT or
(event.type == KEYDOWN and event.key == K_ESCAPE)):
pygame.quit()
# blackbars
def set_demensions(img_w, img_h):
global transform_y, transform_x, offset_y, offset_x
# based on output screen resolution, calculate how to display
ratio_h = (infoObject.current_w * img_h) / img_w
if (ratio_h < infoObject.current_h):
transform_y = ratio_h
transform_x = infoObject.current_w
offset_y = (infoObject.current_h - ratio_h) / 2
offset_x = 0
elif (ratio_h > infoObject.current_h):
transform_x = (infoObject.current_h * img_w) / img_h
transform_y = infoObject.current_h
offset_x = (infoObject.current_w - transform_x) / 2
offset_y = 0
else:
transform_x = infoObject.current_w
transform_y = infoObject.current_h
offset_y = offset_x = 0
def InitFolder():
global imagefolder
#check image folder existing, create if not exists
if not os.path.isdir(imagefolder):
os.makedirs(imagefolder)
imagefolder2 = os.path.join(imagefolder, 'images')
if not os.path.isdir(imagefolder2):
os.makedirs(imagefolder2)
def DisplayText(fontSize, textToDisplay):
global Numeral
global Message
global screen
global background
global pygame
global ImageShowed
global screenPicture
global backgroundPicture
global CountDownPhoto
if (BackgroundColor != ""):
#print(BackgroundColor)
background.fill(pygame.Color("black"))
if (textToDisplay != ""):
#print(displaytext)
font = pygame.font.Font(None, fontSize)
text = font.render(textToDisplay, 1, (227, 157, 200))
textpos = text.get_rect()
textpos.centerx = background.get_rect().centerx
textpos.centery = background.get_rect().centery
if(ImageShowed):
backgroundPicture.blit(text, textpos)
else:
background.blit(text, textpos)
def UpdateDisplay():
# init global variables from main thread
global Numeral
global Message
global screen
global background
global pygame
global ImageShowed
global screenPicture
global backgroundPicture
global CountDownPhoto
background.fill(pygame.Color("white")) # White background
if (BackgroundColor != ""):
#print(BackgroundColor)
background.fill(pygame.Color("black"))
if (Message != ""):
#print(displaytext)
font = pygame.font.Font(None, 100)
text = font.render(Message, 1, (227, 157, 200))
textpos = text.get_rect()
textpos.centerx = background.get_rect().centerx
textpos.centery = background.get_rect().centery
if(ImageShowed):
backgroundPicture.blit(text, textpos)
else:
background.blit(text, textpos)
if (Numeral != ""):
#print(displaytext)
font = pygame.font.Font(None, 800)
text = font.render(Numeral, 1, (227, 157, 200))
textpos = text.get_rect()
textpos.centerx = background.get_rect().centerx
textpos.centery = background.get_rect().centery
if(ImageShowed):
backgroundPicture.blit(text, textpos)
else:
background.blit(text, textpos)
if (CountDownPhoto != ""):
#print(displaytext)
font = pygame.font.Font(None, 500)
text = font.render(CountDownPhoto, 1, (227, 157, 200))
textpos = text.get_rect()
textpos.centerx = background.get_rect().centerx
textpos.centery = background.get_rect().centery
if(ImageShowed):
backgroundPicture.blit(text, textpos)
else:
background.blit(text, textpos)
if(ImageShowed == True):
screenPicture.blit(backgroundPicture, (0, 0))
else:
screen.blit(background, (0, 0))
pygame.display.flip()
return
def ShowPicture(file, delay):
global pygame
global screenPicture
global backgroundPicture
global ImageShowed
backgroundPicture.fill((0, 0, 0))
img = pygame.image.load(file)
img = pygame.transform.scale(img, screenPicture.get_size()) # Make the image full screen
#backgroundPicture.set_alpha(200)
backgroundPicture.blit(img, (0,0))
screen.blit(backgroundPicture, (0, 0))
pygame.display.flip() # update the display
ImageShowed = True
time.sleep(delay)
# display one image on screen
def show_image(image_path):
screen.fill(pygame.Color("white")) # clear the screen
img = pygame.image.load(image_path) # load the image
img = img.convert()
set_demensions(img.get_width(), img.get_height()) # set pixel dimensions based on image
x = (infoObject.current_w / 2) - (img.get_width() / 2)
y = (infoObject.current_h / 2) - (img.get_height() / 2)
screen.blit(img,(x,y))
pygame.display.flip()
def CapturePicture():
global imagefolder
global Numeral
global Message
global screen
global background
global screenPicture
global backgroundPicture
global pygame
global ImageShowed
global BackgroundColor
BackgroundColor = ""
Numeral = ""
Message = ""
UpdateDisplay()
time.sleep(1)
CountDownPhoto = ""
UpdateDisplay()
background.fill(pygame.Color("black"))
screen.blit(background, (0, 0))
pygame.display.flip()
camera.start_preview()
BackgroundColor = "black"
Numeral = ""
Message = "PRENEZ LA POSE"
BackgroundColor = ""
Numeral = ""
Message = ""
UpdateDisplay()
ts = time.time()
filename = os.path.join(imagefolder, 'images', str(ts) + '.jpg')
camera.capture(filename, resize=(IMAGE_WIDTH, IMAGE_HEIGHT))
camera.stop_preview()
ShowPicture(filename, 2)
ImageShowed = False
# return filename
def TakePictures():
global imagecounter
global imagefolder
global Numeral
global Message
global screen
global background
global pygame
global ImageShowed
global CountDownPhoto
global BackgroundColor
input(pygame.event.get())
CapturePicture()
UpdateDisplay()
def MyCallback(channel):
global Printing
GPIO.remove_event_detect(BUTTON_PIN)
Printing=True
def WaitForEvent():
global pygame
NotEvent = True
while NotEvent:
input_state = GPIO.input(BUTTON_PIN)
if input_state == False:
NotEvent = False
return
for event in pygame.event.get():
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
pygame.quit()
if event.key == pygame.K_DOWN:
NotEvent = False
return
time.sleep(0.2)
def main(threadName, *args):
InitFolder()
while True:
show_image('images/start_camera.jpg')
WaitForEvent()
time.sleep(0.2)
TakePictures()
GPIO.cleanup()
# launch the main thread
Thread(target=main, args=('Main', 1)).start()