From 67922dcca2599e9b282f51198fec99647e1fef51 Mon Sep 17 00:00:00 2001 From: Brendan Howell Date: Sun, 14 May 2017 17:58:23 +0200 Subject: [PATCH] cropping and rotating based on switch states --- screenless/bureau/photography/photography.py | 53 ++++++++++++++++++-- 1 file changed, 50 insertions(+), 3 deletions(-) diff --git a/screenless/bureau/photography/photography.py b/screenless/bureau/photography/photography.py index f16fc6d..a9d6fa8 100644 --- a/screenless/bureau/photography/photography.py +++ b/screenless/bureau/photography/photography.py @@ -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) @@ -83,7 +103,34 @@ class Photography(Bureau): filelist[i] = os.path.join("/tmp/webcam", f) 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}