Settle on PIL usage.
authorBenjamin Braatz <benjamin.braatz@graph-it.com>
Wed, 4 Aug 2021 12:02:47 +0000 (14:02 +0200)
committerBenjamin Braatz <benjamin.braatz@graph-it.com>
Wed, 4 Aug 2021 12:02:47 +0000 (14:02 +0200)
controlpi_plugins/camera.py

index 28f12cad5d303a154e035ba8d7dd782aa9a8ee97..e13696862d76fcc1e3793b7f77cf255888e14b13 100644 (file)
@@ -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