From c1b992c3460cb0069127524a9b987a8af475ec14 Mon Sep 17 00:00:00 2001 From: Heiner Lohaus Date: Thu, 8 Feb 2024 22:02:52 +0100 Subject: Add Gemini Provider with image upload and generation --- g4f/image.py | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) (limited to 'g4f/image.py') diff --git a/g4f/image.py b/g4f/image.py index 1a4692b3..3f26f75f 100644 --- a/g4f/image.py +++ b/g4f/image.py @@ -210,20 +210,28 @@ def format_images_markdown(images, alt: str, preview: str = None) -> str: end_flag = "\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__( -- cgit v1.2.3 From 47900f23718e398fc086a6dfbf6590b4c5859c28 Mon Sep 17 00:00:00 2001 From: Heiner Lohaus Date: Fri, 9 Feb 2024 03:31:05 +0100 Subject: Resolve images in Gemini Provider --- g4f/image.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) (limited to 'g4f/image.py') diff --git a/g4f/image.py b/g4f/image.py index 3f26f75f..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: -- cgit v1.2.3