summaryrefslogtreecommitdiffstats
path: root/g4f/image.py
diff options
context:
space:
mode:
authorH Lohaus <hlohaus@users.noreply.github.com>2024-02-09 05:53:47 +0100
committerGitHub <noreply@github.com>2024-02-09 05:53:47 +0100
commit999bc2d617a70ad70d185ba59c108d01794f9b5f (patch)
treed27a53d1549b352c8ab723680d51f01db6ae9d5c /g4f/image.py
parentMerge pull request #1554 from Masha/patch-1 (diff)
parentAdd example for Image Upload & Generation (diff)
downloadgpt4free-999bc2d617a70ad70d185ba59c108d01794f9b5f.tar
gpt4free-999bc2d617a70ad70d185ba59c108d01794f9b5f.tar.gz
gpt4free-999bc2d617a70ad70d185ba59c108d01794f9b5f.tar.bz2
gpt4free-999bc2d617a70ad70d185ba59c108d01794f9b5f.tar.lz
gpt4free-999bc2d617a70ad70d185ba59c108d01794f9b5f.tar.xz
gpt4free-999bc2d617a70ad70d185ba59c108d01794f9b5f.tar.zst
gpt4free-999bc2d617a70ad70d185ba59c108d01794f9b5f.zip
Diffstat (limited to 'g4f/image.py')
-rw-r--r--g4f/image.py25
1 files changed, 16 insertions, 9 deletions
diff --git a/g4f/image.py b/g4f/image.py
index 1a4692b3..f0ee0395 100644
--- a/g4f/image.py
+++ b/g4f/image.py
@@ -46,9 +46,8 @@ def to_image(image: ImageType, is_svg: bool = False) -> Image:
return open_image(BytesIO(image))
elif not isinstance(image, Image):
image = open_image(image)
- copy = image.copy()
- copy.format = image.format
- return copy
+ image.load()
+ return image
return image
def is_allowed_extension(filename: str) -> bool:
@@ -210,20 +209,28 @@ def format_images_markdown(images, alt: str, preview: str = None) -> str:
end_flag = "<!-- generated images end -->\n"
return f"\n{start_flag}{images}\n{end_flag}\n"
-def to_bytes(image: Image) -> bytes:
+def to_bytes(image: ImageType) -> bytes:
"""
Converts the given image to bytes.
Args:
- image (Image.Image): The image to convert.
+ image (ImageType): The image to convert.
Returns:
bytes: The image as bytes.
"""
- bytes_io = BytesIO()
- image.save(bytes_io, image.format)
- image.seek(0)
- return bytes_io.getvalue()
+ if isinstance(image, bytes):
+ return image
+ elif isinstance(image, str):
+ is_data_uri_an_image(image)
+ return extract_data_uri(image)
+ elif isinstance(image, Image):
+ bytes_io = BytesIO()
+ image.save(bytes_io, image.format)
+ image.seek(0)
+ return bytes_io.getvalue()
+ else:
+ return image.read()
class ImageResponse:
def __init__(