2017-01-22 40 views
0

我有一個圖像處理程序,類似於Photoshop,但非常簡單(按鈕和圖像顯示)。在我的程序中,您可以選擇想要使用和修改的圖像。我目前正在研究程序的popart部分,但是當我使用CheGevara的圖像(稱爲「chev.png」)時,它會給出以下錯誤(函數是「def PopArt()」):Python PIL int對象不可下標

Exception in Tkinter callback 
Traceback (most recent call last): 
    File "C:\EduPython\App\lib\tkinter\__init__.py", line 1538, in __call__ 
    return self.func(*args) 
    File "C:\Users\omar\Desktop\azer\Test 1.py", line 52, in PopArt 
    o=int((p[0]+p[1]+p[2])/3) 
TypeError: 'int' object is not subscriptable 

當我使用像「fleur1.png」或「face.png」等其他圖像時,它工作正常,並顯示修改就好。下面的代碼:

from tkinter import* 
import tkinter as Tkinter 
from tkinter import filedialog, DISABLED 
import os 
import ntpath 
from PIL import Image 

def EchelleDeGris(): 
    Ima2=Image.new("RGB",(z[0],z[1])) 
    px=Ima1.load() 
    px1=Ima2.load() 
    for x in range(z[0]): 
     for y in range(z[1]): 
      p=px[x,y] 
      o=int((p[0]+p[1]+p[2])/3) 
      px1[x,y]=(o,o,o) 
    Ima2.save("ImageMod.png") 
    im2 = PhotoImage(file="ImageMod.png") 
    main.image = im2 
    I2 = Tkinter.Label(main, image=im2) 
    I2.grid(row=0, column=3, rowspan =6) 

def SupprimerImage(): 
    I2 = Tkinter.Label(main, image=imt) 
    I2.grid(row=0, column=3, rowspan =6) 

def Luminosite(): 
    Ima2=Image.new("RGB",(z[0],z[1])) 
    px=Ima1.load() 
    px1=Ima2.load() 
    for x in range(z[0]): 
     for y in range(z[1]): 
      p=px[x,y] 
      px1[x,y]=(p[0]+S1.get(),p[1]+S1.get(),p[2]+S1.get()) 
    Ima2.save("ImageMod.png") 
    im2 = PhotoImage(file="ImageMod.png") 
    main.image = im2 
    I2 = Tkinter.Label(main, image=im2) 
    I2.grid(row=0, column=3, rowspan =6) 

def AnnulerModifications(): 
    I2 = Tkinter.Label(main, image=im1) 
    I2.grid(row=0, column=3, rowspan =6) 

def PopArt(): 
    Ima2=Image.new("RGB",(z[0],z[1])) 
    px=Ima1.load() 
    px1=Ima2.load() 
    for x in range(z[0]): 
     for y in range(z[1]): 
      p=px[x,y] 
      o=int((p[0]+p[1]+p[2])/3) 
      if o<85: 
       px1[x,y]=(0,0,255) 
      elif 85<o<170: 
       px1[x,y]=(0,255,0) 
      elif o>170: 
       px1[x,y]=(255,0,0) 
    Ima2.save("ImageMod.png") 
    im2 = PhotoImage(file="ImageMod.png") 
    main.image = im2 
    I2 = Tkinter.Label(main, image=im2) 
    I2.grid(row=0, column=3, rowspan =6) 






main=Tk() 

main.withdraw() 
currdir = os.getcwd() 
a = filedialog.askopenfilename() 
main.deiconify() 

main.configure(background="#a1dbcd") 
main.title("Photoshop Version.Megzari") 

Ima1=Image.open(a) 
z=Ima1.size 
nux=Image.new("RGB",(z[0],z[1])) 
nuxy=nux.load() 
for x in range(z[0]): 
    for y in range(z[1]): 
     nuxy[x,y]=(255,255,255) 
nux.save("Blank.png") 








