summaryrefslogtreecommitdiffstats
path: root/g4f/image.py
diff options
context:
space:
mode:
authorH Lohaus <hlohaus@users.noreply.github.com>2024-04-22 01:35:07 +0200
committerGitHub <noreply@github.com>2024-04-22 01:35:07 +0200
commit4b4d1f08b5c75c8c8932b5edfbb0d020f8e029a7 (patch)
tree6ed0cfc6cd53a3ab32565d6199a929ac1ea6ad80 /g4f/image.py
parentMerge pull request #1869 from hlohaus/carst (diff)
parentAdd vision models to readme (diff)
downloadgpt4free-4b4d1f08b5c75c8c8932b5edfbb0d020f8e029a7.tar
gpt4free-4b4d1f08b5c75c8c8932b5edfbb0d020f8e029a7.tar.gz
gpt4free-4b4d1f08b5c75c8c8932b5edfbb0d020f8e029a7.tar.bz2
gpt4free-4b4d1f08b5c75c8c8932b5edfbb0d020f8e029a7.tar.lz
gpt4free-4b4d1f08b5c75c8c8932b5edfbb0d020f8e029a7.tar.xz
gpt4free-4b4d1f08b5c75c8c8932b5edfbb0d020f8e029a7.tar.zst
gpt4free-4b4d1f08b5c75c8c8932b5edfbb0d020f8e029a7.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,