Remove thumbnail image, web image for each full image.
authorBenjamin Braatz <benjamin.braatz@graph-it.com>
Wed, 4 Aug 2021 09:12:23 +0000 (11:12 +0200)
committerBenjamin Braatz <benjamin.braatz@graph-it.com>
Wed, 4 Aug 2021 09:17:45 +0000 (11:17 +0200)
controlpi_plugins/camera.py

index 065f6f1643f5ecee278cdbe21bf2283a62892ff8..26ada0426186dd81c9e7fbfbb7f26e7cfc3b61a7 100644 (file)
@@ -23,17 +23,15 @@ class Camera(BasePlugin):
                     'full': {'type': 'array', 'items': {'type': 'integer'},
                              'minItems': 2, 'maxItems': 2},
                     'web': {'type': 'array', 'items': {'type': 'integer'},
-                            'minItems': 2, 'maxItems': 2},
-                    'thumb': {'type': 'array', 'items': {'type': 'integer'},
-                              'minItems': 2, 'maxItems': 2}},
+                            'minItems': 2, 'maxItems': 2}},
                    'required': ['pause', 'keep', 'path']}
 
     async def _receive(self, message: Message) -> None:
         if message['command'] == 'get image':
             if len(self._images) > 0:
                 await self.bus.send(Message(self.name,
-                                            {'image': self._images[0][0],
-                                             'date': self._images[0][1]}))
+                                            {'image': self._images[-1][0],
+                                             'date': self._images[-1][1]}))
         elif message['command'] == 'start capture':
             self._capture = True
         elif message['command'] == 'stop capture':
@@ -52,9 +50,6 @@ class Camera(BasePlugin):
         self._web = (640, 480)
         if 'web' in self.conf:
             self._web = self.conf['web']
-        self._thumb = (160, 120)
-        if 'thumb' in self.conf:
-            self._thumb = self.conf['thumb']
         sends = [MessageTemplate({'event': {'const': 'new image'},
                                   'image': {'type': 'string'},
                                   'date': {'type': 'string'}}),
@@ -75,8 +70,6 @@ class Camera(BasePlugin):
         camera = picamera.PiCamera()
         camera.iso = self._iso
         camera.resolution = self._full
-        web_path = os.path.join(self.conf['path'], 'web.jpg')
-        thumb_path = os.path.join(self.conf['path'], 'thumb.jpg')
         try:
             while True:
                 if self._capture:
@@ -92,14 +85,22 @@ class Camera(BasePlugin):
                     camera.awb_gains = gains
                     while self._capture:
                         while len(self._images) >= self.conf['keep']:
-                            file_name = self._images.popleft()[0]
-                            file_path = os.path.join(self.conf['path'],
-                                                     file_name)
-                            await aiofiles.os.remove(file_path)
+                            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)
                         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)
                         stream = io.BytesIO()
                         await loop.run_in_executor(
                                 executor,
@@ -123,16 +124,6 @@ class Camera(BasePlugin):
                                 executor,
                                 functools.partial(
                                     web_image.save, web_path))
-                        thumb_image = await loop.run_in_executor(
-                                executor,
-                                functools.partial(
-                                    full_image.resize,
-                                    self._thumb,
-                                    PIL.Image.ANTIALIAS))
-                        await loop.run_in_executor(
-                                executor,
-                                functools.partial(
-                                    thumb_image.save, thumb_path))
                         stream.close()
                         self._images.append((full_name, iso_time))
                         await self.bus.send(Message(self.name,
@@ -146,13 +137,13 @@ class Camera(BasePlugin):
             camera.stop_preview()
             camera.close()
             while len(self._images) > 0:
-                file_name = self._images.popleft()[0]
-                file_path = os.path.join(self.conf['path'],
-                                         file_name)
-                os.remove(file_path)
-            if os.path.isfile(web_path):
-                os.remove(web_path)
-            if os.path.isfile(thumb_path):
-                os.remove(thumb_path)
+                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)
             executor.shutdown(True)
             raise