cropping and rotating based on switch states

workspace
Brendan Howell 8 years ago
parent 240c9507c6
commit 67922dcca2

@ -5,6 +5,8 @@ import signal
import tempfile
import time
from PIL import Image
try:
from pyA20.gpio import gpio, port
except ImportError:
@ -13,7 +15,11 @@ except ImportError:
class Tmpobj():
pass
port = Tmpobj()
port.PG0 = 42
port.PB3 = 42
port.PB4 = 42
port.PB5 = 42
port.PB6 = 42
port.PB7 = 42
from bureau import Bureau, add_api
@ -27,7 +33,11 @@ class Photography(Bureau):
name = "Photography Dept."
prefix = "PX"
version = 0
doc_light_pin = port.PG0
landscape_pin = port.PB3
a3_pin = port.PB4
a4_pin = port.PB5
a5_pin = port.PB6
doc_light_pin = port.PB7
def __init__(self):
Bureau.__init__(self)
@ -37,6 +47,16 @@ class Photography(Bureau):
gpio.setcfg(self.doc_light_pin, gpio.OUTPUT)
gpio.output(self.doc_light_pin, gpio.LOW)
# set pins and pullups for switches
gpio.setcfg(self.landscape_pin, gpio.INPUT)
gpio.pullup(self.landscape_pin, gpio.PULLUP)
gpio.setcfg(self.a3_pin, gpio.INPUT)
gpio.pullup(self.a3_pin, gpio.PULLUP)
gpio.setcfg(self.a4_pin, gpio.INPUT)
gpio.pullup(self.a4_pin, gpio.PULLUP)
gpio.setcfg(self.a5_pin, gpio.INPUT)
gpio.pullup(self.a5_pin, gpio.PULLUP)
def _doc_light_on(self):
gpio.output(self.doc_light_pin, gpio.HIGH)
@ -84,6 +104,33 @@ class Photography(Bureau):
newest = max(filelist, key=lambda x: os.stat(x).st_mtime)
shutil.copyfile(newest, tmpimg.name)
# crop
img = Image.open(tmpimg.name)
#TODO: crop here to trim calibrated extra pixels
xsize, ysize = img.size
if gpio.input(self.a3_pin) == 0:
pass
if gpio.input(self.a4_pin) == 0:
offset = (xsize - ysize) / 2
box = (offset, 0, (offset + ysize), xsize / 2)
img = img.crop(box)
elif gpio.input(self.a5_pin) == 0:
offset = xsize / 4
box = (offset, 0, (offset + (xsize / 2)), ysize / 2)
img = img.crop(box)
else:
offset = (xsize - (ysize / 2)) / 2
box = (offset, 0, (offset + (ysize / 2)), xsize / 4)
img = img.crop(box)
# rotate
if gpio.input(self.landscape_pin) == 0:
img = img.transpose(Image.ROTATE_90)
img.save(tmpimg.name)
img.close()
self._doc_light_off()
return {"photo": tmpimg.name}

Loading…
Cancel
Save