Convert base64 String to an Image that's compatible with OpenCV

匿名 (未验证) 提交于 2019-12-03 09:02:45

问题:

I'm trying to convert a base64 representation of a JPEG to an image that can be used with OpenCV. The catch is that I'd like to be able to do this without having to physically save the photo (I'd like it to remain in memory). Is there an updated way of accomplishing this?

I'm using python 3.6.2 and OpenCV 3.3

Here is a partial example of the type of input I'm trying to convert:

/9j/4AAQSkZJRgABAQAASABIAAD/4QBYRXhpZgAATU0AKgAAAAgAAgESAAMAAAA....

I've already tried the solutions provided by these questions, but keep getting the same "bad argument type for built-in operation" error:

回答1:

Here an example for python 3.6 that uses imageio instead of PIL. It first loads an image and converts it to a b64_string. This string can then be sent around and the image reconstructed as follows:

import base64 import io import cv2 from imageio import imread import matplotlib.pyplot as plt  filename = "yourfile.jpg" with open(filename, "rb") as fid:     data = fid.read()  b64_bytes = base64.b64encode(data) b64_string = b64_bytes.decode()  # reconstruct image as an numpy array img = imread(io.BytesIO(base64.b64decode(b64_string)))  # show image plt.figure() plt.imshow(img, cmap="gray")  # finally convert RGB image to BGR for opencv # and save result cv2_img = cv2.cvtColor(img, cv2.COLOR_RGB2BGR) cv2.imwrite("reconstructed.jpg", cv2_img) plt.show() 


回答2:

I've been struggling with this issue for a while now and of course, once I post a question - I figure it out.

For my particular use case, I needed to convert the string into a PIL Image to use in another function before converting it to a numpy array to use in OpenCV. You may be thinking, "why convert to RGB?". I added this in because when converting from PIL Image -> Numpy array, OpenCV defaults to BGR for its images.

Anyways, here's my two helper functions which solved my own question:

import io import cv2 import base64  import numpy as np from PIL import Image  # Take in base64 string and return PIL image def stringToImage(base64_string):     imgdata = base64.b64decode(base64_string)     return Image.open(io.BytesIO(imgdata))  # convert PIL Image to an RGB image( technically a numpy array ) that's compatible with opencv def toRGB(image):     return cv2.cvtColor(np.array(image), cv2.COLOR_BGR2RGB) 


回答3:

You can also try this:

import numpy as np import cv2  def to_image_string(image_filepath):     return open(image_filepath, 'rb').read().encode('base64')  def from_base64(base64_data):     nparr = np.fromstring(base64_data.decode('base64'), np.uint8)     return cv2.imdecode(nparr, cv2.IMREAD_ANYCOLOR) 

You can now use it like this:

filepath = 'myimage.png' encoded_string = to_image_string(filepath) 

load it with open cv like this:

im = from_base64(encoded_string) cv2.imwrite('myloadedfile.png', im) 


易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!