Skip to content

Instantly share code, notes, and snippets.

@prettyirrelevant
Created December 7, 2021 13:18
Show Gist options
  • Save prettyirrelevant/2b7de74dc4402d948761d1b01b75a7fa to your computer and use it in GitHub Desktop.
Save prettyirrelevant/2b7de74dc4402d948761d1b01b75a7fa to your computer and use it in GitHub Desktop.
def save_image_from_b64(b64_image):
file_name = secrets.token_hex(6)
file_data = b64_image.split(",")[-1].encode()
file_type = b64_image.split(",")[0].split(";")[0].split("/")[-1] or "png"
with open("{}/{}.{}".format(app.config["UPLOAD_FOLDER"], file_name, file_type), "wb") as f:
f.write(base64.decodebytes(file_data))
return "{}.{}".format(file_name, file_type)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment