import base64
from PIL import Image, UnidentifiedImageError
import io
# Define the main function with the specified parameter types
def main(image_base64: str):
try:
# Decode the base64 encoded image
image_data = base64.b64decode(image_base64)
# Convert the binary data to an image
image = Image.open(io.BytesIO(image_data))
# Convert the image to JPEG format
# Note: We use BytesIO to handle the conversion in memory
with io.BytesIO() as output:
image.convert("RGB").save(output, format="JPEG")
jpeg_data = output.getvalue()
# Encode the JPEG image to base64
jpeg_base64 = base64.b64encode(jpeg_data).decode("utf-8")
# Return the base64 encoded JPEG image
return { "render_all": [ { "file": { "content": jpeg_base64, "filename": "image.jpg" } }, { "jpeg": jpeg_base64 } ]}
except UnidentifiedImageError:
# Handle the case where the image cannot be identified
return "Error: The provided data does not represent a valid image."
Submitted by henri186 212 days ago
import base64
from PIL import Image, UnidentifiedImageError
import io
# Define the main function with the specified parameter types
def main(image_base64: str):
try:
# Decode the base64 encoded image
image_data = base64.b64decode(image_base64)
# Convert the binary data to an image
image = Image.open(io.BytesIO(image_data))
# Convert the image to JPEG format
# Note: We use BytesIO to handle the conversion in memory
with io.BytesIO() as output:
image.convert("RGB").save(output, format="JPEG")
jpeg_data = output.getvalue()
# Encode the JPEG image to base64
jpeg_base64 = base64.b64encode(jpeg_data).decode("utf-8")
# Return the base64 encoded JPEG image
return { "render_all": [ { "file": { "content": jpeg_base64, "filename": "image.jpg" } }, { "jpeg": jpeg_base64 } ]}
except UnidentifiedImageError:
# Handle the case where the image cannot be identified
return "Error: The provided data does not represent a valid image."
Submitted by henri186 212 days ago