0


I am working on a script that allows the user to select a file(image) on their computer which is then processed in the script. All of that works without a problem. The last part of the script creates a GUI using AppJar and displays information gathered from the previous processing. However, the only thing that doesn't work is displaying the image that the user chose in the beginning using the GUI. Below are snippets of the code which is responsible for the described functions from above.

Choosing the File:

Tk().withdraw() 
filename = askopenfilename() 
print(filename)
Tk().destroy()

Starting GUI and adding the picture:

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()

The GUI works except for the Image. The following error is returned:

Exception: Invalid image location: /Users/xxx/Pictures/2017_10_31/IMG_5271.JPG

I appreciate any tips on how to solve this issue.

+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

UpDate:
After trying the solutions proposed by XanderMJ and Malte, the only thing that still doesn't work the way it's supposed to is the image in the GUI. However, the location of the file, which was the problem in the beginning, was solved by the code proposed by Malte. Yet when running the code, I receive the following error message:

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

The interesting part about this error is that the image that was chosen in the beginning isn't called pyimage1 but has names such as "img_1245.jpg". Furthermore, running the code as a seperate programm without variables as names for files or paths, the code works fine. Below are the parts of the code that I just mentioned with and without variables.

This code doesn't cause an issue:

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()

This code causes the previously shown error:

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()

3 Answers3

1

After a quick look at the AppJar docs: link

.setImageLocation(location) Set a folder for image files. This will be put before the names of any image files used.

At the moment you are trying to specify a path where to find images, but the path you are providing is the path to 1 image. Change it to app.setImageLocation(folder_paht), next you provide a file name for your image while using app.addImage('file.png').

So you have to get the folder path of the file(s), I would suggest checking this post about getting the folder path instead of the file path. Then you could list all the files in this folder with a specific extension (link)

Hope this helps!

XanderMJ
  • 148
  • 1
  • 8
0

Updating the file path function to the following allows you to extract the directory, the directory with the filename and the filename all by itself:

Tk().withdraw() 
path = askopenfilename() 
filename = os.path.basename(path)
directory = os.path.split(path)[0]
Tk().destroy()

You need to import import os.path if you haven't already done so.
Furthermore, to display the image/picture the following should work:

app.setImageLocation(directory)
app.addImage(title, file, compound=None)

Hope this helps!

Malte
  • 1
  • I have tried your suggestion for getting the path and the filename separately as well as adding the image to the GUI. Regarding the path and the file name, everything works as planned. However, adding the image with the suggested code returns me with an error message. I will post the complete code related to the error message as an answer into this thread. – Christopher Zerbe Jul 04 '18 at 08:31
-1

I think you should use double slash in the path. enter image description here I've had the same problem and that was the solution.

  • 1
    Please post that single-line of code as **TEXT**. There is no reason to post it as an image instead. – Gino Mempin Nov 03 '21 at 08:03
  • Also if you look at the paths in the question you can see that OP is not on Windows, your answer is off-topic. – Eric Aya Nov 03 '21 at 10:37