在全局 file_name 变量中浏览图像后 imread 出错
Error in the imread after browsing the image in a global file_name variable
我用 tkinter 创建了一个简单的用户界面,允许用户浏览图像,当按下 'calculate angle' 按钮时,它应该打印所选图像中两条线之间的角度并打印值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()
谁能告诉我我哪里搞砸了,如何解决,谢谢。
您的问题可能是 askopenfile
,它使用 open(selected_file)
.
创建文件对象
您需要 file_name.name
来获取它的名称(作为字符串)。
或使用askopenfilename
获取名称而不是文件对象。
我用 tkinter 创建了一个简单的用户界面,允许用户浏览图像,当按下 'calculate angle' 按钮时,它应该打印所选图像中两条线之间的角度并打印值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()
谁能告诉我我哪里搞砸了,如何解决,谢谢。
您的问题可能是 askopenfile
,它使用 open(selected_file)
.
创建文件对象
您需要 file_name.name
来获取它的名称(作为字符串)。
或使用askopenfilename
获取名称而不是文件对象。