python中pixels函数_python tkinter:如何使用像素?

using google (and this site) i have seen some similar questions but my problem is still here:

"i want to draw an image (without reading a file) , being able to manipulate every single pixel's colour in that image."

i have seen another question where was suggested to do something like this:

from tkinter import *

A=Tk()

B=Canvas(A)

B.place(x=0,y=0,height=256,width=256)

for a in range(256):

for b in range(256):

B.create_line(a,b,a+1,b+1,fill=pyList[a][b])#where pyList is a matrix of hexadecimal strings

A.geometry("256x256")

mainloop()

in fact this answers my question but... it is extremely slow.

what should i do with a 1920x1080 image ? wait for my death?

so i am asking something to perform the same as the above code but in a faster way

i have found a way to improve the method suggested by jsbueno , it is explained in the page linked :

解决方案

It is indeed tricky --

I thought you had to use a Canvas widget, but that has no access to Pixels either.

Image items embedded in the Canvas do have, though. The Tkinter.PhotoImage class

does have a "put" method that accepts a color in hex format and pixel coordinates:

from Tkinter import Tk, Canvas, PhotoImage, mainloop

from math import sin

WIDTH, HEIGHT = 640, 480

window = Tk()

canvas = Canvas(window, width=WIDTH, height=HEIGHT, bg="#000000")

canvas.pack()

img = PhotoImage(width=WIDTH, height=HEIGHT)

canvas.create_image((WIDTH/2, HEIGHT/2), image=img, state="normal")

for x in range(4 * WIDTH):

y = int(HEIGHT/2 + HEIGHT/4 * sin(x/80.0))

img.put("#ffffff", (x//4,y))

mainloop()

The good news is that even it being done this way, the updates are "live":

you set pixels on the image, and see them showing up on screen.

This should be much faster than the way drawing higher level lines on screen -

but for lots of pixels it still will be slow, due to a Python function call needed for

every pixel. Any other pure python way of manipulating pixels directly will suffer from that - the only way out is calling primitives that manipulate several pixels at a time in native code from your Python code.

A nice cross-platform library for getting 2d drawing, however poorly documented as well

is Cairo - it would should have much better primitives than Tkinter's Canvas or PhotoImage.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值