2016-03-25 8 views
2

Python 3'ü bir görüntüyü yeniden yükleyen bir programda kullanıyorum. Yaptığı şey, kalıcı olarak file.gif piksel değerlerini (tamsayı olarak döndürülür) döndürür ve sonra her birine 10 ekler. Daha sonra dosyayı kaydetmek istiyorum, ki daha sonra daha titiz bir etiket yazmak için yeniden açılabilir (gerisini aldım, ama sadece tasarruf hakkında bilmem gerek).PIL kullanarak görüntüyü python 3'e nasıl kaydederim?

Bu kodu kullanarak, görüntüyü açmak ve pencerede görüntülemek için programa sahiptim, sonra piksel değerlerini değiştirdim, ancak görüntülenen görüntü değişmiyor. Şu anda bu görüntüyü kullanıyorum

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

Düzenleme: @Tadhg McDonald-Jensen Ben sadece tüm önerdiğiniz düzenlemeleri içeren programını çalıştırmak ettik, ancak bu hatayı var:

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: benim olayım dosyaya dikkat,

PhotoImage(file="AI.gif") 

dosyayı yeniden yüklüyorsunuz:

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

: sadece tüm önerdiğim düzenlemeleri içeren kodudur burada netleştirmek için? –

+0

Bunu nasıl yaparım? Ben – sonrad10

+0

tinkter hala yeni nispeten yeni bahsetmeyi unuttum, kendinizi yeni olarak etiketlemek zorunda kalmazsınız (sizin rep it yardımcı olur) Ben mutlu bir cevap yazacağım ... –

cevap

2

yapmanız her zaman: görünmüyor kodunun en son sürümünü Tkinter penceresindeki görüntüyü değiştirerek olmak Bu süreç boyunca asla değişmez, dolayısıyla görüntü hiçbir zaman değişmez. Eğer PIL ile görüntüyü yüklü varsa o zaman görüntüden veri yüklemek için ImageTk.PhotoImage kullanabilirsiniz:

photo = ImageTk.PhotoImage(img) 

(img tanımlayan sonra bu yapmak emin olun) o zaman gerek asla yeniden açmak bununla resim:

:

Onun yerine sadece birlikte img yeni verilerle photo sonra, img yeni piksel verilerini koymak güncellemeniz gerekir

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

DÜZENLEME: neden `ImageTk.PhotoImage` kullanmıyorsanız

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

Ayrıca, bir pikselin değerini 10 artırmanın bir “gif” olduğunu düşündüğünüzden de bahsetmek istemiyorum. Bunu aklınızda bulundurduğunuz şey büyük olasılıkla değil, RGB'ye dönüştürmeniz ve bu verileri değiştirmeniz gerekebilir. –

+1

Bunu nasıl yaparım? Biliyorum bunu .jpg' için otomatik olarak yapar, ama gifs için de yapabilirsin farkında değildim – sonrad10

+0

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