-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathphotobooth.py
executable file
·1189 lines (970 loc) · 45.3 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
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
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#! /usr/bin/env python
import time
from threading import Thread
import glob
import random
import os
import socket
import gettext
import traceback
import yaml
import sys
from datetime import datetime
from subprocess import check_output
#Own modules
from pygame_utils import *
from user_io import get_user_io_factory, LedState, LedType
from camera import get_camera_factory
from instagram_filters.filters import Gotham, Kelvin, Nashville, Lomo, Toaster, BlackAndWhite
from instagram_filters.decorations import Logo
from instagram_filters.filter import Filter
import print_utils
import storage
#Visual Configuration
DEFAULT_RESOLUTION = [640,424]
COUNTER_FONT_SIZE = 140
INFO_FONT_SIZE = 36
CAPTION_FONT_SIZE = 46
INFO_SMALL_FONT_SIZE = 24
INFO_TEXT_Y_POS = 100
MAX_PREVIEW_FAILURE_CNT = 10
BUTTON_BAR_Y_OFFSET = 80
def draw_wait_box(screen, text):
draw_text_box(screen=screen, text=text, pos=(None, None),
size=INFO_FONT_SIZE, border_color=COLOR_GREY)
pygame.display.update()
class PhotoBoothState(object):
"""
The photobooth application is implemented as state machine, this class represents one photo state.
States are switched using a count down timmer (counter in seconds) or switching is triggered manually
with the switch_* methods. A specific state implementation inherits from this class and implements its logic
either in the counter_callback handler or in the update_callback function or both. The update is periodically
triggered by the main application.
"""
def __init__(self, photobooth, next_state, failure_state=None, counter_callback=None, counter_callback_args=None, counter=-1):
"""
:param photobooth: Reference to the main application
:type photobooth: PhotoBooth
:param next_state: the next state to switch to
:param failure_state: a state we switch to in case of exceptions
:param counter_callback: callback function that is executed once the counter exceeded
:param counter_callback_args: optional arguments for the callback function
:param counter: count down timer in seconds
"""
self.photobooth = photobooth
self.next_state = next_state
self.inital_counter = counter
self.failure_state = failure_state
self.counter_callback = counter_callback
self.counter_callback_args = counter_callback_args
self.counter_sleep_time = 1
self.enabled = False
self._is_processing = False
self.reset()
def reset(self):
self.counter = self.inital_counter
self.counter_last_update_time = time.time()
self.photobooth.io_manager.reset_button_states()
def set_counter(self, value):
self.counter_last_update_time = time.time()
self.counter = value
def update_callback(self, photobooth):
"""
Override this function for periodic checks updates in the state (e.g. drawing, button pressing etc.)
:param photobooth: reference to the photobooth main app to easily access screen, inputs etc.
"""
pass
def update(self):
"""
Main update life cycle function
"""
try:
self.update_callback()
self.update_counter()
except Exception:
print(traceback.format_exc())
if self.failure_state:
self.switch_state(self.failure_state)
else:
print('No failure_state defined, trying to switch to last state')
self.switch_last()
return
def update_counter(self):
"""
function that updates the interal count down timer and calls the counter callback function when the counter
expires
"""
if self.counter > 0:
now = time.time()
diff = now - self.counter_last_update_time
if diff >= self.counter_sleep_time:
self.counter_last_update_time = now
self.counter-=1
if self.is_counter_expired():
if self.counter_callback:
if(self.counter_callback_args):
self.counter_callback(*self.counter_callback_args)
else:
self.counter_callback()
return True
return False
def is_counter_expired(self):
"""
Check if the count down counter is expired
:return: True if it is expired
"""
return self.counter == 0
def is_counter_enabled(self):
"""
Check if the count down counter is enabled
:return: True if it is enabled
"""
return self.counter > -1
def switch_state(self, state):
"""
Switch to the given state object
:param state: new state object
:type state: PhotoBoothState
"""
self.photobooth.state = state
def switch_next(self):
"""
Switch to the next state
"""
self.switch_state(self.next_state)
def switch_last(self):
"""
Switch to the last state
"""
self.switch_state(self.photobooth.last_state)
def _enable_wait_message(self):
"""
create a thread that shows a wait message
:return: thread
"""
t = Thread(target=self._wait_worker)
self._is_processing = True
t.start()
return t
def _disable_wait_message(self, thead):
"""
Disables the wait message
:param thead: need the returned thead of _enable_wait_message
"""
self._is_processing = False
thead.join()
def _wait_worker(self):
"""
worker function for the wait thread
"""
point_count = 0
led_counter = 0
while self._is_processing:
draw_wait_box(self.photobooth.screen, _("Please wait, processing") + '.' * point_count)
point_count += 1
led_counter += 1
point_count = point_count % 4
self.photobooth.io_manager.show_led_coutdown(led_counter)
time.sleep(1)
class PhotoBooth(object):
"""
This is the main application class. It holds all important object references and runs the main application life
cycle.
Important objects in this class:
state: the current state of the photobooth state machine
cam: The camera object
screen: The pygame screen for drawing
event_manager: Cache/handler for pygame events like button presses, mouse clicks etc
io_manager: Handling and enbaling ALL user input output, like external buttons, leds etc
config: Access configuration values
"""
def __init__(self, config):
self.cam = None
self.screen = None
self._state = None
self._last_state = None
# tuple of (pygame.image, image path)
self._last_photo_resized = None
# tuple of (pygame.image, image path)
self._last_photo = None
self._taken_photos = []
self.config = config
self.tmp_dir = config['temp_directory']
self.fullscreen=bool(config['fullscreen'])
# Detect the screen resolution
info_object = pygame.display.Info()
self.screen_resolution = [info_object.current_w, info_object.current_h]
self.set_fullscreen(self.fullscreen)
self.app_resolution = self.screen.get_size()
self.screen.fill((128, 128, 128))
self.event_manager = PyGameEventManager()
self.io_manager = get_user_io_factory().create_algorithm(id_class=config['io_manager'], photobooth=self)
self.change_photo_dir(new_directory=config['photo_directory'], reinit_camera=False)
def set_fullscreen(self, fullscreen):
if fullscreen:
self.screen = pygame.display.set_mode(self.screen_resolution, pygame.FULLSCREEN)
pygame.mouse.set_visible(False)
else:
if self.cam:
picture = self.cam.get_preview()
if picture:
self.screen = pygame.display.set_mode(picture.get_size())
else:
raise Exception("Failed to get camera preview")
else:
self.screen = pygame.display.set_mode(DEFAULT_RESOLUTION)
self.app_resolution = self.screen.get_size()
def change_photo_dir(self, new_directory, reinit_camera=True):
self.photo_directory = new_directory
# create photo directory if necessary
if not os.path.exists(self.photo_directory):
os.makedirs(self.photo_directory)
if reinit_camera:
self.init_camera()
def init_camera(self):
if self.cam:
self.cam.close()
self.cam = get_camera_factory().create_algorithm(id_class=self.config['camera_type'], photo_directory=self.photo_directory, tmp_directory=self.tmp_dir)
self.cam.disable_live_autofocus()
self.set_fullscreen(self.fullscreen)
def update(self):
self.event_manager.update_events()
self.state.update()
#order is important here
self.io_manager.update()
def close(self):
if self.cam:
self.cam.set_idle()
if self.io_manager:
self.io_manager.set_all_led(LedState.OFF)
pygame.quit()
@property
def last_photo(self):
"""
last photo
:return: tuple of (pygame.image, image path)
"""
return self._last_photo
@property
def last_photo_resized(self):
"""
last photo already resized to screen
:return: tuple of (pygame.image, image path)
"""
return self._last_photo_resized
@last_photo.setter
def last_photo(self, value):
"""
set new photo
:param value: tuple of (pygame.image, image path)
"""
self._last_photo = value
#resize photo to screen
self._last_photo_resized = (pygame.transform.scale(value[0], self.screen.get_size()), value[1])
@property
def state(self):
return self._state
@state.setter
def state(self, value):
if not value.enabled:
self.state = value.next_state
return
self._last_state = self._state
self._state = value
self._state.reset()
@property
def last_state(self):
return self._last_state
#State machine state classes
class StateWaitingForCamera(PhotoBoothState):
"""
Initial state that waits for the camera becoming available
This state can also be used to return in failure case
"""
def __init__(self, photobooth, next_state, admin_state):
super(StateWaitingForCamera, self).__init__(photobooth=photobooth, next_state=next_state)
self.admin_state = admin_state
def update_callback(self):
# try initialisation again
try:
#background color
draw_rect(self.photobooth.screen, (0, 0), self.photobooth.app_resolution)
if self.photobooth.io_manager.admin_button_pressed():
if self.admin_state:
self.photobooth.state = self.admin_state
self.photobooth.init_camera()
self.switch_next()
except Exception as e:
pos = get_text_mid_position(self.photobooth.app_resolution)
show_text_mid(self.photobooth.screen, _("Camera not connected:"), pos,
size=INFO_FONT_SIZE, color=COLOR_ORANGE)
show_text_mid(self.photobooth.screen, str(e), (pos[0],pos[1]+40),
size=INFO_FONT_SIZE, color=COLOR_ORANGE)
print(str(e))
time.sleep(1)
class StateShowSlideShow(PhotoBoothState):
"""
State showing already taken photos in a random order slide show
"""
def __init__(self, photobooth, next_state, counter):
self._photo_set = []
self._photo_set_already_shown = []
self.current_photo = None, None
super(StateShowSlideShow, self).__init__(photobooth=photobooth, next_state=next_state, counter=counter,
counter_callback=self._next_photo)
logo = self.photobooth.config['logo']
if logo:
self._logo_img = pygame.image.load(logo)
else:
self._logo_img = None
def update_callback(self):
if self.photobooth.event_manager.mouse_pressed() or self.photobooth.io_manager.any_button_pressed(reset=True):
self.switch_next()
if self.current_photo[0]:
show_cam_picture(self.photobooth.screen, self.current_photo[0])
if self._logo_img:
self.draw_logo(self._logo_img)
self.photobooth.io_manager.show_led_coutdown(self.counter)
draw_text_box(screen=self.photobooth.screen, text=_("Slideshow, press any button to continue"),
pos=(None, self.photobooth.app_resolution[1]-BUTTON_BAR_Y_OFFSET), size=INFO_FONT_SIZE)
def draw_logo(self, logo):
offset = 15
img_size = logo.get_size()
pos = (self.photobooth.app_resolution[0] - img_size[0] - offset, offset)
self.photobooth.screen.blit(logo, pos)
def _last_photo(self):
if len(self._photo_set_already_shown) > 0:
if self.current_photo:
self._photo_set.append(self.current_photo[1])
last = self._photo_set_already_shown[-1]
self._photo_set_already_shown.remove(last)
print("Next photo: " + last)
print("Old ", self._photo_set_already_shown)
print("New ", self._photo_set)
self.current_photo = pygame.image.load(last), last
super(StateShowSlideShow, self).reset() # reset counter
def _next_photo(self):
#save already shown photos
if self.current_photo:
self._photo_set_already_shown.append(self.current_photo[1])
if len(self._photo_set) > 0:
photo_file = random.choice(self._photo_set)
self._photo_set.remove(photo_file)
print("Next photo: " + photo_file)
print("Old ", self._photo_set_already_shown)
print("New ", self._photo_set)
self.current_photo = pygame.image.load(photo_file), photo_file
else:
self._reload_photo_set() # check for new photos
super(StateShowSlideShow, self).reset() # reset counter
def _reload_photo_set(self):
# load all images from directory
self._photo_set = glob.glob(self.photobooth.photo_directory + "/*.jpg")
self._photo_set_already_shown = []
def reset(self):
super(StateShowSlideShow, self).reset()
if self.photobooth.cam:
self.photobooth.cam.set_idle()
self._reload_photo_set()
self._next_photo()
print("Loading Slideshow images")
class StateAdvancedSlideShow(StateShowSlideShow):
def __init__(self, photobooth, next_state, counter, print_state):
super(StateAdvancedSlideShow, self).__init__(photobooth=photobooth, next_state=next_state, counter=counter)
self.print_state = print_state
def update_callback(self):
if self.current_photo[0]:
show_cam_picture(self.photobooth.screen, self.current_photo[0])
if self._logo_img:
self.draw_logo(self._logo_img)
self.photobooth.io_manager.show_led_coutdown(self.counter)
if self.photobooth.io_manager.next_button_pressed():
self._next_photo()
elif self.photobooth.io_manager.prev_button_pressed():
self._last_photo()
if self.photobooth.event_manager.mouse_pressed() or self.photobooth.io_manager.cancel_button_pressed(reset=True):
self.switch_next()
if self.photobooth.io_manager.accept_button_pressed():
self.photobooth.last_photo = self.current_photo
self.switch_state(self.print_state)
draw_text_box(screen=self.photobooth.screen, text=_("Slideshow"), pos=(40, 40), size=CAPTION_FONT_SIZE)
draw_button_bar(self.photobooth.screen, text=[_("Return"), _("Prev"), _("Next"), _("Print")],
pos=(None, self.photobooth.app_resolution[1] - BUTTON_BAR_Y_OFFSET))
class StateWaitingForPhotoTrigger(PhotoBoothState):
"""
State waiting for people initiating the next photo
"""
def __init__(self, photobooth, next_state, timeout_state = None, admin_state=None, failure_state=None, counter=-1):
super(StateWaitingForPhotoTrigger, self).__init__(photobooth=photobooth, next_state=next_state,
failure_state=failure_state, counter=counter,
counter_callback=self._switch_timeout_state)
self.timeout_state = timeout_state
self.admin_state = admin_state
self._preview_failure_cnt = 0
def update_callback(self):
if self.photobooth.io_manager.admin_button_pressed():
if self.admin_state:
self.photobooth.state = self.admin_state
elif self.photobooth.event_manager.mouse_pressed() or self.photobooth.io_manager.any_button_pressed(reset=True):
self.switch_next()
preview_img = None
try:
preview_img = self.photobooth.cam.get_preview()
except:
self._preview_failure_cnt += 1
if preview_img:
show_cam_picture(self.photobooth.screen, preview_img)
self._preview_failure_cnt = 0
elif self._preview_failure_cnt > MAX_PREVIEW_FAILURE_CNT:
raise Exception("Preview failed")
else:
self._preview_failure_cnt += 1
draw_button_bar(self.photobooth.screen, text=[_("Photo"),_("Photo"),_("Photo"),_("Photo")],
pos=(None,self.photobooth.app_resolution[1]-BUTTON_BAR_Y_OFFSET))
self.photobooth.io_manager.set_all_led(LedState.ON) #TODO maybe not necessary, but needs to be tested
def _switch_timeout_state(self):
if self.timeout_state:
self.photobooth.state = self.timeout_state
else:
self.reset()
def reset(self):
super(StateWaitingForPhotoTrigger, self).reset()
self.photobooth.io_manager.set_all_led(LedState.ON)
self._preview_failure_cnt = 0
class StatePhotoTrigger(PhotoBoothState):
"""
Count down photo trigger state
"""
def __init__(self, photobooth, next_state, failure_state=None, counter=-1):
super(StatePhotoTrigger, self).__init__(photobooth=photobooth, next_state=next_state,
failure_state=failure_state, counter=counter,
counter_callback=self._take_photo)
self._arrow_img = pygame.image.load('res/arrow.png')
self._mid_position = get_text_mid_position(self.photobooth.app_resolution)
self._last_preview = None
self._preview_failure_cnt = 0
self._autofocus_enabled = True
def update_callback(self):
self.photobooth.io_manager.show_led_coutdown(self.counter)
if self.counter == 1:
self.show_final_view()
pygame.display.update()
if self._autofocus_enabled:
self.photobooth.cam.disable_live_autofocus()
self._autofocus_enabled = False
else:
preview_img = None
try:
preview_img = self.photobooth.cam.get_preview()
except:
self._preview_failure_cnt += 1
if preview_img:
show_cam_picture(self.photobooth.screen, preview_img)
self._last_preview = preview_img
self._preview_failure_cnt = 0
elif self._preview_failure_cnt > MAX_PREVIEW_FAILURE_CNT:
raise Exception("Preview failed")
else:
self._preview_failure_cnt += 1
# Show countdown
show_text_mid(self.photobooth.screen, str(self.counter), self._mid_position, COUNTER_FONT_SIZE, COLOR_WHITE,
shadow_size=16)
#cancel photo if necessary
draw_button_bar(self.photobooth.screen, text=[_("Cancel"), "", "", ""],
pos=(None, self.photobooth.app_resolution[1] - BUTTON_BAR_Y_OFFSET))
if self.photobooth.io_manager.cancel_button_pressed():
self.photobooth.cam.disable_live_autofocus()
self.switch_last()
def show_final_view(self):
arrow_size = self._arrow_img.get_size()
offset = 50
pos = (self._mid_position[0] - (arrow_size[0] // 2), offset)
draw_rect(self.photobooth.screen, (0, 0),
(self.photobooth.app_resolution[0], self.photobooth.app_resolution[1]))
self.photobooth.screen.blit(self._arrow_img, pos)
show_text_mid(self.photobooth.screen, _("Smile :-)"), (self._mid_position[0], arrow_size[1]+50 + offset),
COUNTER_FONT_SIZE, color=COLOR_DARK_GREY)
def reset(self):
super(StatePhotoTrigger, self).reset()
self._mid_position = get_text_mid_position(self.photobooth.app_resolution) # update in case resolution changed
self._preview_failure_cnt = 0
if self.photobooth.cam:
self._autofocus_enabled = True
self.photobooth.cam.enable_live_autofocus()
def _take_photo(self):
self.photobooth.io_manager.show_led_coutdown(self.counter)
# take photo
self.photobooth.last_photo = self.photobooth.cam.take_photo()
self.photobooth.io_manager.set_all_led(LedState.ON)
self.switch_next()
class StateShowPhoto(PhotoBoothState):
"""
State for showing the last taken photo
"""
def __init__(self, photobooth, next_state, counter=-1):
super(StateShowPhoto, self).__init__(photobooth=photobooth, next_state=next_state, counter=counter, counter_callback=self.switch_next)
def update_callback(self):
show_cam_picture(self.photobooth.screen, self.photobooth.last_photo_resized[0])
class StatePrinting(PhotoBoothState):
"""
State for selecting print out
"""
def __init__(self, photobooth, next_state, counter=-1):
super(StatePrinting, self).__init__(photobooth=photobooth, next_state=next_state, counter=counter, counter_callback=self.switch_next)
self._error_txt = None
self.logo_file = self.photobooth.config['print_logo']
def print_photo(self, photo_file):
try:
if self.logo_file:
printfile = self.photobooth.tmp_dir + "/logo_print_file.jpg"
fil = Filter(filename=photo_file, output_filename=printfile)
decoration = Logo(logo_path=self.logo_file)
fil.add_post_decoration(decoration=decoration)
if fil:
fil.apply()
fil.close_image()
photo_file = printfile
print_utils.print_photo(photo_file=photo_file)
return True
except Exception as e:
self._error_txt = str(e)
return False
def update_callback(self):
show_cam_picture(self.photobooth.screen, self.photobooth.last_photo_resized[0])
self.photobooth.io_manager.set_led(led_type=LedType.GREEN,led_state=LedState.ON)
self.photobooth.io_manager.set_led(led_type=LedType.RED, led_state=LedState.ON)
self.photobooth.io_manager.set_led(led_type=LedType.BLUE, led_state=LedState.OFF)
self.photobooth.io_manager.set_led(led_type=LedType.YELLOW, led_state=LedState.OFF)
if self._error_txt:
show_text_left(self.photobooth.screen, _("Print failure:"), (20, 240), size=INFO_FONT_SIZE, color=COLOR_ORANGE)
show_text_left(self.photobooth.screen, self._error_txt, (20, 270), size=INFO_FONT_SIZE, color=COLOR_ORANGE)
if self.photobooth.event_manager.mouse_pressed() or self.photobooth.io_manager.accept_button_pressed():
wait_thread = self._enable_wait_message()
if self.print_photo(self.photobooth.last_photo[1]):
self.switch_next()
self._error_txt = None
else: # failure
self.reset() # reset timeout counter
self._disable_wait_message(wait_thread)
elif self.photobooth.io_manager.cancel_button_pressed():
self.switch_next()
draw_text_box(screen=self.photobooth.screen, text=_("Print photo?"), pos=(None, INFO_TEXT_Y_POS), size=INFO_FONT_SIZE)
draw_button_bar(self.photobooth.screen, text=[_("Cancel"), "", "", _("Print")],
pos=(None, self.photobooth.app_resolution[1] - BUTTON_BAR_Y_OFFSET))
def reset(self):
super(StatePrinting, self).reset()
class StateFilter(PhotoBoothState):
"""
State for providing several filter options of the photo
"""
def __init__(self, photobooth, next_state, counter=-1):
super(StateFilter, self).__init__(photobooth=photobooth, next_state=next_state, counter=counter, counter_callback=self.switch_next)
self.filter_photos = []
self._picture_size = ()
self._current_filter_idx = 0
self._filter_count = 4
def filter_photo_fullsize(self, photo, idx, dest):
"""
filter photo in full resolution
:param photo: tuple (pygame.image, path)
:param idx: filter_idx to apply
:param dest: destination path of photo
:return: tuple (pygame.image,path) of new photo
"""
filter_file = dest
width = photo[0].get_size()[0]
height = photo[0].get_size()[1]
if self.apply_photo_filter(input_file=photo[1], idx=idx, output_file=dest, width=width, height=height):
photo_obj = pygame.image.load(filter_file)
return photo_obj, filter_file
else:
return photo
def filter_photo_preview(self, photo, idx):
"""
create a small preview filter img
:param photo: tuple (pygame.image,path)
:param idx: filter_idx to apply
:return: tuple (pygame.image,path) of new photo
"""
filter_file = self.photobooth.tmp_dir + "/filter" + str(idx) + ".jpg"
pygame.image.save(photo[0], filter_file)
if self.apply_photo_filter(filter_file, idx):
photo_obj = pygame.image.load(filter_file)
return photo_obj, filter_file
else:
return photo
def apply_photo_filter(self, input_file, idx, output_file=None, width=None, height=None):
"""
apply a photo filter
:param input_file: the photo file to filter
:param idx: the idx of the filter to use
:param output_file: the created file
:param width: optional width of image to save some image loading
:param height: optional height of image to save some image loading
:return: True if a filter was applied
"""
fil = None
if idx == 1:
fil = Nashville(filename=input_file, output_filename=output_file, width=width, height=height)
if idx == 2:
fil = Toaster(filename=input_file, output_filename=output_file, width=width, height=height)
if idx == 3:
fil = BlackAndWhite(filename=input_file, output_filename=output_file, width=width, height=height)
if fil:
fil.apply()
fil.close_image()
return True
else:
return False
def create_filtered_photo_collection(self):
"""
Create small thumbnail preview filtered photos
"""
last_photo = self.photobooth.last_photo_resized
small_size = pygame.transform.scale(last_photo[0], self._picture_size)
#mirror in order match other visualization
mirrored_photo = pygame.transform.flip(small_size,True,False)
scaled_original_photo = (mirrored_photo, last_photo[1])
self.filter_photos = [
scaled_original_photo,
self.filter_photo_preview(photo=scaled_original_photo, idx=1),
self.filter_photo_preview(photo=scaled_original_photo, idx=2),
self.filter_photo_preview(photo=scaled_original_photo, idx=3)
]
def draw_filtered_photos(self):
"""
draw filter previews to screen
"""
if len(self.filter_photos) > 0:
image_pos = (0, 0)
rect_pos = image_pos
self.photobooth.screen.blit(self.filter_photos[0][0], image_pos)
if self._current_filter_idx == 0:
rect_pos = image_pos
image_pos = (self._picture_size[0], 0)
self.photobooth.screen.blit(self.filter_photos[1][0], image_pos)
if self._current_filter_idx == 1:
rect_pos = image_pos
image_pos = ( 0, self._picture_size[1])
self.photobooth.screen.blit(self.filter_photos[2][0], image_pos)
if self._current_filter_idx == 2:
rect_pos = image_pos
image_pos = self._picture_size
self.photobooth.screen.blit(self.filter_photos[3][0], image_pos)
if self._current_filter_idx == 3:
rect_pos = image_pos
draw_rect(self.photobooth.screen, rect_pos, self._picture_size, color=None, color_border=COLOR_ORANGE,
size_border=8)
text_margin = 10
selected_image_text_pos = (rect_pos[0] + text_margin,rect_pos[1]+ text_margin)
draw_text_box(screen=self.photobooth.screen, text=_("Selected"), pos= selected_image_text_pos,
size=INFO_FONT_SIZE, box_color=None, border_color=COLOR_ORANGE, size_border=5, text_color=COLOR_ORANGE)
def update_callback(self):
# sometimes this can be reset from other threads, hence better set always
self.photobooth.io_manager.set_led(led_type=LedType.GREEN,led_state=LedState.ON)
self.photobooth.io_manager.set_led(led_type=LedType.RED, led_state=LedState.ON)
self.photobooth.io_manager.set_led(led_type=LedType.BLUE, led_state=LedState.ON)
self.photobooth.io_manager.set_led(led_type=LedType.YELLOW, led_state=LedState.ON)
self.draw_filtered_photos()
# handle filter selection
if self.photobooth.io_manager.next_button_pressed():
self._current_filter_idx += 1
self.set_counter(self.inital_counter)
if self.photobooth.io_manager.prev_button_pressed():
self._current_filter_idx -= 1
self.set_counter(self.inital_counter)
self._current_filter_idx = self._current_filter_idx % self._filter_count
if self.photobooth.io_manager.cancel_button_pressed():
self.switch_state(self.failure_state)
return
if self.photobooth.io_manager.accept_button_pressed():
self.filter_selected_photo()
self.switch_next()
return
draw_text_box(screen=self.photobooth.screen, text=_("Select photo?"), pos=(None, INFO_TEXT_Y_POS), size=INFO_FONT_SIZE)
draw_button_bar(self.photobooth.screen, text=[_("Cancel"), _("Prev"), _("Next"), _("Select")],
pos=(None,self.photobooth.app_resolution[1]-BUTTON_BAR_Y_OFFSET))
def filter_selected_photo(self):
# create final file name
path, ext = os.path.splitext(self.photobooth.last_photo[1])
filter_file = path + '_filtered' + ext
wait_thread = self._enable_wait_message()
# redo filtering on full image resolution
self.photobooth.last_photo = self.filter_photo_fullsize(photo=self.photobooth.last_photo,
idx=self._current_filter_idx, dest=filter_file)
self._disable_wait_message(wait_thread)
def reset(self):
super(StateFilter, self).reset()
wait_thread = self._enable_wait_message()
self._current_filter_idx = 0
self._picture_size = (self.photobooth.screen.get_size()[0] // 2, self.photobooth.screen.get_size()[1] // 2)
if self.photobooth.last_photo_resized:
self.create_filtered_photo_collection()
self._disable_wait_message(wait_thread)
class StateAdmin(PhotoBoothState):
"""
Administration info and command option state
"""
def __init__(self, photobooth, next_state, counter=2, state_showphoto=None, state_filter=None, state_printing=None):
super(StateAdmin, self).__init__(photobooth=photobooth, next_state=next_state, counter=counter, counter_callback=self.enable_input)
self.state_showphoto = state_showphoto
self.state_filter = state_filter
self.state_printing = state_printing
# we wait some seconds to handle button input to avoid missclicks due required button combo for enabling this mode
self.input_handling = False
self._options = [
(_("Return"), self.switch_next),
(_("Toggle fullscreen"), self.toggle_fullscreen),
(_("Enable/Disable state ShowPhoto"), self.toggle_state_showphoto),
(_("Enable/Disable state Filter"), self.toggle_state_filter),
(_("Enable/Disable state Printing"), self.toggle_state_printing),
(_("Close photobooth"), self.photobooth.close),
(_("Shutdown all"), self.shutdown_all),
(_("Shutdown photobooth"), self.shutdown_phootbooth),
(_("Create new photo directory"), self.create_new_image_dir),
#(_("Start printer"), self.start_printer), Wake-On-Lan not working with Win10
(_("Stop printer"), self.stop_printer),
(_("Mount/Umount USB storage"), self.toggle_usb_storage),
]
def create_new_image_dir(self):
if self.photobooth.photo_directory == self.photobooth.config['photo_directory']:
os.rename(self.photobooth.photo_directory, self.photobooth.photo_directory + '-' + str(datetime.now()).replace(':','-'))
os.makedirs(self.photobooth.photo_directory)
def enable_input(self):
self.input_handling = True
def toggle_state_showphoto(self):
"""
Enable/Disable show_photo state
"""
if self.state_showphoto:
self.state_showphoto.enabled = not self.state_showphoto.enabled
def toggle_state_filter(self):
"""
Enable/Disable filter state
:return:
"""
if self.state_filter:
self.state_filter.enabled = not self.state_filter.enabled
def toggle_fullscreen(self):
"""
Toggle app fullscreen mode
"""
self.photobooth.fullscreen = not self.photobooth.fullscreen
self.photobooth.set_fullscreen(self.photobooth.fullscreen)
def toggle_state_printing(self):
"""
Enable/Disable printing state
"""
if self.state_printing:
self.state_printing.enabled = not self.state_printing.enabled
def toggle_usb_storage(self):
if self.photobooth.photo_directory == self.photobooth.config['photo_directory']:
storage.umount_device(self.photobooth.config['usb_device'])
self.photobooth.change_photo_dir(storage.mount_device(self.photobooth.config['usb_device']))
else:
storage.umount_device(self.photobooth.config['usb_device'])
self.photobooth.change_photo_dir(self.photobooth.config['photo_directory'])
self.refresh_information()
def get_free_space(self):
"""
Determine free space in current directory
:return: free space in MB
"""
free_mb = 0
try:
st = os.statvfs(self.photobooth.photo_directory)
free_mb = st.f_bsize * st.f_bavail // 1024 // 1024
except:
print(traceback.format_exc())
return free_mb
def get_ip_address(self):
socket_name = _("WiFi not found")
try:
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.connect(("8.8.8.8", 80))
socket_name = s.getsockname()[0]
except:
print(_("WiFi not found"))
return socket_name
def get_network_name(self):
ssid = _("WiFi not found")
try:
scanoutput = check_output(["iwconfig", "wlan0"])
for line in scanoutput.split():
line = line.decode("utf-8")
if line[:5] == "ESSID":
ssid = line.split('"')[1]
except:
print(_("WiFi not found"))
return ssid
def get_number_taken_photos(self):
return len(glob.glob(self.photobooth.photo_directory + "/*.jpg"))
def update_callback(self):
# Background
draw_rect(self.photobooth.screen,(10,10),(self.photobooth.app_resolution[0]-20, self.photobooth.app_resolution[1]-20))
x_pos = 20
y_pos= INFO_TEXT_Y_POS
# Caption
show_text_left(self.photobooth.screen, _("Administration"), (x_pos, y_pos), INFO_FONT_SIZE)
y_pos+=30
#Infos
show_text_left(self.photobooth.screen, _("Fullscreen: ") + str(self.photobooth.fullscreen), (x_pos, y_pos),
INFO_SMALL_FONT_SIZE, color=COLOR_DARK_GREY)
y_pos += 30
show_text_left(self.photobooth.screen, _("Free space: ") + str(self._free_space) + "MB", (x_pos, y_pos), INFO_SMALL_FONT_SIZE, color=COLOR_DARK_GREY)
y_pos += 30
show_text_left(self.photobooth.screen, _("Network: ") + str(self._network), (x_pos, y_pos), INFO_SMALL_FONT_SIZE,
color=COLOR_DARK_GREY)
y_pos += 30
show_text_left(self.photobooth.screen, _("IP: ") + str(self._ip_address), (x_pos, y_pos), INFO_SMALL_FONT_SIZE, color=COLOR_DARK_GREY)
y_pos += 30
show_text_left(self.photobooth.screen, _("Printer available: ") + str(self._printer_state), (x_pos, y_pos), INFO_SMALL_FONT_SIZE, color=COLOR_DARK_GREY)