浏览全局file_name变量中的图像后,imread中的错误

时间:2019-04-25 12:21:58

标签: python image tkinter imread

我用tkinter创建了一个简单的用户界面,该界面将允许用户浏览图像,并在按下“计算角度”按钮时,应在所选图像中打印两行之间的角度,并在屏幕上打印该角度的值。 python控制台,但出现这些错误:

File "D:\Python\PyFolder\lib\tkinter\__init__.py", line 1705, in __call__
    return self.func(*args)
  File "D:/PyCharm/PyCharm Community Edition 2018.3.5/PyProjetcs/angelTest/tkTest.py", line 21, in calculate
    image = imread(file_name)
  File "D:\PyCharm\PyCharm Community Edition 2018.3.5\PyProjetcs\angelTest\venv\lib\site-packages\matplotlib\pyplot.py", line 2152, in imread
    return matplotlib.image.imread(fname, format)
  File "D:\PyCharm\PyCharm Community Edition 2018.3.5\PyProjetcs\angelTest\venv\lib\site-packages\matplotlib\image.py", line 1369, in imread
    return handler(fname)
OSError: failed to read file

这是我的python代码:

from tkinter import *
import tkinter.messagebox
from tkinter import filedialog
import numpy as np
from skimage.transform import (hough_line, hough_line_peaks)
from pylab import imread


root = Tk()
root.geometry('270x250')
root.title("Angle Calculation")
root.iconbitmap(r'D:\\Pictures\\iconTest.ico')


def browse_file():
    global file_name
    file_name = filedialog.askopenfile()


def calculate():
    image = imread(file_name)
    image = np.mean(image, axis=2)

    h, theta, d = hough_line(image)

    angle = []
    dist = []

    for _, a, d in zip(*hough_line_peaks(h, theta, d)):
        angle.append(a)
        dist.append(d)

    angle = [a * 180 / np.pi for a in angle]
    angle_reel = np.max(angle) - np.min(angle)

    print(angle_reel)


btn1 = Button(root, command=browse_file, text='Browse Image').pack()

btn2 = Button(root, command=calculate, text='Calculate angle').pack()

label1 = Label(root, text='The angle is equal to:').pack()
text = Entry().pack()


root.mainloop()

任何人都可以向我解释我在哪里搞砸了,如何解决它,谢谢。

1 个答案:

答案 0 :(得分:1)

您的问题可能是Tensorflow,它使文件对象可以用askopenfile创建。
您需要open(selected_file)来获取其名称(作为字符串)。

或者使用file_name.name而不是文件对象来获取名称。