【发布时间】:2018-03-15 15:35:44
【问题描述】:
我有一个从 OpenCV 视频捕获对象中获取的图像:
import cv2
import base64
from PIL import Image
import io
cap = cv2.VideoCapture(0)
# capture frame by frame
ret, frame = cap.read()
如何对图像进行编码和解码(即从原始像素到字节再返回到原始像素)?
到目前为止,我一直在尝试以下方法:
encoded_string = base64.b64encode(frame)
decoded_string = base64.b64decode(encoded_string)
img = Image.open(io.BytesIO(decoded_string))
img.show()
这给了我一个错误:
File "/usr/lib/python3/dist-packages/PIL/Image.py", line 2295, in open
% (filename if filename else fp))
OSError: cannot identify image file <_io.BytesIO object at 0x7efddbb78e08>
【问题讨论】:
-
你看到这个解决方案了吗? stackoverflow.com/questions/33311153/…
-
@ghovat 您指给我的解决方案似乎不相关。我试图弄清楚如何对图像进行编码和解码,这与尝试将图像保存到文件不同。
-
@Miki 感谢您指出这一点,非常有帮助!
标签: python-3.x base64 python-imaging-library opencv-python