0

I'm working with django, and when I recived an encode image by Base64, I recive like this...

enter image description here

How can I decode and save on an image .jpg? I think the problem is that it start with {"imgB64":" and end with }' I been worked with Base64 before and doesn't have this begining and ending, What do you think?

  • Possible duplicate of [Convert base64 to Image in Python](https://stackoverflow.com/questions/5368669/convert-base64-to-image-in-python) – micharaze Sep 10 '19 at 01:54
  • 1
    it can be JSON data with Python's dictionary which has key `"imgB64"` and value `"image in Base64"`. So you can convert JSON back to Python's dictionary and get value from dictionary. OR simply cut off this parts (`{"imgB64":"` and `"}`) and keep only data which you can convert from Base64 to image. – furas Sep 10 '19 at 01:57
  • raze92 - It isn't the same problem, I can't convert to base64 using decodestring() or b64decode – César Sáenz Sep 10 '19 at 02:01
  • furas - Do you know how to convert JSON back to Python's dictionary? I tried the second one option and it doesn't work for me :( – César Sáenz Sep 10 '19 at 02:04
  • what means doesn't work ? can't you slice it ? ie. `data = data[11:-2]` and later use code from link in first comment. But first `print()` data to see if it is sliced correctly. – furas Sep 10 '19 at 02:13
  • `python_dict = json.loads(data.decode())` or even withouth `.decode()` - `python_dict = json.loads(data)` – furas Sep 10 '19 at 02:15

1 Answers1

0

Is this what you are looking for?

import json

myImage = b'{"imgB64":"something"}'
parsed_json = (json.loads(myImage))

print(parsed_json['imgB64'])
Madison Courto
  • 1,231
  • 13
  • 25