From e658771b2f61c92d43591a6b588dac6b5bd491ca Mon Sep 17 00:00:00 2001 From: Benjamin Braatz Date: Wed, 4 Aug 2021 14:02:47 +0200 Subject: [PATCH] Settle on PIL usage. --- controlpi_plugins/camera.py | 56 ++++++++++--------------------------- 1 file changed, 15 insertions(+), 41 deletions(-) diff --git a/controlpi_plugins/camera.py b/controlpi_plugins/camera.py index 28f12ca..e136968 100644 --- a/controlpi_plugins/camera.py +++ b/controlpi_plugins/camera.py @@ -8,7 +8,6 @@ import io import os import picamera # type: ignore import PIL.Image # type: ignore -import time from controlpi import BasePlugin, Message, MessageTemplate @@ -72,33 +71,15 @@ class Camera(BasePlugin): camera.awb_gains = gains while self._capture: while len(self._images) >= self.conf['keep']: - full_name = self._images.popleft()[0] - full_path = os.path.join(self.conf['path'], - full_name) - if os.path.isfile(full_path): - await aiofiles.os.remove(full_path) - web_name = 'web-'+full_name - web_path = os.path.join(self.conf['path'], - web_name) - if os.path.isfile(web_path): - await aiofiles.os.remove(web_path) + file_name = self._images.popleft()[0] + file_path = os.path.join(self.conf['path'], + file_name) + if os.path.isfile(file_path): + await aiofiles.os.remove(file_path) timestamp = datetime.datetime.now() iso_time = timestamp.strftime('%Y-%m-%d %H:%M:%S') - full_name = timestamp.strftime('%Y%m%d%H%M%S%f')+'.jpg' - web_name = 'web-'+full_name - full_path = os.path.join(self.conf['path'], full_name) - web_path = os.path.join(self.conf['path'], web_name) - start_direct = time.perf_counter() - await loop.run_in_executor( - executor, - functools.partial( - camera.capture, - full_path, - 'jpeg', - use_video_port=True)) - end_direct = time.perf_counter() - print(f"Direct: {end_direct - start_direct}") - start_pil = time.perf_counter() + file_name = timestamp.strftime('%Y%m%d%H%M%S%f')+'.jpg' + file_path = os.path.join(self.conf['path'], file_name) stream = io.BytesIO() await loop.run_in_executor( executor, @@ -108,20 +89,17 @@ class Camera(BasePlugin): 'jpeg', use_video_port=True)) stream.seek(0) - full_image = await loop.run_in_executor( + image = await loop.run_in_executor( executor, functools.partial(PIL.Image.open, stream)) await loop.run_in_executor( executor, - functools.partial( - full_image.save, web_path)) + functools.partial(image.save, file_path)) stream.close() - end_pil = time.perf_counter() - print(f"PIL: {end_pil - start_pil}") - self._images.append((full_name, iso_time)) + self._images.append((file_name, iso_time)) await self.bus.send(Message(self.name, {'event': 'new image', - 'image': full_name, + 'image': file_name, 'date': iso_time})) await asyncio.sleep(self.conf['pause']) camera.stop_preview() @@ -130,13 +108,9 @@ class Camera(BasePlugin): camera.stop_preview() camera.close() while len(self._images) > 0: - full_name = self._images.popleft()[0] - full_path = os.path.join(self.conf['path'], full_name) - if os.path.isfile(full_path): - os.remove(full_path) - web_name = 'web-'+full_name - web_path = os.path.join(self.conf['path'], web_name) - if os.path.isfile(web_path): - os.remove(web_path) + file_name = self._images.popleft()[0] + file_path = os.path.join(self.conf['path'], file_name) + if os.path.isfile(file_path): + os.remove(file_path) executor.shutdown(True) raise -- 2.34.1