我在python 3中使用PIL,在一个程序中对图像进行重新着色。它所做的是,它永久地遍历file.gif的像素值(这些值作为整数返回),然后将每一个像素值添加10个。然后,我希望它保存该文件,然后可以重新打开该文件,以便将其写入tkinter标签(剩下的部分我已经得到了,但我只需要知道保存的内容)。
使用这段代码,我得到了打开图像并显示在窗口中的程序,然后修改像素值,但是它不会改变显示的图像。
from tkinter import *
from PIL import Image
from PIL import Image, ImageTk
import time, sys
def col():
global count1,count,pix,x,root
count1+=1
print("("+str(count1)+")")
count=-1
for i in pix:
count+=1
#print(i)
i+=10
pix[count]=i
photo = PhotoImage(file="AI.gif")
x.configure(image=photo)
root.update()
root.after(100, col)
root=Tk()
photo = PhotoImage(file="AI.gif")
x=Label(root, compound="top", image=photo)
x.pack(side="right")
img = Image.open("AI.gif")
pix=list(img.getdata())
width=img.size[0]
height=img.size[1]
img.close()
root.geometry((str(width)+"x"+str(height))+"-0+0")
root.update()
count1=0
col()
root.mainloop()我目前正在使用这张图片:

编辑:@Tadhg麦克唐-詹森我刚刚运行了这个程序,上面有你建议的所有编辑,但有以下错误:
Traceback (most recent call last):
File "C:\Users\name\Desktop\recolour1.py", line 47, in <module>
col()
File "C:\Users\name\Desktop\recolour1.py", line 19, in col
photo.paste(img)
AttributeError: 'PhotoImage' object has no attribute 'paste'Edit2:这里是我最新版本的代码,它似乎没有修改tkinter窗口中的图像:
from tkinter import *
from PIL import Image
from PIL import Image, ImageTk
import time, sys
def col():
global count1,count,pix,x,root,photo
img = Image.open("AI.gif").convert("RGB")
pix=list(img.getdata())
count1+=1
print("("+str(count1)+")")
count=-1
for i in pix:
count+=1
#print(i)
i = tuple(V+100 for V in i)
img.putdata(pix)
photo.paste(img)
root.update()
img.close()
root.after(10, col)
root=Tk()
photo = ImageTk.PhotoImage(file="AI.gif")
x=Label(root, compound="top", image=photo)
x.pack(side="right")
img = Image.open("AI.gif").convert("RGB")
width,height=img.size[0],img.size[1]
img.close()
root.geometry((str(width)+"x"+str(height))+"-0+0")
root.update()
count1=0
col()
root.mainloop()发布于 2016-03-25 22:14:22
每次你这么做:
PhotoImage(file="AI.gif")您将再次加载该文件,请注意,该文件在整个过程中从不更改,因此图像永远不会更改。如果您已经用PIL加载了映像,那么可以使用ImageTk.PhotoImage从映像加载数据:
photo = ImageTk.PhotoImage(img)(在定义img之后一定要这样做),那么您就不需要用以下内容重新打开图像:
photo = PhotoImage(file="AI.gif")
x.configure(image=photo)相反,您只需要将新的像素数据放入img中,然后使用img中的新数据更新img中的以下内容:
img.putdata(pix)
photo.paste(img)编辑:为了澄清这里是您的代码与所有我建议的编辑:
from tkinter import *
from PIL import Image
from PIL import Image, ImageTk
import time, sys
def col():
global count1,count,pix,x,root,img
count1+=1
print("("+str(count1)+")")
count=-1
for i in pix:
count+=1
#print(i)
i+=10
pix[count]=i
#update the data in img and then paste it into photo
img.putdata(pix)
photo.paste(img)
root.update()
root.after(100, col)
root=Tk()
#load the image before making PhotoImage
img = Image.open("AI.gif")
pix=list(img.getdata())
width=img.size[0]
height=img.size[1]
#img.close() #don't close the image as you won't be able to modify it after closing
# do this part after defining img
photo = ImageTk.PhotoImage(img)
# ^ use PIL's PhotoImage to use PIL operations on it
x=Label(root, compound="top", image=photo)
x.pack(side="right")
root.geometry((str(width)+"x"+str(height))+"-0+0")
root.update()
count1=0
col()
root.mainloop()https://stackoverflow.com/questions/36228629
复制相似问题