Skip to content

Commit

Permalink
The save_extension parameter now also takes effect for the `base64 …
Browse files Browse the repository at this point in the history
…` field in the returned data

The base64 field in the returned data has an identifier added, such as `data: image/png; base64, `
  • Loading branch information
mrhan1993 committed Jul 11, 2024
1 parent 8d98fe0 commit 948f342
Showing 1 changed file with 5 additions and 2 deletions.
7 changes: 5 additions & 2 deletions fooocusapi/utils/file_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,12 +100,15 @@ def output_file_to_base64img(filename: str | None) -> str | None:
if not os.path.exists(file_path) or not os.path.isfile(file_path):
return None

ext = filename.split('.')[-1]
if ext.lower() not in ['png', 'jpg', 'webp', 'jpeg']:
ext = 'png'
img = Image.open(file_path)
output_buffer = BytesIO()
img.save(output_buffer, format='PNG')
img.save(output_buffer, format=ext.upper())
byte_data = output_buffer.getvalue()
base64_str = base64.b64encode(byte_data).decode('utf-8')
return base64_str
return f"data:image/{ext};base64," + base64_str


def output_file_to_bytesimg(filename: str | None) -> bytes | None:
Expand Down

0 comments on commit 948f342

Please sign in to comment.