I am attempting to convert a jpg, which is encoded as a base64 string that I receive over the server into an RGB image that I can use with numpy, but also plot with matplot lib. I am unable to find docs on how to take a raw base64 string and plot that using python.
I also have the option to receive as a png.
Here is where I am currently, which does not work. Feel free to snag the image bytes.
image = '/9j/4AAQSkZJRgABAQAAAQABAAD/2wBDAAgGBgcGBQgHBwcJCQgKDBQNDAsLDBkSEw8UHRofHh0aHBwgJC4nICIsIxwcKDcpLDAxNDQ0Hyc5PTgyPC4zNDL/2wBDAQkJCQwLDBgNDRgyIRwhMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjL/wAARCAA2AGADASIAAhEBAxEB/8QAHwAAAQUBAQEBAQEAAAAAAAAAAAECAwQFBgcICQoL/8QAtRAAAgEDAwIEAwUFBAQAAAF9AQIDAAQRBRIhMUEGE1FhByJxFDKBkaEII0KxwRVS0fAkM2JyggkKFhcYGRolJicoKSo0NTY3ODk6Q0RFRkdISUpTVFVWV1hZWmNkZWZnaGlqc3R1dnd4eXqDhIWGh4iJipKTlJWWl5iZmqKjpKWmp6ipqrKztLW2t7i5usLDxMXGx8jJytLT1NXW19jZ2uHi4+Tl5ufo6erx8vP09fb3+Pn6/8QAHwEAAwEBAQEBAQEBAQAAAAAAAAECAwQFBgcICQoL/8QAtREAAgECBAQDBAcFBAQAAQJ3AAECAxEEBSExBhJBUQdhcRMiMoEIFEKRobHBCSMzUvAVYnLRChYkNOEl8RcYGRomJygpKjU2Nzg5OkNERUZHSElKU1RVVldYWVpjZGVmZ2hpanN0dXZ3eHl6goOEhYaHiImKkpOUlZaXmJmaoqOkpaanqKmqsrO0tba3uLm6wsPExcbHyMnK0tPU1dbX2Nna4uPk5ebn6Onq8vP09fb3+Pn6/9oADAMBAAIRAxEAPwDxNYvapBF7VoLaH0qVbM+ldCgYOZmCH2p3k+1aosm9KeLFv7tVyE85keT7Unk+1bP2Fv7tJ9hb+7RyBzmN5PtSGL2ro7LQri/l2Rr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Xk0Lq8UroynKlWxg1q2/jTXrWMomoyMucnzMOfzNddn3/r5Cu/6/4BkXek6lZoHubG4iQnALxkDNZr5U4IIPvXfRfEu8IIu7OKUdtpxj880lx4m8Oak8n2mxVWdfmkeIZPbqOaWvYd/I87SQ1OshoopgSLIakEhoopgRzDd83eq+40UUAG40m7miigC0H+UU1pDRRQBEzmoWkNFFJgf/2Q=='
import base64
import io
from matplotlib import pyplot as plt
from array import array
import numpy as np
i = base64.b64decode(image)
i = np.fromstring(image, np.ubyte)
plt.imshow(i, interpolation='nearest')
plt.show()
Any assistance is greatly appreciated.
Here is code that generates the encoded string that is sent out by the server:
byte[] bytes = image.EncodeToJPG();
return Convert.ToBase64String(bytes);
To decode an image using Python, we simply use the base64. b64decode(s) function. Python mentions the following regarding this function: Decode the Base64 encoded bytes-like object or ASCII string s and return the decoded bytes.
To convert a string into a Base64 character the following steps should be followed: Get the ASCII value of each character in the string. Compute the 8-bit binary equivalent of the ASCII values. Convert the 8-bit characters chunk into chunks of 6 bits by re-grouping the digits.
image = '/9j/4AAQSkZJRgABAQAAAQABAAD/2wBDAAgGBgcGBQgHBwcJCQgKDBQNDAsLDBkSEw8UHRofHh0aHBwgJC4nICIsIxwcKDcpLDAxNDQ0Hyc5PTgyPC4zNDL/2wBDAQkJCQwLDBgNDRgyIRwhMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjL/wAARCAA2AGADASIAAhEBAxEB/8QAHwAAAQUBAQEBAQEAAAAAAAAAAAECAwQFBgcICQoL/8QAtRAAAgEDAwIEAwUFBAQAAAF9AQIDAAQRBRIhMUEGE1FhByJxFDKBkaEII0KxwRVS0fAkM2JyggkKFhcYGRolJicoKSo0NTY3ODk6Q0RFRkdISUpTVFVWV1hZWmNkZWZnaGlqc3R1dnd4eXqDhIWGh4iJipKTlJWWl5iZmqKjpKWmp6ipqrKztLW2t7i5usLDxMXGx8jJytLT1NXW19jZ2uHi4+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o05/eCTPG7r0qHxI8BuLfYIpQIsE+Z0O5uKwIdYvbWQyQXDxORgsjYOPTim3Wt3t5t+03EsxX7u9ycV2e0fYlU1tY3tDhE3noERC20D5s+tctLGdx/dL/31/wDWpr3rn/8AXUo1ydI1TyrbCjGTAhP545pObK5Cuj1OsnvWeklTLJQmOxoLJUqykHIOPpWestSCWncRpiefPEsu4rlMHitZbzUTp2A6mTH38n+WK4ya5kEhUOwA6YOKeuqXax7BMdvuAa8qvgZVHdNFqTOosbq7+1RJJI4ZzjkkgjvxW1aaP4U1Wadb7WHtr92fIP3A2TgkkY9+tefR6teRA7LhhkYz3/D0qGO5bzSzsSW6k1vh8NKnJylb5Cep6ZP8LVmtUm0vXba53HgsNq49iCc81z+ofDzxDZNJttlnjQZ3xODnjsOv6Vz6Xk0Lq8UroynKlWxg1q2/jTXrWMomoyMucnzMOfzNddn3/r5Cu/6/4BkXek6lZoHubG4iQnALxkDNZr5U4IIPvXfRfEu8IIu7OKUdtpxj880lx4m8Oak8n2mxVWdfmkeIZPbqOaWvYd/I87SQ1OshoopgSLIakEhoopgRzDd83eq+40UUAG40m7miigC0H+UU1pDRRQBEzmoWkNFFJgf/2Q=='
import base64
import io
from matplotlib import pyplot as plt
import matplotlib.image as mpimg
i = base64.b64decode(image)
i = io.BytesIO(i)
i = mpimg.imread(i, format='JPG')
plt.imshow(i, interpolation='nearest')
plt.show()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With