Spaces:
Running on Zero
Running on Zero
File size: 574 Bytes
fda0e73 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | import base64
import os
import uuid
def save_base64_image(data_uri):
if not isinstance(data_uri, str):
return data_uri
if not data_uri.startswith("data:image"):
return data_uri
try:
header, encoded = data_uri.split(",", 1)
ext = header.split(";")[0].split("/")[1]
filepath = f"/tmp/{uuid.uuid4().hex}.{ext}"
with open(filepath, "wb") as f:
f.write(base64.b64decode(encoded))
return filepath
except Exception as e:
print(f"Error decoding base64 image: {e}")
return data_uri
|