在处理照片图像对象时,请使用:
import tkinter as tk
img = tk.PhotoImage(file="myFile.gif")
for x in range(0,1000):
for y in range(0,1000):
img.put("{red}", (x, y))put操作需要很长时间。有没有更快的方法来做这件事?
发布于 2012-05-03 09:00:58
使用边界框:
from Tkinter import *
root = Tk()
label = Label(root)
label.pack()
img = PhotoImage(width=300,height=300)
data = ("{red red red red blue blue blue blue}")
img.put(data, to=(20,20,280,280))
label.config(image=img)
root.mainloop()发布于 2018-08-24 06:19:01
只需使用put()命令的to可选参数就足够了,不需要创建复杂的字符串:
import tkinter as tk
root = tk.Tk()
img = tk.PhotoImage(width=1000, height=1000)
data = 'red'
img.put(data, to=(0, 0, 1000, 1000))
label = tk.Label(root, image=img).pack()
root_window.mainloop()进一步的观察
我找不到太多关于PhotoImage的文档,但是to参数比标准循环更有效地缩放数据。这里有一些我可能会发现有帮助的信息,这些信息似乎没有在网上得到很好的记录。
data参数接受名为(official list)或8位颜色十六进制代码的空格分隔颜色值字符串。该字符串表示要按像素重复的颜色数组,其中具有一种以上颜色的行用大括号括起来,列仅用空格分隔。行必须具有相同数量的列/颜色。
acceptable:
3 column 2 row: '{color color color} {color color color}'
1 column 2 row: 'color color', 'color {color}'
1 column 1 row: 'color', '{color}'
unacceptable:
{color color} {color}如果使用包含空格的命名颜色,则必须用大括号将其括起来。即。‘{躲避蓝}’
这里有几个例子来说明上面的操作,其中需要一个很长的字符串:
img = tk.PhotoImage(width=80, height=80)
data = ('{{{}{}}} '.format('{dodger blue} ' * 20, '#ff0000 ' * 20) * 20 +
'{{{}{}}} '.format('LightGoldenrod ' * 20, 'green ' * 20) * 20)
img.put(data, to=(0, 0, 80, 80))

data = ('{{{}{}}} '.format('{dodger blue} ' * 20, '#ff0000 ' * 10) * 20 +
'{{{}{}}} '.format('LightGoldenrod ' * 20, 'green ' * 10) * 10)

发布于 2012-05-03 01:20:16
尝试构造一个二维颜色数组,并使用该数组作为参数调用put。
如下所示:
import tkinter as tk
img = tk.PhotoImage(file="myFile.gif")
# "#%02x%02x%02x" % (255,0,0) means 'red'
line = '{' + ' '.join(["#%02x%02x%02x" % (255,0,0)] * 1000) + '}'
img.put(' '.join([line] * 1000))https://stackoverflow.com/questions/10417524
复制相似问题