我在一个脚本上工作,允许用户选择他们的计算机上的文件(图像),然后在脚本中处理。所有这些都可以毫无问题地工作。脚本的最后一部分使用AppJar创建图形用户界面,并显示从前面的处理中收集的信息。但是,唯一不起作用的是显示用户在开始时使用GUI选择的图像。下面是负责上述功能的代码片段。
选择文件:
Tk().withdraw()
filename = askopenfilename()
print(filename)
Tk().destroy()启动GUI并添加图片:
app = gui()
app.setImageLocation(filename)
app.addImage(Processed_Image, filename, compound=None)
for i in range(0, extractor):
app.addLabel(f"f{number}", labels1[diction_counter])
number += 1
diction_counter += 1
app.go()除图像外,GUI可正常工作。返回以下错误:
Exception: Invalid image location: /Users/xxx/Pictures/2017_10_31/IMG_5271.JPG我很感谢任何关于如何解决这个问题的建议。
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
UpDate:
在尝试了XanderMJ和Malte提出的解决方案后,唯一不能按预期方式工作的就是图形用户界面中的图像。然而,文件的位置,这是一开始的问题,由Malte提出的代码解决了。然而,在运行代码时,我收到以下错误消息:
app.addImage("Analyzed", filename, compound=None)
File "/Users/XXX/anaconda3/lib/python3.6/site-packages/appJar/appjar.py", line
7118, in addImage
self._addImageObj(name, imgObj, row, column, colspan, rowspan,
compound=compound)
File "/Users/XXX/anaconda3/lib/python3.6/site-packages/appJar/appjar.py", line
7144, in _addImageObj
label.config(anchor=CENTER,
font=self._getContainerProperty('labelFont'),image=img)
File "/Users/XXX/anaconda3/lib/python3.6/tkinter/__init__.py", line 1482, in
configure
return self._configure('configure', cnf, kw)
File "/Users/XXX/anaconda3/lib/python3.6/tkinter/__init__.py", line 1473, in
_configure
self.tk.call(_flatten((self._w, cmd)) + self._options(cnf))
_tkinter.TclError: image "pyimage1" doesn't exist关于这个错误的有趣部分是,在开始时选择的图像不是名为pyimage1,而是具有诸如"img_1245.jpg“之类的名称。此外,将代码作为单独的程序员运行,而不使用变量作为文件或路径的名称,代码运行得很好。下面是我刚才提到的带变量和不带变量的代码部分。
此代码不会导致问题:
from appJar import gui
from PIL import Image, ImageTk
app = gui()
app.setImageLocation("/Users/XXX/Desktop")
app.addImage("test", "road.jpg")
app.zoomImage("test", -3)
app.go()此代码导致前面显示的错误:
app = gui()
app.setImageLocation(directory)
app.addImage("Analyzed", filename)
for i in range(0, extractor):
app.addLabel(f"f{number}", labels1[diction_counter])
number += 1
diction_counter += 1
app.go()发布于 2018-07-03 23:52:19
发布于 2018-07-04 15:28:16
通过将文件路径函数更新为以下内容,您可以单独提取目录、带有文件名的目录和文件名:
Tk().withdraw()
path = askopenfilename()
filename = os.path.basename(path)
directory = os.path.split(path)[0]
Tk().destroy()如果您还没有导入import os.path,则需要这样做。
此外,要显示图像/图片,应执行以下操作:
app.setImageLocation(directory)
app.addImage(title, file, compound=None)希望这能有所帮助!
发布于 2021-11-03 06:56:33
我认为你应该在路径中使用双斜杠。enter image description here,我也遇到过同样的问题,这就是解决方案。
https://stackoverflow.com/questions/51157322
复制相似问题