if z>(400,400): 
    im2 = Tkinter.PhotoImage(file="ImageBlank.png") 
    main.image = im2 
    I2 = Tkinter.Label(main, image=im2) 
    I2.grid(padx=20, pady=20, row=0, column=1, rowspan =6) 
    T1 = Tkinter.Label(main, image=im2) 
    T1.grid(padx=20, pady=20, row=0, column=3, rowspan =6) 
    B1 = Tkinter.Button(main, text ="Echelle de gris", command = EchelleDeGris, fg="#a1dbcd", bg="#383a39", state=DISABLED) 
    B1.grid(padx=20, pady=20, row=0, column=2) 
    B3 = Tkinter.Button(main, text ="Appliquer Luminosité", command = Luminosite, fg="#a1dbcd", bg="#383a39", state=DISABLED) 
    B3.grid(padx=20, pady=20, row=2, column=2) 
    S1 = Scale(main, from_=0, to=254, orient=HORIZONTAL, fg="#a1dbcd", bg="#383a39", state=DISABLED, length = 200) 
    S1.grid(row=1, column=2) 
    B2 = Tkinter.Button(main, text ="Supprimer Image", command = SupprimerImage, fg="#a1dbcd", bg="#383a39", state=DISABLED) 
    B2.grid(padx=20, pady=20, row=4, column=2) 
    B3 = Tkinter.Button(main, text ="Annuler Modifications", command = AnnulerModifications, fg="#a1dbcd", bg="#383a39", state=DISABLED) 
    B3.grid(padx=20, pady=20, row=3, column=2) 
    B4 = Tkinter.Button(main, text ="Pop Art", command = PopArt, fg="#a1dbcd", bg="#383a39", state=DISABLED) 
    B4.grid(padx=20, pady=20, row=5, column=2) 
elif z<(400,400): 
    im1 = Tkinter.PhotoImage(file=a) 
    I1 = Tkinter.Label(main, image=im1) 
    I1.grid(padx=20, pady=20, row=0, column=1, rowspan =6) 
    imt = Tkinter.PhotoImage(file="Blank.png") 
    T1 = Tkinter.Label(main, image=imt) 
    T1.grid(padx=20, pady=20, row=0, column=3, rowspan =6) 
    B1 = Tkinter.Button(main, text ="Echelle de gris", command = EchelleDeGris, fg="#a1dbcd", bg="#383a39", state=NORMAL) 
    B1.grid(padx=20, pady=20, row=0, column=2) 
    B3 = Tkinter.Button(main, text ="Appliquer Luminosité", command = Luminosite, fg="#a1dbcd", bg="#383a39") 
    B3.grid(padx=20, pady=20, row=2, column=2) 
    S1 = Scale(main, from_=0, to=254, orient=HORIZONTAL, fg="#a1dbcd", bg="#383a39", length = 200) 
    S1.grid(row=1, column=2) 
    B2 = Tkinter.Button(main, text ="Supprimer Image", command = SupprimerImage, fg="#a1dbcd", bg="#383a39") 
    B2.grid(padx=20, pady=20, row=4, column=2) 
    B3 = Tkinter.Button(main, text ="Annuler Modifications", command = AnnulerModifications, fg="#a1dbcd", bg="#383a39") 
    B3.grid(padx=20, pady=20, row=3, column=2) 
    B4 = Tkinter.Button(main, text ="Pop Art", command = PopArt, fg="#a1dbcd", bg="#383a39") 
    B4.grid(padx=20, pady=20, row=5, column=2) 

s=S1.get() 




main.mainloop() 

下面是我使用的圖像(所有的人都PNG,但chev.png和fleur.png從JPG轉換,並fleur1.png從fleur.png調整):

chev.png

編輯:其他的圖片都在評論,因爲我不能把超過1個鏈接在我的崗位

+0

我不能發佈超過1個鏈接(少於10個聲望),所以我把它們放在評論中:[face.png] [2]; [2]:https://i.stack.imgur.com/bz9Gd.png –

+0

[fleur1.png] [3]; [3]:https://i.stack.imgur.com/JtgH4.png –

+0

[fleur.png] [4]; [4]:https://i.stack.imgur.com/ZqPYr.png –

回答

1

事實上,我找到了解決辦法。 My chev.png是灰度級圖像,所以他的像素值是一個整數,其他元素是元組(R,G,B)。因此,所有我要做的就是檢查像素的類型:

if type(p)==int: 

那麼,如果它是一個整數,他把它轉換成一個元組:

p=(p,p,p) 

和你所有的設置;)

+0

將圖像轉換爲RGB會更好。如果圖像被索引,那麼'p'將是一個顏色索引,元組'(p,p,p)'不會代表像素的實際顏色。只要做'px = Ima1.load()。convert('RGB')'。 –