fix: Improve image decoding by using original format if available
This commit is contained in:
@@ -7,6 +7,7 @@ with a focus on image and text data conversion:
|
|||||||
- Text encoding/decoding (multiple character sets)
|
- Text encoding/decoding (multiple character sets)
|
||||||
- Bytes conversion utilities
|
- Bytes conversion utilities
|
||||||
"""
|
"""
|
||||||
|
|
||||||
import base64
|
import base64
|
||||||
import binascii
|
import binascii
|
||||||
import io
|
import io
|
||||||
@@ -59,7 +60,9 @@ def decode_image(image: Union[str, bytes, Image.Image, np.ndarray]) -> str:
|
|||||||
elif isinstance(image, Image.Image):
|
elif isinstance(image, Image.Image):
|
||||||
# Handle PIL Image: save to buffer then encode
|
# Handle PIL Image: save to buffer then encode
|
||||||
buffer = io.BytesIO()
|
buffer = io.BytesIO()
|
||||||
image.save(buffer, format=image.format)
|
# Use original format if available, otherwise default to PNG
|
||||||
|
img_format = image.format if image.format else "PNG"
|
||||||
|
image.save(buffer, format=img_format)
|
||||||
return base64.b64encode(buffer.getvalue()).decode()
|
return base64.b64encode(buffer.getvalue()).decode()
|
||||||
|
|
||||||
elif isinstance(image, np.ndarray):
|
elif isinstance(image, np.ndarray):
|
||||||
|
|||||||
Reference in New Issue
Block a user