-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathphotobooth_handler.py
executable file
·182 lines (149 loc) · 4.82 KB
/
photobooth_handler.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
#!/usr/bin/env python3
"""
Handles watermarking of gphoto2 loaded image.
needs
Gphoto2HookScriptHandler
SimpleWatermark
"""
# import sys
import os
import subprocess
from contextlib import contextmanager
from gphoto2_script_hook_handler import Gphoto2HookScriptHandler
from watermark import SimpleWatermark
##########################################
# functions
@contextmanager
def cd(newdir):
"""
Change directory.
found at:
http://stackoverflow.com/questions/431684/how-do-i-cd-in-python/24176022#24176022
"""
prevdir = os.getcwd()
os.chdir(os.path.expanduser(newdir))
try:
yield
finally:
os.chdir(prevdir)
##########################################
# classes
class PhotoBoothHandler(Gphoto2HookScriptHandler):
"""docstring for PhotoBoothHandler."""
def __init__(self):
"""Initialize Instance."""
# print("PhotoBoothHandler")
self.path_script = os.path.dirname(os.path.abspath(__file__))
print("path_script", self.path_script)
super(PhotoBoothHandler, self).__init__()
def __del__(self):
"""Clean up."""
pass
def download(self, argument):
"""Photo downloaded and ready to work on."""
print("gphoto2 donwload")
if argument:
# print("ARGUMENT:", os.environ['ARGUMENT'])
input_filename = os.environ['ARGUMENT']
output_filename = input_filename
watermark_filename = "./mark2.png"
myMarker = SimpleWatermark(
input_filename=input_filename,
output_filename=output_filename,
watermark_filename=watermark_filename
)
myMarker.load()
myMarker.process()
myMarker.save()
self.stop_slideshow()
self.start_slideshow(output_filename)
def start(self, argument):
"""gPhoto2 started."""
self.start_slideshow()
def stop(self, argument):
"""gPhoto2 started."""
self.stop_slideshow()
def stop_slideshow(self):
"""Stop Slideshow."""
print("stop slideshow:")
command = [
"killall",
"feh",
]
result_string = ""
try:
print("command:{}".format(" ".join(command)))
# result_string += subprocess.check_output(command).decode()
pass
except subprocess.CalledProcessError as e:
error_message = "failed: {}".format(e)
print(error_message)
result_string += "\n" + error_message
else:
pass
return result_string
def start_slideshow(self, startfile=None):
"""Restart Slideshow."""
print("start slideshow:")
duration_new_picture = 5.0
duration_loop = 1.0
target_path = self.path_script
image_directory = "./captured/"
# image_directory = target_path + "/captured/"
# image_directory = "./"
# https://man.finalrewind.org/1/feh/
command_start = [
"feh",
"--fullscreen",
"--cycle-once",
"--slideshow-delay={}".format(duration_new_picture),
"{}".format(startfile),
]
command_loop = [
"feh",
"--fullscreen",
"--randomize",
"--slideshow-delay={}".format(duration_loop),
"{}".format(image_directory),
]
command = []
if startfile:
command.extend(command_start)
command.append("&&")
command.extend(command_loop)
else:
command = command_loop
result_string = ""
try:
print("command:{}".format(" ".join(command)))
with cd(target_path):
# result_string += subprocess.check_output(command).decode()
# this does not work with the && combining of commands..
# subprocess.Popen(command)
# subprocess.Popen(command, shell=True)
# command = " ".join(command)
# subprocess.Popen(command, shell=True)
# subprocess.run(command)
pass
# subprocess.run(command, shell=True)
# subprocess.run(command)
# result_string += subprocess.check_output(command).decode()
except subprocess.CalledProcessError as e:
error_message = "failed: {}".format(e)
print(error_message)
result_string += "\n" + error_message
else:
pass
return result_string
##########################################
# globals
myPBHandler = None
##########################################
# main
def main():
"""Run Main SW."""
global myPBHandler
myPBHandler = PhotoBoothHandler()
# myPBHandler.start_slideshow()
if __name__ == "__main__":
main()