PIL图像索引超出范围(PIL image index out of range)

编程入门 行业动态 更新时间:2024-10-26 02:32:55
PIL图像索引超出范围(PIL image index out of range)

我正在写一个关于tkinter的程序,类似于photoshop(图像处理),问题是我的一个函数(Moyenne())告诉我索引超出范围,但它之前有效,但现在没有。 我搜索过但我没有找到任何东西。 如果您需要任何规格,请询问;)

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) def Moyenne(): Ima2=Image.new("RGB",(z[0],z[1])) px=Ima1.load() px1=Ima2.load() rouge=0 vert=0 bleu=0 for x in range(1,z[0]): for y in range(1,z[1]): for t in range(-1,1): for w in range(-1,1): k=px[x+t,y+w] if k[0]>k[1] and k[0]>k[2]: rouge=rouge+1 if k[1]>k[0] and k[1]>k[2]: vert=vert+1 if k[2]>k[1] and k[2]>k[0]: bleu=bleu+1 x=x-1 y=y-1 Max=rouge if bleu>Max: Max=bleu elif vert>Max: Max=vert pax=px[x,y] if Max == rouge: pax=(pax[0],0,0) if Max == vert: pax=(0,pax[1],0) if Max == bleu: pax = (0,0,pax[2]) Ima2.show() 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() Moyenne() main.mainloop()

我在defMoyenne()中复制了代码并将其放在另一个项目中并对其进行了测试但仍无法正常工作:

from PIL import Image Ima1=Image.open("face.png") z=Ima1.size Ima2=Image.new("RGB",(z[0],z[1])) px=Ima1.load() px1=Ima2.load() rouge=0 vert=0 bleu=0 for x in range(1,z[0]-1): for y in range(1,z[1]-1): for t in range(-1,1): print(t) for w in range(-1,1): print(w) k=px[x+t,y+w] if k[0]>k[1] and k[0]>k[2]: rouge=rouge+1 if k[1]>k[0] and k[1]>k[2]: vert=vert+1 if k[2]>k[1] and k[2]>k[0]: bleu=bleu+1 x=x-1 y=y-1 Max=rouge if bleu>Max: Max=bleu elif vert>Max: Max=vert pax=px[x,y] if Max == rouge: pax=(pax[0],0,0) if Max == vert: pax=(0,pax[1],0) if Max == bleu: pax = (0,0,pax[2]) Ima2.show()

I'm writing a program on tkinter similar to photoshop (image treatment), and the problem is one of my functions (Moyenne()) is telling me index out of range, but it's worked before but not now. I've searched but I haven't found anything. If you need any specification just ask ;)

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) def Moyenne(): Ima2=Image.new("RGB",(z[0],z[1])) px=Ima1.load() px1=Ima2.load() rouge=0 vert=0 bleu=0 for x in range(1,z[0]): for y in range(1,z[1]): for t in range(-1,1): for w in range(-1,1): k=px[x+t,y+w] if k[0]>k[1] and k[0]>k[2]: rouge=rouge+1 if k[1]>k[0] and k[1]>k[2]: vert=vert+1 if k[2]>k[1] and k[2]>k[0]: bleu=bleu+1 x=x-1 y=y-1 Max=rouge if bleu>Max: Max=bleu elif vert>Max: Max=vert pax=px[x,y] if Max == rouge: pax=(pax[0],0,0) if Max == vert: pax=(0,pax[1],0) if Max == bleu: pax = (0,0,pax[2]) Ima2.show() 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() Moyenne() main.mainloop()

I copied the code in the defMoyenne() and put it in another project and tested it and still doesn't work:

from PIL import Image Ima1=Image.open("face.png") z=Ima1.size Ima2=Image.new("RGB",(z[0],z[1])) px=Ima1.load() px1=Ima2.load() rouge=0 vert=0 bleu=0 for x in range(1,z[0]-1): for y in range(1,z[1]-1): for t in range(-1,1): print(t) for w in range(-1,1): print(w) k=px[x+t,y+w] if k[0]>k[1] and k[0]>k[2]: rouge=rouge+1 if k[1]>k[0] and k[1]>k[2]: vert=vert+1 if k[2]>k[1] and k[2]>k[0]: bleu=bleu+1 x=x-1 y=y-1 Max=rouge if bleu>Max: Max=bleu elif vert>Max: Max=vert pax=px[x,y] if Max == rouge: pax=(pax[0],0,0) if Max == vert: pax=(0,pax[1],0) if Max == bleu: pax = (0,0,pax[2]) Ima2.show()

最满意答案

你的问题是由Moyenne()体内的这些陈述引起的:

x=x-1 y=y-1

无论出于何种原因,您都在手动递减循环体中的循环变量。 这最终将导致x在达到k=px[x+t,y+w]时变为0的情况,并且如果t为-1,这将导致您尝试访问索引处的像素[-1,y+w] ,导致您看到“索引超出范围”错误。

Your problem is caused by these statements in the body of Moyenne():

x=x-1 y=y-1

For whatever reason you're manually decrementing your loop variables in the body of the loop. This will eventually result in a case where x becomes 0 by the time you reach k=px[x+t,y+w], and if t is -1 this will result in you trying to access the pixel at index [-1,y+w], causing the "Index out of range" error you see.

更多推荐

本文发布于:2023-04-29 09:52:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1335874.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:索引   图像   PIL   range   index

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!