|
1 | | -import base64 |
2 | | -import logging |
3 | | -from functools import partial |
4 | | -from io import BytesIO |
5 | | - |
6 | | -from PIL import Image |
7 | | - |
8 | | -test_image_base64 = "iVBORw0KGgoAAAANSUhEUgAAAGQAAABkCAIAAAD/gAIDAAAA6ElEQVR4nO3QwQ3AIBDAsIP9d25XIC+EZE8QZc18w5l9O+AlZgVmBWYFZgVmBWYFZgVmBWYFZgVmBWYFZgVmBWYFZgVmBWYFZgVmBWYFZgVmBWYFZgVmBWYFZgVmBWYFZgVmBWYFZgVmBWYFZgVmBWYFZgVmBWYFZgVmBWYFZgVmBWYFZgVmBWYFZgVmBWYFZgVmBWYFZgVmBWYFZgVmBWYFZgVmBWYFZgVmBWYFZgVmBWYFZgVmBWYFZgVmBWYFZgVmBWYFZgVmBWYFZgVmBWYFZgVmBWYFZgVmBWYFZgVmBWYFZgVmBT+IYAHHLHkdEgAAAABJRU5ErkJggg==" |
9 | | -test_image = base64.b64decode(test_image_base64) |
10 | | - |
11 | | - |
12 | | -async def image2id(d: dict, storage_put_func: partial, objname:str, bucket:str="imagetemps"): |
13 | | - import logging |
14 | | - from io import BytesIO |
15 | | - import trio |
16 | | - from rag.svr.task_executor import minio_limiter |
17 | | - if not d.get("image"): |
18 | | - return |
19 | | - |
20 | | - with BytesIO() as output_buffer: |
21 | | - if isinstance(d["image"], bytes): |
22 | | - output_buffer.write(d["image"]) |
23 | | - output_buffer.seek(0) |
24 | | - else: |
25 | | - # If the image is in RGBA mode, convert it to RGB mode before saving it in JPEG format. |
26 | | - if d["image"].mode in ("RGBA", "P"): |
27 | | - converted_image = d["image"].convert("RGB") |
28 | | - d["image"] = converted_image |
29 | | - try: |
30 | | - d["image"].save(output_buffer, format='JPEG') |
31 | | - except OSError as e: |
32 | | - logging.warning( |
33 | | - "Saving image exception, ignore: {}".format(str(e))) |
34 | | - |
35 | | - async with minio_limiter: |
36 | | - await trio.to_thread.run_sync(lambda: storage_put_func(bucket=bucket, fnm=objname, binary=output_buffer.getvalue())) |
37 | | - d["img_id"] = f"{bucket}-{objname}" |
38 | | - if not isinstance(d["image"], bytes): |
39 | | - d["image"].close() |
40 | | - del d["image"] # Remove image reference |
41 | | - |
42 | | - |
43 | | -def id2image(image_id:str|None, storage_get_func: partial): |
44 | | - if not image_id: |
45 | | - return |
46 | | - arr = image_id.split("-") |
47 | | - if len(arr) != 2: |
48 | | - return |
49 | | - bkt, nm = image_id.split("-") |
50 | | - try: |
51 | | - blob = storage_get_func(bucket=bkt, filename=nm) |
52 | | - if not blob: |
53 | | - return |
54 | | - return Image.open(BytesIO(blob)) |
55 | | - except Exception as e: |
56 | | - logging.exception(e) |
| 1 | +# |
| 2 | +# Copyright 2024 The InfiniFlow Authors. All Rights Reserved. |
| 3 | +# |
| 4 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +# you may not use this file except in compliance with the License. |
| 6 | +# You may obtain a copy of the License at |
| 7 | +# |
| 8 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +# |
| 10 | +# Unless required by applicable law or agreed to in writing, software |
| 11 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +# See the License for the specific language governing permissions and |
| 14 | +# limitations under the License. |
| 15 | +# |
0 commit comments