image =具有变量的ImageTk.PhotoImage

时间:2019-01-30 09:12:26

标签: python python-3.x tkinter

所以基本上,我想使用image = ImageTk.PhotoImage()打开图片,并且可以正常使用

image = ImageTk.PhotoImage(file="C:/Users/timol/PycharmProjects/Test1/haha.jpg")

在代码的早期,我将路径保存在一个变量中,该变量是

def click():
    file = askopenfilename(initialdir='C:/Users/%s')
    directory = os.path.split(file)[0]
    print(directory)

现在,我想使用保存在“目录”中的路径放入image = ImageTk.PhotoImage(file= "directory") 但这只会给我带来很多错误。

如果您需要知道,我想这样做,因为我希望以后的用户能够上载图像并在程序中显示/使用。

这是完整的代码:

`from tkinter import *
from PIL import ImageTk, Image, ImageDraw
import PIL
import os
from tkinter import filedialog

root = Tk()
root.title("Zahlenerfassung")
root.geometry("500x400")
topFrame = Frame(root)
topFrame.pack()
bottomFrame = Frame(root)
bottomFrame.pack(side=BOTTOM)
def create(): command=os.system("start "+"pepsi.txt")
#def click():
    # Get the file
   # file = askopenfilename(initialdir='C:/Users/%s')
    # Split the filepath to get the directory
#  directory = os.path.split(file)[0]
#   print(directory)

def click():
     image_file_location = filedialog.askopenfilename(initialdir="C:/Users/%s")
     image = ImageTk.PhotoImage(file=image_file_location)
     canvas.create_image(50, 50, image=image, anchor=NW)

button1 = Button(topFrame, text="Bild auswerten", fg="red")
button2 = Button(bottomFrame, text="Erstelle ein Bild", fg="blue", command=lambda: create())
button3 = Button(bottomFrame, text="Lade dein Bild hoch", fg="blue", command=lambda: click())

canvas = Canvas(width=200, height=200)
canvas.pack(expand=NO, fill=NONE)

button1.pack(side=LEFT)
button2.pack(side=LEFT)
button3.pack(side=LEFT)

one = Label(root, text="", fg="white")
one.pack(fill=BOTH, expand=TRUE)
root.mainloop()

2 个答案:

答案 0 :(得分:0)

您可以尝试以下方法:

from tkinter import *
from PIL import ImageTk, Image
import os
from tkinter import filedialog

root = Tk()
root.title("Zahlenerfassung")
root.geometry("500x400")
topFrame = Frame(root)
topFrame.pack()
bottomFrame = Frame(root)
bottomFrame.pack(side=BOTTOM)
def create():
    command=os.system("start "+"pepsi.txt")

def click():
     image_file_location = filedialog.askopenfilename(initialdir="C:/Users/%s")
     img = Image.open(image_file_location)
     canvas.image = ImageTk.PhotoImage(img.resize((200, 200), Image.ANTIALIAS))     #  this line is for resizing the image to fit canvas size
     # canvas.image = ImageTk.PhotoImage(img)   #  you have to use this line instead of the upper line if you don't want resizing
     canvas.create_image(0, 0, image=canvas.image, anchor='nw')

button1 = Button(topFrame, text="Bild auswerten", fg="red")
button2 = Button(bottomFrame, text="Erstelle ein Bild", fg="blue", command=lambda: create())
button3 = Button(bottomFrame, text="Lade dein Bild hoch", fg="blue", command=lambda: click())

canvas = Canvas(width=200, height=200)
canvas.pack(expand=NO, fill=NONE)

button1.pack(side=LEFT)
button2.pack(side=LEFT)
button3.pack(side=LEFT)

one = Label(root, text="", fg="white")
one.pack(fill=BOTH, expand=TRUE)
root.mainloop()

答案 1 :(得分:0)

我使用了askopenfile()及其name属性来获取文件名。 PhotoImage接受类型为Image.open()的图像文件。我已经测试了此代码,并且图像显示在画布窗口内。

from tkinter import filedialog, Tk, Canvas, NW, NO, NONE, mainloop
import os
from PIL import ImageTk, Image


canvas = Canvas(width=200, height=200)
canvas.pack(expand=NO, fill=NONE)
image_file_loc = filedialog.askopenfile(initialdir=r'C:\Users')
file_path = image_file_loc.name
image = Image.open(file_path)
tk_image = ImageTk.PhotoImage(image)
canvas.create_image(50, 50, image=tk_image, anchor=NW)
mainloop()