Ich benutze PIL in Python 3 in einem Programm, das ein Bild neu einfärbt. Was es tut ist, es durchläuft permanent die Pixelwerte von file.gif
(die als Ganzzahlen zurückgegeben werden) und fügt dann 10 zu jedem von ihnen hinzu. Ich möchte dann, dass es die Datei speichert, die dann wieder geöffnet werden kann, um auf ein Tkinter Label zu schreiben (ich habe den Rest, aber ich muss nur über das Speichern wissen).Wie speichere ich ein Bild in Python 3 mit PIL?
Mit diesem Code habe ich das Programm, um das Bild zu öffnen und es im Fenster anzuzeigen, dann ändern Sie die Pixelwerte, aber es ändert nicht das angezeigte Bild.
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()
Edit: @Tadhg McDonald-Jensen Ich habe nur das Programm mit allen vorgeschlagenen Änderungen laufen, haben aber diesen Fehler habe:
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: hier ist mein
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()
, warum Sie nicht 'ImageTk.PhotoImage' verwenden? –
Wie würde ich das tun? Ich habe vergessen zu erwähnen, dass ich immer noch relativ neu bin zu tkinter – sonrad10
du musst dich nie als neu bezeichnen (dein rep hilft dabei, es zu implizieren) Ich werde gerne eine Antwort posten ... –