From 8241189b0c11b39f54c32100c47e5e76bcf81113 Mon Sep 17 00:00:00 2001 From: Balthasar Reuter Date: Thu, 23 Aug 2018 15:57:01 +0200 Subject: [PATCH] Added rotation of camera, closes #45 --- photobooth/camera/__init__.py | 18 +++++++++++++++--- photobooth/defaults.cfg | 2 ++ photobooth/gui/Qt5Gui/Frames.py | 19 +++++++++++++++++++ 3 files changed, 36 insertions(+), 3 deletions(-) diff --git a/photobooth/camera/__init__.py b/photobooth/camera/__init__.py index 1f2c9de..538e12c 100644 --- a/photobooth/camera/__init__.py +++ b/photobooth/camera/__init__.py @@ -52,6 +52,10 @@ class Camera: self._is_preview = self._cfg.getBool('Photobooth', 'show_preview') self._is_keep_pictures = self._cfg.getBool('Storage', 'keep_pictures') + rot_vals = {0: None, 90: Image.ROTATE_90, 180: Image.ROTATE_180, + 270: Image.ROTATE_270} + self._rotation = rot_vals[self._cfg.getInt('Camera', 'rotation')] + def startup(self): self._cap = self._cam() @@ -59,8 +63,11 @@ class Camera: logging.info('Using camera {} preview functionality'.format( 'with' if self._is_preview else 'without')) - self._pic_dims = PictureDimensions(self._cfg, - self._cap.getPicture().size) + test_picture = self._cap.getPicture() + if self._rotation is not None: + test_picture = test_picture.transpose(self._rotation) + + self._pic_dims = PictureDimensions(self._cfg, test_picture.size) self._is_preview = self._is_preview and self._cap.hasPreview background = self._cfg.get('Picture', 'background') @@ -120,8 +127,11 @@ class Camera: if self._is_preview: while self._comm.empty(Workers.CAMERA): - picture = ImageOps.mirror(self._cap.getPreview()) + picture = self._cap.getPreview() + if self._rotation is not None: + picture = picture.transpose(self._rotation) picture = picture.resize(self._pic_dims.previewSize) + picture = ImageOps.mirror(picture) self._comm.send(Workers.GUI, StateMachine.CameraEvent('preview', picture)) @@ -129,6 +139,8 @@ class Camera: self.setIdle() picture = self._cap.getPicture() + if self._rotation is not None: + picture = picture.transpose(self._rotation) self._pictures.append(picture) self.setActive() diff --git a/photobooth/defaults.cfg b/photobooth/defaults.cfg index b1bc399..bdf76ec 100644 --- a/photobooth/defaults.cfg +++ b/photobooth/defaults.cfg @@ -16,6 +16,8 @@ style = default # Camera module to use (python-gphoto2, gphoto2-cffi, gphoto2-commandline, # opencv, picamera, dummy) module = python-gphoto2 +# Specify rotation of camera in degree (possible values: 0, 90, 180, 270) +rotation = 0 [Gpio] # Enable use of GPIO (True/False) diff --git a/photobooth/gui/Qt5Gui/Frames.py b/photobooth/gui/Qt5Gui/Frames.py index 905d66e..b1c86b3 100644 --- a/photobooth/gui/Qt5Gui/Frames.py +++ b/photobooth/gui/Qt5Gui/Frames.py @@ -613,8 +613,25 @@ class Settings(QtWidgets.QFrame): self._cfg.get('Camera', 'module')) self.add('Camera', 'module', module) + self.rot_vals_ = (0, 90, 180, 270) + cur_rot = self._cfg.getInt('Camera', 'rotation') + + rotation = QtWidgets.QComboBox() + for r in self.rot_vals_: + rotation.addItem(str(r)) + + idx = [x for x, r in enumerate(self.rot_vals_) if r == cur_rot] + rotation.setCurrentIndex(idx[0] if len(idx) > 0 else -1) + + # Fix bug in Qt to allow changing the items in a stylesheet + delegate = QtWidgets.QStyledItemDelegate() + rotation.setItemDelegate(delegate) + + self.add('Camera', 'rotation', rotation) + layout = QtWidgets.QFormLayout() layout.addRow(_('Camera module:'), module) + layout.addRow(_('Camera rotation:'), rotation) widget = QtWidgets.QWidget() widget.setLayout(layout) @@ -862,6 +879,8 @@ class Settings(QtWidgets.QFrame): self._cfg.set('Camera', 'module', camera.modules[self.get('Camera', 'module').currentIndex()][0]) + self._cfg.set('Camera', 'rotation', str( + self.rot_vals_[self.get('Camera', 'rotation').currentIndex()])) self._cfg.set('Picture', 'num_x', self.get('Picture', 'num_x').text()) self._cfg.set('Picture', 'num_y', self.get('Picture', 'num_y').text())