如何使用 PIL 将图像保存在 python 3 中?
How do I save an image in python 3 using PIL?
我在 python 3 的程序中使用 PIL 为图像重新着色。它所做的是,它永久循环遍历 file.gif
的像素值(以整数形式返回),然后将每个像素值加 10。然后我希望它保存文件,然后可以重新打开以写入 tkinter 标签(我已经得到了其余部分,但我只需要了解保存)。
使用这段代码,我得到了打开图像并将其显示在 window 中的程序,然后修改像素值,但它不会改变显示的图像。
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 McDonald-Jensen
我刚刚 运行 包含您建议的所有编辑的程序,但出现此错误:
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'
编辑2:
这是我最新版本的代码,似乎没有修改 tkinter window:
中的图像
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()
每次你这样做:
PhotoImage(file="AI.gif")
您正在再次加载文件,请注意,文件在整个过程中不会更改,因此图像也不会更改。如果您已经使用 PIL 加载了图像,那么您可以使用 ImageTk.PhotoImage
从图像中加载数据:
photo = ImageTk.PhotoImage(img)
(一定要在定义之后img
执行此操作)
那么你永远不需要用这个重新打开图像:
photo = PhotoImage(file="AI.gif")
x.configure(image=photo)
相反,您只需要将新的像素数据放入 img,然后使用 img
中的新数据更新 photo
并使用:
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()
我在 python 3 的程序中使用 PIL 为图像重新着色。它所做的是,它永久循环遍历 file.gif
的像素值(以整数形式返回),然后将每个像素值加 10。然后我希望它保存文件,然后可以重新打开以写入 tkinter 标签(我已经得到了其余部分,但我只需要了解保存)。
使用这段代码,我得到了打开图像并将其显示在 window 中的程序,然后修改像素值,但它不会改变显示的图像。
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 McDonald-Jensen 我刚刚 运行 包含您建议的所有编辑的程序,但出现此错误:
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'
编辑2: 这是我最新版本的代码,似乎没有修改 tkinter window:
中的图像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()
每次你这样做:
PhotoImage(file="AI.gif")
您正在再次加载文件,请注意,文件在整个过程中不会更改,因此图像也不会更改。如果您已经使用 PIL 加载了图像,那么您可以使用 ImageTk.PhotoImage
从图像中加载数据:
photo = ImageTk.PhotoImage(img)
(一定要在定义之后img
执行此操作)
那么你永远不需要用这个重新打开图像:
photo = PhotoImage(file="AI.gif")
x.configure(image=photo)
相反,您只需要将新的像素数据放入 img,然后使用 img
中的新数据更新 photo
并使用:
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()