Added rotation of camera, closes #45

This commit is contained in:
Balthasar Reuter
2018-08-23 15:57:01 +02:00
parent af0c62ab0f
commit 8241189b0c
3 changed files with 36 additions and 3 deletions

View File

@@ -52,6 +52,10 @@ class Camera:
self._is_preview = self._cfg.getBool('Photobooth', 'show_preview') self._is_preview = self._cfg.getBool('Photobooth', 'show_preview')
self._is_keep_pictures = self._cfg.getBool('Storage', 'keep_pictures') 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): def startup(self):
self._cap = self._cam() self._cap = self._cam()
@@ -59,8 +63,11 @@ class Camera:
logging.info('Using camera {} preview functionality'.format( logging.info('Using camera {} preview functionality'.format(
'with' if self._is_preview else 'without')) 'with' if self._is_preview else 'without'))
self._pic_dims = PictureDimensions(self._cfg, test_picture = self._cap.getPicture()
self._cap.getPicture().size) 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 self._is_preview = self._is_preview and self._cap.hasPreview
background = self._cfg.get('Picture', 'background') background = self._cfg.get('Picture', 'background')
@@ -120,8 +127,11 @@ class Camera:
if self._is_preview: if self._is_preview:
while self._comm.empty(Workers.CAMERA): 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 = picture.resize(self._pic_dims.previewSize)
picture = ImageOps.mirror(picture)
self._comm.send(Workers.GUI, self._comm.send(Workers.GUI,
StateMachine.CameraEvent('preview', picture)) StateMachine.CameraEvent('preview', picture))
@@ -129,6 +139,8 @@ class Camera:
self.setIdle() self.setIdle()
picture = self._cap.getPicture() picture = self._cap.getPicture()
if self._rotation is not None:
picture = picture.transpose(self._rotation)
self._pictures.append(picture) self._pictures.append(picture)
self.setActive() self.setActive()

View File

@@ -16,6 +16,8 @@ style = default
# Camera module to use (python-gphoto2, gphoto2-cffi, gphoto2-commandline, # Camera module to use (python-gphoto2, gphoto2-cffi, gphoto2-commandline,
# opencv, picamera, dummy) # opencv, picamera, dummy)
module = python-gphoto2 module = python-gphoto2
# Specify rotation of camera in degree (possible values: 0, 90, 180, 270)
rotation = 0
[Gpio] [Gpio]
# Enable use of GPIO (True/False) # Enable use of GPIO (True/False)

View File

@@ -613,8 +613,25 @@ class Settings(QtWidgets.QFrame):
self._cfg.get('Camera', 'module')) self._cfg.get('Camera', 'module'))
self.add('Camera', 'module', 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 = QtWidgets.QFormLayout()
layout.addRow(_('Camera module:'), module) layout.addRow(_('Camera module:'), module)
layout.addRow(_('Camera rotation:'), rotation)
widget = QtWidgets.QWidget() widget = QtWidgets.QWidget()
widget.setLayout(layout) widget.setLayout(layout)
@@ -862,6 +879,8 @@ class Settings(QtWidgets.QFrame):
self._cfg.set('Camera', 'module', self._cfg.set('Camera', 'module',
camera.modules[self.get('Camera', camera.modules[self.get('Camera',
'module').currentIndex()][0]) '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_x', self.get('Picture', 'num_x').text())
self._cfg.set('Picture', 'num_y', self.get('Picture', 'num_y').text()) self._cfg.set('Picture', 'num_y', self.get('Picture', 'num_y').text())