gpt4 book ai didi

python - PIL 与 BytesIO : cannot identify image file

转载 作者:行者123 更新时间:2023-12-02 16:16:15 25 4
gpt4 key购买 nike

我正在尝试通过套接字连接发送图像以进行视频聊天,但从字节格式重建图像不正确。这是我将图像转换为要发送的字节:

pil_im = Image.fromarray(img)
b = io.BytesIO()
pil_im.save(b, 'jpeg')
im_bytes = b.getvalue()
return im_bytes

这发送得很好,但是,我无法解决将这些字节重新格式化为图像文件的问题。这是我的代码,用于重新格式化为图像以​​进行显示:

pil_bytes = io.BytesIO(im_bytes)
pil_image = Image.open(pil_bytes)
cv_image = cv2.cvtColor(np.array(pil_image), cv2.COLOR_RGB2BGR)
return cv_image

第二行引发以下异常:

cannot identify image file <_io.BytesIO object at 0x0388EF60>

我查看了其他一些线程( this onethis one ),但没有解决方案对我有帮助。我也在使用this作为尝试纠正自己的引用,但似乎对他们有效的方法对我来说并不适用。感谢您提供的任何帮助,如有错误请原谅,我仍在学习 python。

最佳答案

首先谢谢您!因为您问题中的代码帮助我解决了问题的第一部分。第二部分已经使用这个简单的代码为我解决了(不要转换为数组)

dataBytesIO = io.BytesIO(im_bytes)
image = Image.open(dataBytesIO)

希望这有帮助

关于python - PIL 与 BytesIO : cannot identify image file,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43904561/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com