我正在做一个简单的程序,让用户重新调整图像的大小。但我遇到了一个问题。当我试图使用Image.open()打开图像时,我得到以下错误:
AttributeError: class Image has no attribute 'open'
我已经研究过这一点,它是从分配一些东西给Image,比如把它变成一个变量。但是,在我的代码中,我看不到我做了任何分配给Image的事情。
这是我的代码:
from PIL import Image
from Tkinter import *
import tkFileDialog
import ttk
class Application(Frame):
def __init__(self, parent):
Frame.__init__(self,parent)
self.pack(fill=BOTH)
self.create_widgets()
def create_widgets(self):
self.tfr = Frame(self)
self.tfr.pack(side=TOP)
self.title = Label(self.tfr, font=("Arial", 20), text="Image Resizer")
self.title.pack(side=TOP, fill=X, padx=40)
self.spacer = Frame(self.tfr, bg="black")
self.spacer.pack(side=TOP, fill=X)
self.mfr = Frame(self)
self.mfr.pack(side=TOP)
self.brButton = ttk.Button(self.mfr, text="Browse", command=self.browse)
self.brButton.pack(side=LEFT, padx=(0, 2), pady=2)
self.diField = Label(self.mfr, text="File Path...", relief=SOLID, bd=1, width=25, anchor=W)
self.diField.pack(side=LEFT)
self.spacer2 = Frame(self, bg="black")
self.spacer2.pack(side=TOP, fill=X)
self.bfr = Frame(self)
self.bfr.pack(side=TOP)
self.rButton = ttk.Button(self.bfr, text="Resize", width=41, command=self.resize)
self.rButton.pack(side=TOP, pady=2)
def browse(self):
supportedFiles = [("PNG", "*.png"), ("JPEG", "*.jpg,*.jpeg,*.jpe,*.jfif"), ("GIF", "*.gif")]
filePath = tkFileDialog.askopenfile(filetypes=supportedFiles, defaultextension=".png", mode="rb")
if filePath != None:
photo = Image.open(filePath, "rb")
size = photo.size
print(size)
else:
pass
def resize(self):
print("Resize")
root = Tk()
root.title("Image Resizer")
root.resizable(0,0)
app = Application(root)
root.mainloop()有人能解释我为什么会犯这个错误吗。任何帮助都是非常感谢的。
发布于 2015-01-21 23:01:03
您确实应该出于明显的原因避免使用from PIL Tkinter import *,但是如果必须的话,您可以使用from PIL import IMAGE as img来区别于Tkinter Image
发布于 2015-01-21 22:49:37
Image从Tkinter代替来自PIL的相同。
发布于 2015-01-21 23:21:13
使用ImageTk代替:
from PIL import ImageTk # add to imports
# later on when loading selected image
photo = ImageTk.PhotoImage(file = filePath)
size = photo.width(), photo.height()
print(size)https://stackoverflow.com/questions/28078276
复制相似问题