boog
boog

Reputation: 644

Python - Decoding an image from json

Working with an api and the requests library.

Background info: creating a program to interop with an sms messaging platform. All responses from the api are provided in json. When working with the endpoint to obtain sms attachments, the response is in json and is basically a large block of encoded text.

I'm able to send the proper request to obtain the response I want, but I don't know what to do with the response.

How can I decode the json and work with the image file?

The complete response is too long to post here, but I imagine anything important is included in the beginning... I'm just unsure what to do with it.

b'{"id":1067442,"friendlyName":"attachment0","md5":"4dCsb2PEljAqYY1JBl3FXA==","contentType":"image/jpeg","size":1017241,"createdDate":1582649116340,"updatedDate":1582649116340,"thumbnailBase64":"/9j/4AAQSkZJRgABAgAAAQABAAD/2wBDAAgGBgcGBQgHBwcJCQgKDBQNDAsLDBkSEw8UHRofHh0aHBwgJC4nICIsIxwcKDcpLDAxNDQ0Hyc5PTgyPC4zNDL/2wBDAQkJCQwLDBgNDRgyIRwhMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjL/wAARCACWAJYDASIAAhEBAxEB/8QAHwAAAQUBAQEBAQEAAAAAAAAAAAECAwQFBgcICQoL/8QAtRAAAgEDAwIEAwUFBAQAAAF9AQIDAAQRBRIhMUEGE1FhByJxFDKBkaEII0KxwRVS0fAkM2JyggkKFhcYGRolJicoKSo0NTY3ODk6Q0RFRkdISUpTVFVWV1hZWmNkZWZnaGlqc3R1dnd4eXqDhIWGh4iJipKTlJWWl5iZmqKjpKWmp6ipqrKztLW2t7i5usLDxMXGx8jJytLT1NXW19jZ2uHi4+Tl5ufo6erx8vP09fb3+Pn6/8QAHwEAAwEBAQEBAQEBAQAAAAAAAAECAwQFBgcICQoL/8QAtREAAgECBAQDBAcFBAQAAQJ3AAECAxEEBSExBhJBUQdhcRMiMoEIFEKRobHBCSMzUvAVYnLRChYkNOEl8RcYGRomJygpKjU2Nzg5OkNERUZHSElKU1RVVldYWVpjZGVmZ2hpanN0dXZ3eHl6goOEhYaHiImKkpOUlZaXmJmaoqOkpaanqKmqsrO0tba3uLm6wsPExcbHyMnK0tPU1dbX2Nna4uPk5ebn6Onq8vP09fb3+Pn6/9oADAMBAAIRAxEAPwDu1AwB29B/9al4BPr7f5/Gk7Dn86Xt/niuY1HZ5HApcZ6ikXpxxTvccUhABwD1/rUkZ2nHUUwc07GRkUrgTBfTp7U9QB0pEyyg96cxSNGkdwqqCxZjwB6+3elcLmfq19aaRCt/dSMqAGMqD97jI49eP1rz2bUdc8YGR7eaLS9EVtpup22ofYZxvP6VieMfFP8AbGrMu9RaRnbBEcksvqR2z/8AWrkpdRS7vIobi7mlSFdsUKnIX2FdEY6ak8x6zp/hDwhHEJbrUotQY9ZJbsBc+wUgfzrQi0XwUp+SHSH+ro38zXlkdzEBuYiOIDqzYAqvcahaO4W3thcTngNInC/nT5Q5j2iLQfD8ozb6fprD1jhQ4/IVTvfBGkz5aGFYj6xqB+nSvJrU3EUgeGcxSA53wHaQfwrttL+J/wBhtI7fWrZ5pFYoZ4mA3Y9Qcc/jScRqRHqHgu7sQ09oInRPmYL8mQPUdDVeLXWh037HJtLbt0cj9AOhUiux/wCEx0XUtFu5rS4/eLEcROMNk8DHY8ntXO+B9PXUb2+nlQNBHGIfmGQWY5P8h+dS1pqProUrfVBuCGOEjHy+W+P0NU9KeeDVL2a6sLiO2kHlrKUyD................

Upvotes: 0

Views: 2148

Answers (2)

Omer Tuchfeld
Omer Tuchfeld

Reputation: 3022

First parse the json string:

import json
response = b'{"id":1067442,"friendlyName":"attachment0","md5":"4dCsb2PEljAqYY1JBl3FXA==","contentType":"image/jpeg","size":1017241,"createdDate":1582649116340,"updatedDate":1582649116340,"thumbnailBase64":"/9j/4AAQSkZJRgABAgAAAQABAAD/2wBDAAgGBgcGBQgHBwcJCQgKDBQNDAsLDBkSEw8UHRofHh0aHBwgJC4nICIsIxwcKDcpLDAxNDQ0Hyc5PTgyPC4zNDL/2wBDAQkJCQwLDBgNDRgyIRwhMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMj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Z5pFYoZ4mA3Y9Qcc/jScRqRHqHgu7sQ09oInRPmYL8mQPUdDVeLXWh037HJtLbt0cj9AOhUiux/wCEx0XUtFu5rS4/eLEcROMNk8DHY8ntXO+B9PXUb2+nlQNBHGIfmGQWY5P8h+dS1pqProUrfVBuCGOEjHy+W+P0NU9KeeDVL2a6sLiO2kHlrKUyD................"}'
response_parsed = json.loads(response)

response_parsed['contentType'] being image/jpeg hints to us that the format of the image is jpeg.

You didn't provide us with the actual image data, just the beginning of the thumbnail of the image which is stored in thumbnailBase64.

The thumbnail is encoded in base64. We can decode the base64 string into bytes using base64.b64decode:

from base64 import b64decode
thumbnail_bytes = b64decode(response_parsed['thumbnailBase64'])

Now that we have the thumbnail bytes we can save it to a file and view it like any regular image file:

with open(r'thumbnail.jpg', 'wb') as x:
    x.write(thumbnail_bytes)

The actual image is probably in a JSON field called "image" or "imageData" or something similar. You did not include it in your question so there's no way to be sure.

This results in:

Partial thumbnail

Obviously it's corrupt because you only included the beginning of the thumbnail.

Upvotes: 3

Antwane
Antwane

Reputation: 22658

Something like that could do the trick

import json, base64

resp = b'{"id":1067442,"friendlyName":"attachment0","md5":"4dCsb2PEljAqYY1JBl3FXA==","contentType":"image/jpeg","size":1017241,"createdDate":1582649116340,"updatedDate":1582649116340,"thumbnailBase64":"/9j/4AAQSkZJRgABAgAAAQABAAD/2wBDAAgG...'
resp_dict = json.loads(resp)

with open("img.jpeg", "wb") as fp:
    content = base64.b64decode(resp_dict['thumbnailBase64'])
    fp.write(content)

In addition, keep in mind that base64 value can be used directly to print an image into a HTML page

<div>
  <p>Taken from wikpedia</p>
  <img src="data:image/jpeg;base64, iVBORw0KGgoAAAANSUhEUgAAAAUA
    AAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO
        9TXL0Y4OHwAAAABJRU5ErkJggg==" alt="Red dot" />
</div>

In all case, you will need to check the content-type to correctly interpret the data you received. This is not always jpeg, but may be png, gif, tiff, etc.

Upvotes: 1

Related Questions