我有一个RGB图像在一个数字数组的三维。
我目前正在使用这个
base64.b64encode(img).decode('utf-8')
但是当我将输出复制/粘贴到这个网站时,https://codebeautify.org/base64-to-image-converter
它不会将图像转换回来。
但是如果我使用这个代码:
import base64
with open("my_image.jpg", "rb") as img_file:
my_string = base64.b64encode(img_file.read())
my_string = my_string.decode('utf-8')
那就成功了。但我的形象没有保存在记忆中。我不想保存它,因为它会降低程序的速度。
发布于 2019-12-03 04:52:56
您可以将RGB直接编码到内存中的jpg,并为此创建base64编码。
jpg_img = cv2.imencode('.jpg', img)
b64_string = base64.b64encode(jpg_img[1]).decode('utf-8')
完整的例子:
import cv2
import base64
img = cv2.imread('test_image.jpg')
jpg_img = cv2.imencode('.jpg', img)
b64_string = base64.b64encode(jpg_img[1]).decode('utf-8')
基本64字符串应该可以用https://codebeautify.org/base64-to-image-converter解码。
发布于 2019-12-03 03:46:00
尝试此方法:- RGB图像base64编码/解码
import cStringIO
import PIL.Image
def encode_img(img_fn):
with open(img_fn, "rb") as f:
data = f.read()
return data.encode("base64")
def decode_img(img_base64):
decode_str = img_base64.decode("base64")
file_like = cStringIO.StringIO(decode_str)
img = PIL.Image.open(file_like)
# rgb_img[c, r] is the pixel values.
rgb_img = img.convert("RGB")
return rgb_img
https://stackoverflow.com/questions/59156265
复制相似问题