关于python:尝试使用PIL打开图像

Trying to open image with PIL

本问题已经有最佳答案,请猛点这里访问。

我正试图打开一个文件与皮尔和特金特。我使用此代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
import tkinter as tk
from PIL import Image,ImageTk
from tkinter import *
intWidth=20
intHeight=5

class SampleApp(tk.Tk):

    def __init__(self, *args, **kwargs):
        tk.Tk.__init__(self, *args, **kwargs)



        # the container is where we'll stack a bunch of frames
        # on top of each other, then the one we want visible
        # will be raised above the others
        container = tk.Frame(self)
        container.grid(row=0,column=0)#pack(side="top", fill="both", expand=True)



        self.frames = {}
        for F in (StartPage, Departure, PageTwo):
            page_name = F.__name__
            frame = F(parent=container, controller=self)
            self.frames[page_name] = frame
            frame.config(bg='#FBC311')

            # put all of the pages in the same location;
            # the one on the top of the stacking order
            # will be the one that is visible.
            frame.grid(row=0, column=0, sticky="nsew")

        self.show_frame("StartPage")

    def show_frame(self, page_name):
        '''Show a frame for the given page name'''
        frame = self.frames[page_name]
        frame.tkraise()


class StartPage(tk.Frame):

    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        self.controller = controller
        for row in range(9):
            self.grid_rowconfigure(row, weight=1)
            Button(self,text="Button %d"%(row), bg = '#005ca0', fg = 'white', font ="Verdana 10", width = intWidth, height = intHeight).grid(row = row,column = 0,sticky = E+W)

        for c in range(9):
            self.grid_columnconfigure(c, weight=1)
            Button(self,text="Button %d"%(c), bg = '#005ca0', fg = 'white', font ="Verdana 10", width = intWidth, height = intHeight).grid(row = 5,column = c,sticky = E+W)

        label = tk.Label(self, text="Welkom bij NS", font='Verdana 50', fg='#005ca0', bg='#FBC311')
        label.grid(row=1,column=3,columnspan=3)

        path ="nslogo.png"
        img = ImageTk.PhotoImage(Image.open(path))
        panel = Label(self , image=img, bg = '#FBC311', width = 340)
        panel.photo = img
        panel.grid(column=4, row=2)


        button1 = tk.Button(self, text="Actuele reistijden", command=lambda: controller.show_frame("Departure"),bg='#005ca0', fg='white',width=20,height=5)
        button1.grid(row=6, column=3,sticky='nsew')
        button2 = tk.Button(self, text="Go to Page Two", command=lambda: controller.show_frame("PageTwo"),bg='#005ca0', fg='white',width=20,height=5)
        button2.grid(row=6,column=5,sticky='nsew')

class Departure(tk.Frame):

    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        self.controller = controller
        for row in range(7):
            self.grid_rowconfigure(row, weight=1)
        for c in range(7):
            self.grid_columnconfigure(c, weight=1)
        label = tk.Label(self, text="Actuele vertrektijden", font='Verdana 50', fg='#005ca0', bg='#FBC311')
        label.grid(row=0,column=2,columnspan=5)
        button = tk.Button(self, text="Start",command=lambda: controller.show_frame("StartPage"),bg='#005ca0', fg='white',width=20,height=5)
        button.grid(row=2,column=4)


class PageTwo(tk.Frame):

    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        self.controller = controller
        for row in range(7):
            self.grid_rowconfigure(row, weight=1)
        for c in range(7):
            self.grid_columnconfigure(c, weight=1)
        label = tk.Label(self, text="Storingen", font='Verdana 50', fg='#005ca0', bg='#FBC311')
        label.grid(row=0,column=2,columnspan=5)
        button = tk.Button(self, text="Start", command=lambda: controller.show_frame("StartPage"), bg='#005ca0', fg='white',width=20,height=5)
        button.grid(row=2,column=4)


app = SampleApp()

app.mainloop()

但我得到了一个错误:

文件"c:/users/kevin/desktop/hu/programming/tict-viprog-15/mini_project/test.py",第59行,in initimg=imagetk.photoimage(image.open(path))。attributeError:类型对象"image"没有属性"open"

我不知道为什么会这样,我希望有人能帮我解决这个问题。

事先谢谢。


名称空间冲突。而不是……

1
from PIL import Image,ImageTk

尝试做:

1
2
from PIL import ImageTk
from PIL import Image as PilImage

然后,在您得到错误的行中,您将执行以下操作:

1
img = ImageTk.PhotoImage(PilImage.open(path))

希望有帮助。当做。


问题是由于您导入tkinter模块的方式导致的:

1
2
from PIL import Image,ImageTk
from tkinter import *

第二次导入将用tkinter模块中包含的变量、类等的任何现有名称替换。恰好在tkinter中定义了一个Image类(没有open()方法),因此它将替换已经从PIL导入的类。

1
2
3
>> from tkinter import  *
>>> Image
<class 'tkinter.Image'>

这说明了为什么最好不要使用import *,尽管使用tkinter很常见。

解决此问题的一种方法是交换导入的顺序,以便PIL.Image替换tkinter.Image,但如果您希望在同一命名空间中同时使用这两个名称空间,现在这将阻止您使用tkinter.Image

因此,可以使用as导入,为从模块导入的项定义自己的名称:

1
from PIL import ImageTk, Image as PILImage

当你需要一个PIL.Image时,用PILImage代替Image