将从数组生成的图像加载到 wxpython 框架中

Loading an Image generated from an array into a wxpython frame

我正在尝试制作一个程序来显示 DICOM 图像并使用事件按钮在切片中移动。我想先在移动 np.array 的 z 切片时测试它。该代码基于在线示例文件查看器。

如何获得调用随机生成图像的测试按钮?

我已经简化了代码,只显示一个 200x200 像素阵列,不穿过切片,但它仍然无法显示生成的图像。

import os
import wx
import numpy as np
from PIL import Image

data = np.random.randint(low = 0, high = 255, size =(200, 200)) #generation of random array
test_img = Image.fromarray(data.astype('uint8')) #turn array into image

class PhotoCtrl(wx.App):
    def __init__(self, redirect=False, filename=None):
        wx.App.__init__(self, redirect, filename)
        self.frame = wx.Frame(None, title='Slice Viewer')
        self.panel = wx.Panel(self.frame) 
        self.PhotoMaxSize = 200 
        self.createWidgets()
        self.frame.Show()

    def createWidgets(self):
        instructions = 'Browse for an image'
        img = wx.EmptyImage(200,200)
        self.imageCtrl = wx.StaticBitmap(self.panel, wx.ID_ANY, 
                                         wx.BitmapFromImage(img))

        instructLbl = wx.StaticText(self.panel, label=instructions)
        self.photoTxt = wx.TextCtrl(self.panel, size=(100,-1))
        browseBtn = wx.Button(self.panel, label='Browse')
        browseBtn.Bind(wx.EVT_BUTTON, self.onBrowse)

        up_btn = wx.Button(self.panel, label='Up')
        up_btn.Bind(wx.EVT_BUTTON, self.on_press_up) 

        down_btn = wx.Button(self.panel, label='Down')
        down_btn.Bind(wx.EVT_BUTTON, self.on_press_down)

        test_btn = wx.Button(self.panel, label='Test')
        test_btn.Bind(wx.EVT_BUTTON, self.onViewTest)

        self.mainSizer = wx.BoxSizer(wx.VERTICAL)
        self.sizer = wx.BoxSizer(wx.HORIZONTAL)

        self.mainSizer.Add(wx.StaticLine(self.panel, wx.ID_ANY), 0, wx.ALL|wx.EXPAND, 5)
        self.mainSizer.Add(instructLbl, 0, wx.ALL, 5)
        self.mainSizer.Add(self.imageCtrl, 0, wx.ALL, 5)
        self.sizer.Add(self.photoTxt, 0, wx.ALL, 5)
        self.sizer.Add(browseBtn, 0, wx.ALL, 5)   
        self.mainSizer.Add(up_btn, 0, wx.ALL, 5)
        self.mainSizer.Add(down_btn, 0, wx.ALL, 5)
        self.mainSizer.Add(test_btn, 0, wx.ALL, 5)
        self.mainSizer.Add(self.sizer, 0, wx.ALL, 5)

        self.panel.SetSizer(self.mainSizer)
        self.mainSizer.Fit(self.frame)

        self.panel.Layout()

    def onBrowse(self, event):
        """ 
        Browse for file mode for later testing
        """
        wildcard = "JPEG files (*.jpg)|*.jpg"
        dialog = wx.FileDialog(None, "Choose a file",
                               wildcard=wildcard,
                               style=wx.FD_OPEN)
        if dialog.ShowModal() == wx.ID_OK:
            self.photoTxt.SetValue(dialog.GetPath())
        dialog.Destroy() 
        self.onView()

    def onView(self):
        """
        Part of later data selection
        """
        filepath = self.photoTxt.GetValue()
        img = wx.Image(filepath, wx.BITMAP_TYPE_ANY)
        # scale the image, preserving the aspect ratio
        W = img.GetWidth()
        H = img.GetHeight()
        if W > H:
            NewW = self.PhotoMaxSize
            NewH = self.PhotoMaxSize * H / W
        else:
            NewH = self.PhotoMaxSize
            NewW = self.PhotoMaxSize * W / H
        img = img.Scale(NewW,NewH)

        self.imageCtrl.SetBitmap(wx.BitmapFromImage(img))
        self.panel.Refresh()

    def onViewTest(self):
        """
        Problem code area, trying to call the generated image and display
        """
        img = wx.Image(test_img, wx.BITMAP_TYPE_ANY)
        self.imageCtrl.SetBitmap(wx.BitmapFromImage(img))
        self.panel.Refresh()

    def on_press_up(self, event):
        print('up')

    def on_press_down(self, event):
        print('down')


if __name__ == '__main__':
    app = PhotoCtrl()
    app.MainLoop()

