这是两个错误
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Users\seif- zizo\PycharmProjects\pythonProject\venv\lib\site-packages\PIL\Image.py", line 2895, in open
fp.seek(0)
AttributeError: 'str' object has no attribute 'seek'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\Users\seif- zizo\AppData\Local\Programs\Python\Python37-32\lib\tkinter\__init__.py", line 1705, in __call__
return self.func(*args)
File "C:/Users/seif- zizo/PycharmProjects/pythonProject/main.py", line 7, in resize
IMG=Image.open(data1,'r')
File "C:\Users\seif- zizo\PycharmProjects\pythonProject\venv\lib\site-packages\PIL\Image.py", line 2897, in open
fp = io.BytesIO(fp.read())
AttributeError: 'str' object has no attribute 'read'
以下是引发这些错误的代码:
from tkinter import Tk,Label,Entry,Button,RIDGE,SOLID
from PIL import Image
从data1,data2,data3,data4中获取值后调整图像大小的功能
def resize():
IMG=Image.open(data1,'r')
new_image=IMG.resize((data2,data3))
x=new_image.show()
y=new_image.save(data4)
print(x)
GUI应用程序代码:
window=Tk()
window.title("MR RESIZE")
window.iconbitmap(r"C:\Users\seif- zizo\Downloads\Google-Noto-Emoji-Smileys-10104-pile-of-poo.ico")
window.geometry('500x250')
text1=Label(text="IMAGE PATH",background="#E91EDA" ,fg="WHITE",width=22,height=5,relief=SOLID)
text2=Label(text="NEW IMAGE WIDTH",background="#2471A3" ,fg="WHITE",width=22,height=5,relief=SOLID)
text3=Label(text="NEW IMAGE HEIGHT",background="#1738B2" ,fg="WHITE",width=22,height=5,relief=SOLID)
text4=Label(text="SAVE IMAGE AS",background="#24B41B",fg="WHITE",width=22,height=5,relief=SOLID)
text1.grid(row=0,column=0,padx=5)
text2.grid(row=0,column=1,padx=5)
text3.grid(row=0,column=2,padx=5)
text4.grid(row=0,column=3,padx=5)
entry1=Entry(width=20,relief=RIDGE,borderwidth=3)
entry2=Entry(width=20,relief=RIDGE,borderwidth=3)
entry3=Entry(width=20,relief=RIDGE,borderwidth=3)
entry4=Entry(width=20,relief=RIDGE,borderwidth=3)
entry1.grid(row=1,column=0,padx=5,pady=5)
entry2.grid(row=1,column=1,padx=5,pady=5)
entry3.grid(row=1,column=2,padx=5,pady=5)
entry4.grid(row=1,column=3,padx=5,pady=5)
data1=entry1.get()
data2=entry2.get()
data3=entry3.get()
data4=entry4.get()
enter_btn=Button(text='ENTER',width=15,command=resize)
enter_btn.grid(row=2,columnspan=2,column=1,padx=5,pady=5)
for i in range(4):
window.columnconfigure(i, weight=1, minsize=30)
window.rowconfigure(i, weight=1, minsize=30)
window.mainloop()
在上面的代码中,我尝试重新缩放图像,然后在重新缩放后输出新图像。问题是我遇到了这两个错误,我不知道该怎么办。我该如何解决此代码?
答案 0 :(得分:0)
这是因为自创建data1
之后立即调用data1=entry1.get()
,entry1
是空字符串。
您应该在data1 = entry1.get()
内致电resize()
(与data2
.. data4
相同):
def resize():
data1 = entry1.get().strip()
data2 = entry2.get().strip()
data3 = entry3.get().strip()
data4 = entry4.get().strip()
if data1 and data2 and data3 and data4:
try:
data2 = int(data2)
data3 = int(data3)
IMG=Image.open(data1,'r') # 'r' is not necessary
new_image=IMG.resize((data2,data3))
new_image.show()
new_image.save(data4)
except ValueError:
print('Invalid value of width or height')
else:
print('Empty value found')