簡體   English   中英

如何使用 PIL 在 python 3 中保存圖像?

[英]How do I save an image in python 3 using PIL?

我在重新着色圖像的程序中在 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 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'

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()

每次你這樣做:

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()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM