我正在尝试生成一个程序来显示 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.
这是您使用 python 3 为 wxpython 4.0.4 修改的代码
你会注意到:
测试图像生成方式的变化
选择图像文件时语句的更改if
def
的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
您的“随机”测试图像正在显示
您收到位置参数错误,因为方法 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)
现在全部解决了。
需要进行此修改以避免浮动错误 def onView(self):
if W > H:
NewW = int(self.PhotoMaxSize)
NewH = int(self.PhotoMaxSize * H / W)
else:
NewH = int(self.PhotoMaxSize * H / W)
NewW = int(self.PhotoMaxSize)
img = img.Scale(width=NewW,height=NewH)