Python 如何从cv2.imread获取PIL.Image对象&引用;“无法识别的图像模式”;错误

Python 如何从cv2.imread获取PIL.Image对象&引用;“无法识别的图像模式”;错误,python,image,opencv,pillow,Python,Image,Opencv,Pillow,我正在尝试将从img=cv2.imread(path)获取的图像转换为字节。所以我用了这个: def get_bytes(img): result = PIL.Image.fromarray(img, 'BGR') bio = io.BytesIO() bio.name = 'result.jpeg' result.save(bio, 'JPEG') bio.seek(0) return bio 但是它在第2行的函数fromarray中给了我一个

我正在尝试将从
img=cv2.imread(path)
获取的图像转换为字节。所以我用了这个:

def get_bytes(img):
    result = PIL.Image.fromarray(img, 'BGR')
    bio = io.BytesIO()
    bio.name = 'result.jpeg'
    result.save(bio, 'JPEG')
    bio.seek(0)
    return bio
但是它在第2行的函数
fromarray
中给了我一个“无法识别的图像模式”错误

图像不是无或什么的。
img.dtype=uint8

“BGR”是无法识别的模式。使用OpenCV
img=cv2.cvtColor(img,cv2.COLOR\u BGR2RGB)
将“BGR”转换为“RGB”,然后调用
result=PIL.Image.fromarray(img,'RGB')
@zindarod请不要在评论中回答。@zindarod谢谢!