diff --git a/backend/open_webui/routers/images.py b/backend/open_webui/routers/images.py index 8c6e772896..c894f2c651 100644 --- a/backend/open_webui/routers/images.py +++ b/backend/open_webui/routers/images.py @@ -509,8 +509,9 @@ async def get_image_data(data: str, headers=None, trusted_base_url: str | None = mime_type = header.split(';')[0].lstrip('data:') img_data = base64.b64decode(encoded) else: - mime_type = 'image/png' img_data = base64.b64decode(data) + with Image.open(io.BytesIO(img_data)) as image: + mime_type = Image.MIME.get(image.format, 'image/png') return img_data, mime_type except Exception as e: log.exception(f'Error loading image data: {e}') @@ -520,7 +521,7 @@ async def get_image_data(data: str, headers=None, trusted_base_url: str | None = async def upload_image(request, image_data, content_type, metadata, user, db=None): if image_data is None or content_type is None: raise ValueError('Failed to retrieve image data from the generation backend') - image_format = mimetypes.guess_extension(content_type) + image_format = IMAGE_FILE_EXTENSIONS.get(content_type.lower()) or mimetypes.guess_extension(content_type) or '.png' file = UploadFile( file=io.BytesIO(image_data), filename=f'generated-image{image_format}', # will be converted to a unique ID on upload_file