(self, image_data, metadata=None, sid=None)
| 1299 | await self.send_bytes(BinaryEventTypes.PREVIEW_IMAGE, preview_bytes, sid=sid) |
| 1300 | |
| 1301 | async def send_image_with_metadata(self, image_data, metadata=None, sid=None): |
| 1302 | image_type = image_data[0] |
| 1303 | image = image_data[1] |
| 1304 | max_size = image_data[2] |
| 1305 | if max_size is not None: |
| 1306 | if hasattr(Image, 'Resampling'): |
| 1307 | resampling = Image.Resampling.BILINEAR |
| 1308 | else: |
| 1309 | resampling = Image.Resampling.LANCZOS |
| 1310 | |
| 1311 | image = ImageOps.contain(image, (max_size, max_size), resampling) |
| 1312 | |
| 1313 | mimetype = "image/png" if image_type == "PNG" else "image/jpeg" |
| 1314 | |
| 1315 | # Prepare metadata |
| 1316 | if metadata is None: |
| 1317 | metadata = {} |
| 1318 | metadata["image_type"] = mimetype |
| 1319 | |
| 1320 | # Serialize metadata as JSON |
| 1321 | import json |
| 1322 | metadata_json = json.dumps(metadata).encode('utf-8') |
| 1323 | metadata_length = len(metadata_json) |
| 1324 | |
| 1325 | # Prepare image data |
| 1326 | bytesIO = BytesIO() |
| 1327 | image.save(bytesIO, format=image_type, quality=95, compress_level=1) |
| 1328 | image_bytes = bytesIO.getvalue() |
| 1329 | |
| 1330 | # Combine metadata and image |
| 1331 | combined_data = bytearray() |
| 1332 | combined_data.extend(struct.pack(">I", metadata_length)) |
| 1333 | combined_data.extend(metadata_json) |
| 1334 | combined_data.extend(image_bytes) |
| 1335 | |
| 1336 | await self.send_bytes(BinaryEventTypes.PREVIEW_IMAGE_WITH_METADATA, combined_data, sid=sid) |
| 1337 | |
| 1338 | async def send_bytes(self, event, data, sid=None): |
| 1339 | message = self.encode_bytes(event, data) |
no test coverage detected