如何直接将wxpython图像转换为pyplot图像而不保存

时间:2014-01-28 15:00:30

标签: image matplotlib wxpython

我正在尝试在pyplot中显示wxpython屏幕截图,但我不想保存图像。 这就是我所拥有的

import wx
from matplotlib import pyplot as plt
import matplotlib.image as mpimg
thisApp = wx.App( redirect=False )  
def saveSnapshot(dcSource): #takes arg dcSource
    # based largely on code posted to wxpython-users by Andrea Gavana 2006-11-08
    size = dcSource.Size
    bmp = wx.EmptyBitmap(size.width, size.height)
    memDC = wx.MemoryDC()
    memDC.SelectObject(bmp)
    memDC.Blit( 0, 0, size.width, size.height, dcSource, 0, 0)
    memDC.SelectObject(wx.NullBitmap)
    img = bmp.ConvertToImage()
    img.SaveFile('saved.png', wx.BITMAP_TYPE_PNG)
    img = mpimg.imread('saved.png')
    plt.imshow(img)
    plt.show()
saveSnapshot(wx.ScreenDC())

这就像我想要的,基本上不是保存文件只是显示它。

    img = bmp.ConvertToImage()
    plt.imshow(img)
    plt.show()

1 个答案:

答案 0 :(得分:0)

您可以使用BytesIO对象。可能的解决方案是:

import wx
from matplotlib import pyplot as plt
from io import BytesIO
thisApp = wx.App(redirect=False)

def saveSnapshot(dcSource):
    size = dcSource.Size
    bmp = wx.EmptyBitmap(size.width, size.height)
    memDC = wx.MemoryDC()
    memDC.SelectObject(bmp)
    memDC.Blit( 0, 0, size.width, size.height, dcSource, 0, 0)
    memDC.SelectObject(wx.NullBitmap)
    img = bmp.ConvertToImage()
    bio = BytesIO()
    bs = wx.OutputStream(bio)
    img.SaveStream(bs, wx.BITMAP_TYPE_PNG)
    bio.seek(0) #rewind stream
    plt.imshow(plt.imread(bio))
    plt.show()

saveSnapshot(wx.ScreenDC())

我发现了这种方法的一些想法here

<强>更新 使用pyscreenshot的方法略有不同看起来像:

import matplotlib.pyplot as plt
import pyscreenshot as ImageGrab

def saveSnapshot():
    im = ImageGrab.grab()
    plt.imshow(im)
    plt.show()