How to convert image to json in python
I need to convert an image (can be any type jpg, png etc.) to JSON serializable. Show
I looked into the solution here but the accepted solution has a typo and I am not sure how to resolve it.
Chandan 5414 silver badges21 bronze badges asked Feb 13, 2019 at 0:38
This might get you started:
answered Feb 13, 2019 at 3:03
Cole TierneyCole Tierney 8,8431 gold badge27 silver badges31 bronze badges Python 2 As the
Then, use answered Jun 25, 2019 at 22:17
ErfanErfan 3023 silver badges8 bronze badges 1 Questions : Python - convert image to JSON2022-09-30T01:57:40+00:00 2022-09-30T01:57:40+00:00 884 I need to convert an image (can be any type anycodings_json jpg, png etc.) to JSON serializable. I looked into the solution here but the anycodings_json accepted solution has a typo and I am not anycodings_json sure how to resolve it. Total Answers 2 31 Answers 1 : of Python - convert image to JSONThis might get you started:
0 2022-09-30T01:57:40+00:00 2022-09-30T01:57:40+00:00Answer Link mRahman 2 Answers 2 : of Python - convert image to JSONPython 2 As the base64.encodebytes() has been anycodings_json deprecated in base64, the code snippet anycodings_json above can be modified as follows:
Then, use base64.b64decode(data['img']) anycodings_json to convert back. 0 2022-09-30T01:57:40+00:00 2022-09-30T01:57:40+00:00Answer Link jidam Thing you want to doI want to POST the image as json and receive it in flask. However, bytes type data cannot be used as the json value, so some ingenuity is required. I set up a server locally and tried it. What is base64?As mentioned above, binary data cannot be an element of json. Text data is OK. In that case, the binary data may be converted into text data once according to a certain rule, transmitted, and then converted into the original binary data at the receiving destination. One of the rules for converting binary data to text data is base64. TheseExplained fairly carefully. First on the client side. The transition of data is as follows. Import an image as a Pillow Image ⇒ Convert to bytes ⇒ Encode with base64 (still bytes) ⇒ Convert data that was bytes to str ⇒ json .dumps to json ⇒ You can safely POST with json
|