【问题标题】:PIL assistance PythonPIL 辅助 Python
【发布时间】:2015-01-21 22:47:27
【问题描述】:

我正在开发一个简单的程序,可以让用户调整图像大小。但是我遇到了一个问题。当我尝试使用 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()

谁能解释我为什么会收到这个错误。非常感谢任何帮助..

【问题讨论】:

  • 你的 Image.open :如果给出了模式参数,它必须是“r”。
  • @KevinDTimm 这不是导致此错误的原因。在它甚至可以读取图像文件之前。它给了我上面的 ht 错误。
  • 正确,但这将是你的下一个问题
  • 这很好地说明了为什么全局导入不好。

标签: python tkinter syntax-error python-imaging-library


【解决方案1】:

来自TkinterImage 替换来自PIL 的相同内容。

【讨论】:

  • 有没有办法阻止它这样做。这很奇怪,因为我已经使用 TKitner 和 PIL 制作了其他 GUI,而 PIL Image.open() 没有遇到这样的问题
  • 我只需将from PIL import Image 替换为import PIL.Image 并使用PIL.Image.Image。编辑:Image.Image.
  • 我做了,我得到了以下错误:AttributeError: 'module' object has no attribute 'Image'
  • @PadraicCunningham 你能把它写成答案吗,因为它就像一个魅力:)
【解决方案2】:

出于显而易见的原因,您确实应该避免使用 from PIL Tkinter import *,但如果必须,您可以使用 from PIL import IMAGE as imgTkinter Image 区分开来

【讨论】:

    【解决方案3】:

    改用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)
    

    【讨论】:

      猜你喜欢
      • 2021-05-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-03-26
      • 2020-11-26
      • 2019-02-10
      • 1970-01-01
      • 2012-10-10
      相关资源
      最近更新 更多