无法识别图像文件<_io.BytesIO object at 0x7f15d3b20400>

问题描述 投票:0回答:1

我的 python 服务器通过套接字从客户端接收 Base64 图像,我想解码该图像并使用 tkinter 显示它。有时图像可以解码并且图像在某些部分显示,有时会显示此错误消息:

cannot identify image file <_io.BytesIO object at 0x7f15d3b20400>
。 这就是我的代码的样子:

def display_image_of_base64(self, base64_image):
        if not self.is_base64(base64_image):
            print("Invalid Base64 string")
            return False
        try:
            image_bytes = base64.b64decode(base64_image.decode("utf8"))
        except binascii.Error:
            return False
        try:
            try:
                image_stream = io.BytesIO(image_bytes)
                image_stream.seek(0)
                pil_image = Image.open(image_stream)
            except OSError as e:
                print(e)
                return False
            tk_image = ImageTk.PhotoImage(pil_image)

        except ValueError:
            return False
        print(image_bytes)
        self.label.config(image=tk_image)
        self.label.image = tk_image

以下是解码工作时图像的示例: Enter image description here 不应出现灰色。屏幕的其余部分应该存在

我想修复此错误:“无法识别图像文件<_io.BytesIO object at 0x7f15d3b20400>”并希望正确显示收到的 Base64 编码图像。

谢谢您的帮助!

python sockets tkinter
1个回答
0
投票

我解决了这个问题。问题是发送的图像被自动分成几个包,尽管我只发送了一次。因此,服务器只收到了图像的一部分,直接显示图像,没有等待其他包,所以我在客户端发送的图像末尾添加了一个文本,应该表明该图像是完全的。然后,服务器接收图像的某些部分并将它们放在一起,直到 Base64 字符串末尾的文本出现在发送的消息中,以表明图像已完全发送。

© www.soinside.com 2019 - 2024. All rights reserved.