summaryrefslogtreecommitdiffstats
path: root/g4f/image.py
diff options
context:
space:
mode:
authorHeiner Lohaus <hlohaus@users.noreply.github.com>2024-04-22 01:27:48 +0200
committerHeiner Lohaus <hlohaus@users.noreply.github.com>2024-04-22 01:27:48 +0200
commit4bc4d635bca9c1c7633ff87ff24b757c653ff60f (patch)
tree6ed0cfc6cd53a3ab32565d6199a929ac1ea6ad80 /g4f/image.py
parentImprove python support (diff)
downloadgpt4free-4bc4d635bca9c1c7633ff87ff24b757c653ff60f.tar
gpt4free-4bc4d635bca9c1c7633ff87ff24b757c653ff60f.tar.gz
gpt4free-4bc4d635bca9c1c7633ff87ff24b757c653ff60f.tar.bz2
gpt4free-4bc4d635bca9c1c7633ff87ff24b757c653ff60f.tar.lz
gpt4free-4bc4d635bca9c1c7633ff87ff24b757c653ff60f.tar.xz
gpt4free-4bc4d635bca9c1c7633ff87ff24b757c653ff60f.tar.zst
gpt4free-4bc4d635bca9c1c7633ff87ff24b757c653ff60f.zip
Diffstat (limited to 'g4f/image.py')
-rw-r--r--g4f/image.py9
1 files changed, 8 insertions, 1 deletions
diff --git a/g4f/image.py b/g4f/image.py
index ed8af103..270b59ad 100644
--- a/g4f/image.py
+++ b/g4f/image.py
@@ -86,7 +86,7 @@ def is_data_uri_an_image(data_uri: str) -> bool:
if image_format not in ALLOWED_EXTENSIONS and image_format != "svg+xml":
raise ValueError("Invalid image format (from mime file type).")
-def is_accepted_format(binary_data: bytes) -> bool:
+def is_accepted_format(binary_data: bytes) -> str:
"""
Checks if the given binary data represents an image with an accepted format.
@@ -241,6 +241,13 @@ def to_bytes(image: ImageType) -> bytes:
else:
return image.read()
+def to_data_uri(image: ImageType) -> str:
+ if not isinstance(image, str):
+ data = to_bytes(image)
+ data_base64 = base64.b64encode(data).decode()
+ return f"data:{is_accepted_format(data)};base64,{data_base64}"
+ return image
+
class ImageResponse:
def __init__(
self,