【问题标题】:Save image as stream and then display将图像另存为流,然后显示
【发布时间】:2017-10-18 08:48:40
【问题描述】:

我最终想将我的图像保存在数据库中,但现在一个文件就可以了。

我已经开始了

    with open("blue.png", "rb") as imageFile:
        f = imageFile.read()
        b = bytearray(f)
    with open("blue.bytes", "w") as streamFile:
        f = streamFile.write(str(b))

这给了我一个看起来像这样的文件:

bytearray(b'\x89PNG\r\n\x1a\n\x00\x00\x00\rIHDR\x0 ...

目前为止是这样吗?

然后我怎样才能将其读回并转换为图像? 我承认我对我想要字节还是字节数组以及如何将这个字符串恢复到那种形式感到绝望的困惑

我在这里包含了我的尝试,但我知道它不起作用

import wx
from io import BytesIO


class MainFrame(wx.Frame):
    def __init__(self):
        wx.Frame.__init__(self, None, -1, 'Demonstrate wxPython image')
        self.panel = MainPanel(self)
        self.Centre()
        self.save_stream()

    def save_stream(self):
        bitmap = wx.Bitmap('blue.png', wx.BITMAP_TYPE_ANY)
        with open("blue.png", "rb") as imageFile:
            f = imageFile.read()
            b = str(bytearray(f))
        with open("blue.bytes", "w") as streamFile:
            f = streamFile.write(b)


class MainPanel(wx.Panel):
    def __init__(self, frame):
        """Initialise the class."""
        wx.Panel.__init__(self, frame)
        file_image_sizer = self._image_from_file()
        stream_image_sizer = self._image_from_stream()
        main_sizer = wx.BoxSizer(wx.HORIZONTAL)
        main_sizer.Add(file_image_sizer, flag=wx.ALL, border=10)
        main_sizer.Add(stream_image_sizer, flag=wx.ALL, border=10)
        self.SetSizerAndFit(main_sizer)

    def _image_from_file(self):
        image_sizer = wx.BoxSizer(wx.VERTICAL)
        bitmap = wx.Bitmap('red.png', wx.BITMAP_TYPE_ANY)
        static_bitmap = wx.StaticBitmap(self, wx.ID_ANY, wx.NullBitmap)
        static_bitmap.SetBitmap(bitmap)
        image_sizer.Add(static_bitmap)
        return image_sizer

    def _image_from_stream(self):
        image_sizer = wx.BoxSizer(wx.VERTICAL)
        static_bitmap = wx.StaticBitmap(self, wx.ID_ANY, wx.NullBitmap)
        with open("blue.bytes", "rb") as imageFile:
            f = imageFile.read()
        bitmap = wx.Bitmap(BytesIO(bytes(f)), wx.BITMAP_TYPE_ANY)
        static_bitmap = wx.StaticBitmap(self, wx.ID_ANY, wx.NullBitmap)
        static_bitmap.SetBitmap(bitmap)
        image_sizer.Add(static_bitmap)
        return image_sizer


if __name__ == '__main__':
    screen_app = wx.App()
    main_frame = MainFrame()
    main_frame.Show(True)
    screen_app.MainLoop()

根据 Robin 的回答,我已将 _image_from_stream 函数更改为:

def _image_from_stream(self):
    image_sizer = wx.BoxSizer(wx.VERTICAL)
    static_bitmap = wx.StaticBitmap(self, wx.ID_ANY, wx.NullBitmap)
    with open("blue.bytes", "r") as imageFile:
        image = wx.Image(imageFile)
        bitmap = wx.Bitmap(image)
    static_bitmap = wx.StaticBitmap(self, wx.ID_ANY, wx.NullBitmap)
    static_bitmap.SetBitmap(bitmap)
    image_sizer.Add(static_bitmap)
    return image_sizer

【问题讨论】:

    标签: python-3.x wxpython


    【解决方案1】:

    由于它是数据而不是文本,因此您需要将其保存为字节或字节数组,而不是 str。

    在 wxPython4 中,您可以从文件或类似文件的对象加载图像,然后转换为位图(如果需要),如下所示:

    image = wx.Image(file_object)
    bmp = wx.Bitmap(image)
    

    【讨论】:

    • 谢谢。我已经做出了您建议的更改(请参阅更新的问题)。我收到消息“流警告:未知数据格式”。我的 wx 版本是 4.0.0b2 gtk2 (phoenix)
    • blue.bytes 仍然是有效的 PNG 文件吗? (您创建该文件是否有其他原因,而不仅仅是为了说明这个问题。)您可能需要使用“rb”模式打开它,以确保它不会被解释为文本。
    • 谢谢。我对文本和二进制数据感到困惑。就这样搞定了
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-01
    • 1970-01-01
    • 2015-08-06
    • 2017-09-24
    相关资源
    最近更新 更多