将图像另存为流然后显示

时间:2017-10-18 08:48:41

标签: python-3.x wxpython

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

我已经开始了

    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

1 个答案:

答案 0 :(得分:2)

因为它是数据而不是文本,所以你需要将它保存为字节或bytearray,而不是str。

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

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