del app
'''

Currently i get a positional argument error, but don't understand why as the browse function works with only one argument.

您收到位置参数错误,因为方法 def onViewTest(self): 需要第二个参数,例如 def onViewTest(self, event) :

第二个错误是变量test_img 需要图像文件路径的字符串。您可以执行以下操作:

def onView(self):
    """
    Part of later data selection
    """
    filepath = self.photoTxt.GetValue()
    self.filepath = filepath

然后

def onViewTest(self, event):
    """
    Fixed code area, to call the generated image and display
    """
    img = wx.Image(self.filepath, wx.BITMAP_TYPE_ANY)

现在都解决了。

这是为 wxpython 4.0.4 修改的代码 python 3
你会注意到:

A change in how the test image is generated
A change to the if statement when selecting an image file
The def of onViewTest

希望这些改变能满足您的需求。
如果您仍在使用 wxPython 2.8,您可能需要保留一些现有的图像操作,因为 wxPython 4+ 已经改变了其中的一些。

import os
import wx
import numpy as np
from PIL import Image

data = np.random.randint(low = 0, high = 255, size =(200, 200)) #generation of random array
#test_img = Image.fromarray(data.astype('uint8')) #turn array into image

class PhotoCtrl(wx.App):
    def __init__(self, redirect=False, filename=None):
        wx.App.__init__(self, redirect, filename)
        self.frame = wx.Frame(None, title='Slice Viewer')
        self.panel = wx.Panel(self.frame)
        self.PhotoMaxSize = 200
        self.createWidgets()
        self.frame.Show()

    def createWidgets(self):
        instructions = 'Browse for an image'
        img = wx.Image(200,200)
        self.imageCtrl = wx.StaticBitmap(self.panel, wx.ID_ANY,
                                         wx.Bitmap(img))

        instructLbl = wx.StaticText(self.panel, label=instructions)
        self.photoTxt = wx.TextCtrl(self.panel, size=(100,-1))
        browseBtn = wx.Button(self.panel, label='Browse')
        browseBtn.Bind(wx.EVT_BUTTON, self.onBrowse)

        up_btn = wx.Button(self.panel, label='Up')
        up_btn.Bind(wx.EVT_BUTTON, self.on_press_up)

        down_btn = wx.Button(self.panel, label='Down')
        down_btn.Bind(wx.EVT_BUTTON, self.on_press_down)

        test_btn = wx.Button(self.panel, label='Test')
        test_btn.Bind(wx.EVT_BUTTON, self.onViewTest)

        self.mainSizer = wx.BoxSizer(wx.VERTICAL)
        self.sizer = wx.BoxSizer(wx.HORIZONTAL)

        self.mainSizer.Add(wx.StaticLine(self.panel, wx.ID_ANY), 0, wx.ALL|wx.EXPAND, 5)
        self.mainSizer.Add(instructLbl, 0, wx.ALL, 5)
        self.mainSizer.Add(self.imageCtrl, 0, wx.ALL, 5)
        self.sizer.Add(self.photoTxt, 0, wx.ALL, 5)
        self.sizer.Add(browseBtn, 0, wx.ALL, 5)
        self.mainSizer.Add(up_btn, 0, wx.ALL, 5)
        self.mainSizer.Add(down_btn, 0, wx.ALL, 5)
        self.mainSizer.Add(test_btn, 0, wx.ALL, 5)
        self.mainSizer.Add(self.sizer, 0, wx.ALL, 5)

        self.panel.SetSizer(self.mainSizer)
        self.mainSizer.Fit(self.frame)

        self.panel.Layout()

    def onBrowse(self, event):
        """
        Browse for file mode for later testing
        """
        wildcard = "JPEG files (*.jpg)|*.jpg"
        dialog = wx.FileDialog(None, "Choose a file",
                               wildcard=wildcard,
                               style=wx.FD_OPEN)
        if dialog.ShowModal() == wx.ID_OK:
            self.photoTxt.SetValue(dialog.GetPath())
            dialog.Destroy()
            self.onView()

    def onView(self):
        """
        Part of later data selection
        """
        filepath = self.photoTxt.GetValue()
        img = wx.Image(filepath, wx.BITMAP_TYPE_ANY)
        # scale the image, preserving the aspect ratio
        W = img.GetWidth()
        H = img.GetHeight()
        if W > H:
            NewW = self.PhotoMaxSize
            NewH = self.PhotoMaxSize * H / W
        else:
            NewH = self.PhotoMaxSize
            NewW = self.PhotoMaxSize * W / H
        img = img.Scale(NewW,NewH)

        self.imageCtrl.SetBitmap(wx.Bitmap(img))
        self.panel.Refresh()

    def onViewTest(self, event):
        """
        Problem code area, trying to call the generated image and display
        """
#        img = wx.Image(test_img, wx.BITMAP_TYPE_ANY)
#        self.imageCtrl.SetBitmap(wx.BitmapFromImage(img))
        img = wx.Image(200,200)
        img.SetData(data)
        self.imageCtrl.SetBitmap(wx.Bitmap(img))
        self.panel.Refresh()

    def on_press_up(self, event):
        print('up')

    def on_press_down(self, event):
        print('down')


if __name__ == '__main__':
    app = PhotoCtrl()
    app.MainLoop()

del app

正在显示您的 "random" 测试图像