Python 3.x Python枕头:用数据加载图像
我需要知道如何加载图像及其数据Python 3.x Python枕头:用数据加载图像,python-3.x,image,python-imaging-library,Python 3.x,Image,Python Imaging Library,我需要知道如何加载图像及其数据 我使用base64模块读取数据 print(base64.b64encode(打开('FILENAME','rb').read()) 这给了我图像的数据 我需要像这样的东西 img=Load(imgdata)#而不是Image.open() 以下是如何对JPEG/PNG或任何其他图像表示进行base64编码: import base64 # Base64 encode a PNG/JPEG image b64 = base64.b64encode(open('
我使用
base64
模块读取数据
print(base64.b64encode(打开('FILENAME','rb').read())
这给了我图像的数据我需要像这样的东西
img=Load(imgdata)#而不是Image.open()
以下是如何对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,您可能希望返回一个用于图像处理的Numpy数组,在这种情况下,您可以执行以下操作:
import cv2
NumpyIM = cv2.imdecode(np.asarray(bytearray(base64.b64decode(b64))),0)
请注意,这将使蓝色和绿色通道相对于PIL/枕头互换,即BGR与RGB
关键词:OpenCV、PIL、Pillow、Python、图像处理、base64编码、编码、解码、解码、imencode、imdecode、BytesIO、io.BytesIO。我只使用第二个示例(io)