我正在尝试制作一个程序来显示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.
答案 0 :(得分:1)
这是使用python 3为wxpython 4.0.4修改的代码
您会注意到:
测试图像生成方式的变化
选择图像文件时对if
语句的更改
def
的{{1}}
希望这些更改将为您提供所需的内容。
如果您仍在使用wxPython 2.8,则可能需要保留一些现有的图像操作,因为wxPython 4+已更改了其中一些。
onViewTest
您显示的“随机”测试图像
答案 1 :(得分:0)
您收到位置参数错误,因为方法 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)
现在所有问题都解决了。