Python枕头:使用数据加载图像

时间:2019-05-18 09:40:39

标签: python-3.x image python-imaging-library

我需要知道如何使用其数据加载图像
我使用base64模块读取数据

print(base64.b64encode(open('FILENAME','rb').read()))

这给了我图像的数据
我需要类似的东西

img=Load(imgdata)  #instead of Image.open()

1 个答案:

答案 0 :(得分:0)

以下是对JPEG / PNG或图像的任何其他表示形式进行base64编码的方法:

import base64

# Base64 encode a PNG/JPEG image
b64 = base64.b64encode(open('image.png','rb').read())

以下是解码并恢复图像的方法:

import io
from PIL import Image

# Base64 decode and convert from PNG/JPEG to PIL Image
im = Image.open(io.BytesIO(base64.b64decode(b64))) 

或者,如果您使用的是 OpenCV ,则可能要取回 OpenCV 用于图像处理的Numpy数组,在这种情况下,您可以执行以下操作:

import cv2

NumpyIM = cv2.imdecode(np.asarray(bytearray(base64.b64decode(b64))),0) 

请注意,这将使蓝色和绿色通道相对于PIL / Pillow互换,即BGR与RGB。


关键字:OpenCV,PIL,Pillow,Python,图像处理,base64编码,编码,解码,解码,imencode,imdecode,BytesIO,io.BytesIO。