python-3.x - 如何使用 PIL 在 python 3 中保存图像?

标签 python-3.x python-imaging-library

我正在 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()

我目前正在使用这张图片: enter image description here

编辑: @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'

编辑2: 这是我的最新版本的代码,它似乎没有修改 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()

最佳答案

每次你这样做:

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.x - 如何使用 PIL 在 python 3 中保存图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36228629/

相关文章:

比较两个排序列表并计算有多少元素相同的Python算法

python - 我如何给变量自定义元数据?

python - 使用 pil/任何 python 包进行图像分割

image-processing - 我应该使用哪些处理步骤来清洁线条图的照片?

python - PIL : make image transparent on a percent scale

python - 如何将PDF转换为opencv-python可读的图像?

python - 我应该在 Python 2.7 中使用 print 语句还是函数?

python - 逐行读取文件,分割其内容,跳过空行

python - 将所有 `None` 移动到列表的末尾 (python3)

python - PIL 的 ImageGrab 以错误的分辨率捕获