-
Notifications
You must be signed in to change notification settings - Fork 0
/
booth.py
180 lines (135 loc) · 4.36 KB
/
booth.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
import time
import picamera
import picamera.array
from PIL import Image
from PIL import ImageFont
from PIL import ImageDraw
from time import sleep
import RPi.GPIO as gpio
import io
import string
from subprocess import Popen, PIPE
from subprocess import call
import numpy as np
def white_balance():
# custom white balance
camera.awb_mode = 'off'
# Start off with ridiculously low gains
rg, bg = (0.5, 0.5)
camera.awb_gains = (rg, bg)
with picamera.array.PiRGBArray(camera, size=(128, 72)) as output:
# Allow 30 attempts to fix AWB
for i in range(30):
# Capture a tiny resized image in RGB format, and extract the
# average R, G, and B values
camera.capture(output, format='rgb', resize=(128, 72), use_video_port=True)
r, g, b = (np.mean(output.array[..., i]) for i in range(3))
print('R:%5.2f, B:%5.2f = (%5.2f, %5.2f, %5.2f)' % (
rg, bg, r, g, b))
# Adjust R and B relative to G, but only if they're significantly
# different (delta +/- 2)
if abs(r - g) > 2:
if r > g:
rg -= 0.1
else:
rg += 0.1
if abs(b - g) > 1:
if b > g:
bg -= 0.1
else:
bg += 0.1
camera.awb_gains = (rg, bg)
output.seek(0)
output.truncate()
def display_overlay_timeout(file, delay):
img = Image.open(file)
o = display_overlay_image(img)
sleep(delay)
camera.remove_overlay(o)
def display_overlay_image(img):
pad = Image.new('RGB', (
((img.size[0] + 31) // 32) * 32,
((img.size[1] + 15) // 16) * 16,
))
pad.paste(img, (0, 0))
o = camera.add_overlay(pad.tostring(), size=img.size)
o.alpha = 128
o.layer = 3
return o
def start_capture():
print " capture "
display_overlay_timeout('overlay_3.png',1)
display_overlay_timeout('overlay_2.png',1)
display_overlay_timeout('overlay_1.png',1)
# capture to stream
stream = io.BytesIO()
camera.capture(stream, format='jpeg')
stream.seek(0)
img = Image.open(stream)
o = display_overlay_image(img)
top = (height /2) - (crop_size() /2)
left = (width /2) -(crop_size() /2)
# debug info
print "height "+str(height)
print "width "+ str(width)
print crop_size(),top, left
img = img.crop((left, top, left+crop_size(),top+crop_size()))
img.save('out.jpg')
camera.remove_overlay(o)
img.close();
stream = None
#camera.capture('foo.jpg')
#-o media=4X6FULL -o position=top -o orientation-requested=1 -o mediatype=PMPHOTO_HIGH
# print quality parameters config in cups.
img = Image.open("out.jpg")
display_overlay_timeout("develop.png",5)
output = Image.new('RGBA',(img.size[0],img.size[1]+50),"white")
draw = ImageDraw.Draw(output)
font = ImageFont.truetype("/usr/share/fonts/truetype/dejavu/DejaVuSans-Bold.ttf", 32)
h = int(img.size[1])+15
draw.text((0,h),"Moi je ...",(0,0,0),font=font)
output.paste(img,(0,0))
output.save('sample-out.jpg')
#write out some text
call(["lp","sample-out.jpg","-o","position=top"])
output.close()
img.close();
#write out some text
def crop_size():
if (orientation == ORIENTATION_PORTRAIT):
return width
else:
return height
# initialize scrpt
GPIO_PIN=25
ORIENTATION_PORTRAIT = 0
ORIENTATION_LANDSCAPE = 1
gpio.setmode(gpio.BCM)
gpio.setup(GPIO_PIN, gpio.IN, pull_up_down=gpio.PUD_UP)
#detecting configured framebuffer size, you can force it in /boot/config.txt
p = Popen(['cat', '/sys/class/graphics/fb0/virtual_size'], stdin=PIPE, stdout=PIPE, stderr=PIPE)
output, err = p.communicate()
#print output,err
rc = p.returncode
width,height = string.split(output,",")
width = int(width)
height = int(height)
#print width,height
if height > width:
orientation = ORIENTATION_PORTRAIT
else:
orientation = ORIENTATION_LANDSCAPE
camera = picamera.PiCamera()
camera.led = False
camera.hflip = True
camera.resolution = (int(width), int(height))
camera.framerate = 24
camera.start_preview()
white_balance()
# Wait indefinitely until the user terminates the script
while True:
input = gpio.input(GPIO_PIN)
if input == False:
print "button"
start_capture()
time.sleep(0.1)