使用tkinter scale小部件显示3D图像(缩放小部件太慢)。该程序挂起

时间:2018-06-03 07:28:55

标签: python image tkinter display

您好我正在尝试将numpy数组中的3d图像显示为tkinter gui中的图像序列。

当我滑过tkinter中的缩放小部件时,我想更改图像。以下程序正在运行。但是比例小部件卡住了,程序会在一段时间后挂起。

import SimpleITK as sitk 
import numpy as np 

import tkinter as Tkinter
from tkinter import Button,Scrollbar 
from PIL import Image, ImageTk
import numpy

class mainWindow():
    def __init__(self,img):

        self.img = img 

        self.pil_images = [] 

        for i in range(self.img.shape[0]):

            img_slice = self.img[i,:,:]

            im=Image.frombytes('L', (img_slice.shape[1],img_slice.shape[0]), img_slice.astype('b').tostring())
            self.pil_images.append(im)

        slices, im_height, im_width = img.shape

        margin_width = 30
        margin_height = 30

        self.root = Tkinter.Tk()
        self.frame = Tkinter.Frame(self.root, width=im_width + margin_width*2, height=im_height + margin_height*2)
        self.frame.pack()

        self.sliderv1 = Tkinter.Scale(self.frame, orient= Tkinter.VERTICAL, from_= np.min(self.img), to= np.max(self.img), length = im_height)
        self.sliderv1.set(np.min(self.img))
        self.sliderv2 = Tkinter.Scale(self.frame, orient= Tkinter.VERTICAL, from_= np.min(self.img), to= np.max(self.img), length = im_height)
        self.sliderv2.set(np.max(self.img))

        self.canvas = Tkinter.Canvas(self.frame, width=im_width,height=im_height)

        self.sliderh = Tkinter.Scale(self.frame, orient= Tkinter.HORIZONTAL, 
            from_= np.min(self.img), to= np.max(self.img), length = im_width, 
            command=self.update_slice,repeatdelay = 1, resolution =  1)

        self.sliderv1.pack(side = Tkinter.LEFT)
        self.sliderv2.pack(side = Tkinter.LEFT)
        self.canvas.pack()
        self.sliderh.pack(side = Tkinter.BOTTOM)

        self.update_slice(0)

    def draw_image(self,slice):

        slice = int(slice)
        photo = ImageTk.PhotoImage(image=self.pil_images[slice])

        self.canvas.delete("IMG")
        self.canvas.create_image(0,0,image=photo,anchor=Tkinter.NW)
        self.root.update()
        self.root.mainloop()

    def update_slice(self,val):
        slice = val
        self.draw_image(slice)

if __name__ == "__main__":

    image = sitk.ReadImage("filepath")
    im_arr = sitk.GetArrayFromImage(image)
    mainWindow(im_arr)

0 个答案:

没有答案