【问题标题】:Byte stream and utf-8 in python 3python 3中的字节流和utf-8
【发布时间】:2018-04-02 09:34:59
【问题描述】:

我正在从包含 png 文件的数据库中读取 blob。

blob 看起来正确并且是字节数据类型。它开始了:

b'\x89PNG\r\n\x1a\n\x00\x00\x00\rIHDR\x00\x00\x00\x92\x00\x00\x00m\x08\x06\x00\x00\x00J\xbf8B\x00\x00\x00\x06bKGD\x00\x00\x00\x00\x00\x00\xf9C\xbb\x7f\x00\x00\x00\tpHYs\x00\x00\x0b\x13\x00\x00\

但是,当我执行时:

image = wx.Image(blob)

我收到消息:

UnicodeDecodeError: 'utf-8' codec can't decode byte 0x89 in position 0: invalid start byte

似乎将字节视为字符串,但为什么呢?

【问题讨论】:

标签: python image python-3.x wxpython


【解决方案1】:

wx.Image() 不支持从原始字节流创建图像。该类将其解释为文件名(必须是字符串,因此正在解码)。

将您的数据包装在io.BytesIO() 对象中; wx 接受流这样的对象:

import io

image = wx.Image(io.BytesIO(blob))

【讨论】:

    猜你喜欢
    • 2017-05-23
    • 2015-07-26
    • 1970-01-01
    • 2012-10-16
    • 2015-01-10
    • 2015-10-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多