2016-03-25 27 views
2

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

ich dieses Bild bin derzeit: enter image description here

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

, warum Sie nicht 'ImageTk.PhotoImage' verwenden? –

+0

Wie würde ich das tun? Ich habe vergessen zu erwähnen, dass ich immer noch relativ neu bin zu tkinter – sonrad10

+0

du musst dich nie als neu bezeichnen (dein rep hilft dabei, es zu implizieren) Ich werde gerne eine Antwort posten ... –

Antwort

2

jedes Mal, wenn Sie tun: neueste Version des Codes, der nicht das Bild im tkinter Fenster modifiziert zu sein scheint

PhotoImage(file="AI.gif") 

Sie laden die Datei erneut beachten, dass die Datei ändert sich nie während dieses Prozesses, daher ändert sich das Bild nie. wenn Sie das Foto mit PIL geladen haben, dann können Sie ImageTk.PhotoImage verwenden, um die Daten aus dem Bild zu laden:

photo = ImageTk.PhotoImage(img) 

(sicher sein, dies zu tun nach definieren img) dann müssen Sie nie wieder öffnen Bild mit diesem:

Stattdessen müssen Sie nur die neuen Pixeldaten in img setzen, dann aktualisieren photo mit den neuen Daten in img mit:

img.putdata(pix) 
photo.paste(img) 

EDIT: gerade hier zu klären, ist der Code mit allen meinen vorgeschlagenen Änderungen:

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

Ich möchte auch erwähnen, dass, weil das Bild ein 'gif' ist, die Erhöhung des Pixelwerts um 10 sehr wahrscheinlich nicht das ist, was Sie im Sinn hatten, müssen Sie es möglicherweise in RGB konvertieren und diese Daten stattdessen ändern. –

+1

Wie würde ich das tun? Ich weiß, es macht es automatisch für '.jpg' Dateien, aber ich wusste nicht, dass du es auch für Gifs machen könntest. – sonrad10

+0

' img = Image.open ("AI.gif") '[' .convert ("RGB") '] (https://pillow.readthedocs.org/en/3.1.x/reference/Image.html#PIL.Image.Image.convert) –

Verwandte Themen