简体   繁体   English

在 tkinter 上绘制像素的最快方法

[英]Fastest way to draw a pixel on tkinter

Note: You really only need to read the checklist and understand that I want to do this in Tkinter, the rest of the information is for clarification注意:您真的只需要阅读清单并了解我想在 Tkinter 中执行此操作,其余信息用于澄清

The complete code is here: https://gist.github.com/SnugBug/1cc5ea67d11487d69aae8549107372ef完整代码在这里: https : //gist.github.com/SnugBug/1cc5ea67d11487d69aae8549107372ef

I need to be able to manipulate pixels.我需要能够操纵像素。 The goal is to be able to:目标是能够:

  • Control which pixels are drawn first控制先绘制哪些像素

  • Change the color and position of each pixel改变每个像素的颜色和位置

  • Update everything as a whole, so that if a pixel changes the change shows up整体更新所有内容,以便如果像素发生变化,变化就会显示出来

  • Clear everything as a whole清除一切作为一个整体

The question is, what's the fastest way to check off this list in tkinter?问题是,在 tkinter 中检查此列表的最快方法是什么? I tried creating an image with PIL, then loading it into tkinter, but I cannot update the image or clear it.我尝试使用 PIL 创建图像,然后将其加载到 tkinter,但我无法更新图像或清除它。 The other thing I tried is using tkinter's PhotoImage class, as shown below:我尝试的另一件事是使用 tkinter 的 PhotoImage 类,如下所示:

#The function definitions are in the GIST.
#This snippet should be enough information to understand the problem, however.
for i in range(0,3600):
    rot = [0,i,0]
    Tx,Ty,Tz,Zm = [0,0,200,200]
    x,y,z = [10,10,10]
    for m,n in itertools.product(range(-50,50,2),range(-50,50,2)):
        x,y,z = rotate([m,n,0],rot)
        img.put("#ffffff", (int(WIDTH/2 + ((x+Tx)*Zm/(z+Tz))), int(HEIGHT/2 - ((y+Ty)*Zm/(z+Tz)))))
    canvas.update()
    img.blank()
#the confusing math in the `img.put` call is just 3D math

This way is extremely slow.这种方式极其缓慢。 The next way I tried is even slower.我尝试的下一种方法甚至更慢。 It's drawing a line like this: canvas.create_line(x,y,x+1,y+1, ...) Which creates a line of length 1, showing a single pixel.它正在画一条这样的线: canvas.create_line(x,y,x+1,y+1, ...)它创建一条长度为 1 的线,显示单个像素。 This is excruciatingly slow.这是极其缓慢的。

If the separate image method is the fastest, could you include a working snippet in your answer?如果单独的图像方法是最快的,您能否在答案中包含一个工作片段? I cannot figure out the separate image method.我无法弄清楚单独的图像方法。 I have PIL installed, that's what I was using to attempt it.我已经安装了 PIL,这就是我用来尝试的。 I lost the python file so I cannot include the code I used to attempt the separate image method.我丢失了 python 文件,因此无法包含用于尝试单独图像方法的代码。

What I mean by the separate image method : create an image using PIL, drawing on it using PIL, then making that show up on a tkinter screen.我的意思是单独的图像方法:使用 PIL 创建一个图像,使用 PIL 在其上绘制,然后使其显示在 tkinter 屏幕上。 This doesn't meet everything on the checklist, however (from what I understand).然而,这并不符合清单上的所有内容(据我所知)。

If the separate image method is not the fastest, please tell me a way I can check off everything in the checklist some other way.如果单独的图像方法不是最快的,请告诉我一种方法,我可以通过其他方式检查清单中的所有内容。 I have a few questions I looked at for help and some sites.我有几个问题需要帮助和一些网站。 They're below他们在下面

Why is Photoimage put slow? 为什么 Photoimage 放慢? Any of these answers don't work for me because it only creates squares.这些答案中的任何一个都不适合我,因为它只会创建正方形。 I need to be able to make any shape.我需要能够做出任何形状。

python tkinter: how to work with pixels? python tkinter:如何处理像素? This answer doesn't work because it's too slow.这个答案不起作用,因为它太慢了。

How to load .bmp file into BitmapImage class Tkinter python This could be helpful 如何将 .bmp 文件加载到 BitmapImage 类 Tkinter python这可能会有所帮助

http://zetcode.com/gui/tkinter/drawing/ None of these methods work because I cannot manipulate the order each pixel is drawn, and the color of each individual pixel. http://zetcode.com/gui/tkinter/drawing/这些方法都不起作用,因为我无法操纵每个像素的绘制顺序以及每个像素的颜色。 If you are familiar with 3D terminology, I need this for a Z-Buffer如果你熟悉 3D 术语,我需要这个用于 Z-Buffer

If there are any confusions or you need something clarified, please tell me below in the comment section.如果有任何混淆或您需要澄清的内容,请在下面的评论部分告诉我。 I am open minded, so if you have a deep understanding of my question and have another idea on how to solve it, I would love to hear it.我思想开放,所以如果你对我的问题有深刻的理解并对如何解决它有其他想法,我很乐意听到。

If you are using Windows, then the fastest way to put an image on a frame is by ImageWin.如果您使用的是 Windows,那么将图像放在框架上的最快方法是通过 ImageWin。 The tkinter process of first transforming from PIL image to a tkphotoimage is very slow.首先从 PIL 图像转换为 tkphotoimage 的 tkinter 过程非常缓慢。

from PIL import Image, ImageWin
from win32gui import GetDC
from tkinter import Tk

root = Tk()
im = Image.open(<file path>)
ImageWin.Dib(im).draw(
        GetDC(ImageWin.HWND(root.winfo_id())), 
        (0,0,100,100)
) 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM