目前,我正在编写一个程序,需要允许用户绘制简单的线,也可以绘制矩形来选择一个区域。我以前用过tkinter的画布,但它在一些操作中有性能,而且内存泄漏,所以我试着用枕头来代替
以下是我绘制矩形的代码:
def drawRect(self, start, end):
x1 = end[0]
y1 = end[1]
x0 = start[0]
y0 = start[1]
t0 = time()
t = time()
#size of image is roughly between 1000x1000 to 1080p
rectLayer = Image.new("RGBA", self.backgroundImage.size)
rectDraw = ImageDraw.Draw(rectLayer)
rectDraw.rectangle([start, end], fill="#00000080")
rectDraw.line((x0, y0, x1, y0, x1, y1, x0, y1, x0, y0), fill="#ffffff", width=1)
print("drawing: ", time() - t)
t = time()
displayImage = Image.alpha_composite(self.backgroundImage, self.linesLayer)
displayImage.alpha_composite(rectLayer, (0, 0), (0, 0))
print("image blend: ", time() - t)
t = time()
self.photoImage = ImageTk.PhotoImage(displayImage)
print("photoImage convert: ", time() - t)
t = time()
self.imageContainer.configure(image=self.photoImage) # imageContainer is a Label
print("label config: ", time() - t)
print("total: ", time() - t0)
'''
Output for drawing a single rect:
drawing: 0.001994609832763672
image blend: 0.009583711624145508
photoImage convert: 0.0139617919921875
label config: 0.02194380760192871
total: 0.049475669860839844
'''
我面临的问题是,尽管PIL速度很快,但显示图像却不是。从我的时间分析中,绘制矩形只需很少的时间,但将图像转换为photoimage,然后将其设置为标签图像需要很长时间。我希望这个函数每秒至少可以运行60次左右,这样程序使用起来会更流畅。有没有一种方法可以更快地显示图像
目前没有回答
相关问题 更多 >
编程相关推荐