QFileDialog and OpenCV's imread()-Problem
-
Hi, guys,
i want to create a code where i can select different images via a GUI. For this I use
QFileFialog.getOpenFileNames()
. After that I want to store the path of the image in a variable to display it with imread/imshow. But it only works if I use the path directly. As soon as I copy the path into a variable it does not work anymore. Can someone give me a hint ?def open_dialog_box(self): filename, _ = np.asarray(QFileDialog.getOpenFileNames(self, 'Select Multi File', 'default', 'All Files (*)')) for name in filename: #import pic pic = cv2.imread(name, cv2.IMREAD_ANYDEPTH) #This doesn't works **pic = cv2.imread("C:\\Users\\Image_name", cv2.IMREAD_ANYDEPTH)** #This works cv2.imshow("Window", pic) app = QApplication(sys.argv) w = Fenster() w.show() sys.exit(app.exec_())
The bold line works. But I don't want to enter the whole path every time. I want the path to be automatically stored in a variable and then used to display the image. Is there a method to use a variable instead of typing the whole path ?
-
@elias_hh Hi and welcome to devnet,
Since you are on Windows, I'll make an educated guess. Qt handles path in all platform using forward slashes so it makes the code easier to write cross-platform. My take is that OpenCV is not ready for that on Windows, therefore you should use QDir.toNativeSeparators before passing it to imread.
-
@elias_hh said in QFileDialog and OpenCV's imread()-Problem:
Please print out the
name
you are passing tocv2.imread(name
, which you ultimately get vianp.asarray(QFileDialog.getOpenFileNames())
, so that you & we can compare it to the whatevercv2.imread("C:\\Users\\Image_name"
you say works. Then we will know what the difference is.Which may be as @SGaist says. Or there is some other difference. My question would be take out the
np.asarray()
, try withfilename, _ = QFileDialog.getOpenFileNames(self, 'Select Multi File', 'default', 'All Files (*)')
. That returns a tuple in Python, and you arenp.asarray()
-ing that, so I'm not sure yourfilename
is what you think it is? We'll know once you print outname
. -
@Denni-0 said in QFileDialog and OpenCV's imread()-Problem:
I do not have a problem with the forward slashes in a Windows environment
From this Python examples page for cv2.imread(), it seems to be the case since the examples looks like Windows paths (D: drive) with forward slashes
img = cv2.imread('D:/image-1.png')
-
I remember reading that recent versions of Windows (well the command line / power shell / maybe another one) improved the support for that. But the OP didn't say which one he is running.
-
@elias_hh
If that's so, it looks like exactly what @SGaist suggested. If you are saying when you use a literal"C:\\Users\\Image_name"
(i.e. valueC:\Users\Image_name
) it works but when you pass the result fromQFileDialog.getOpenFileNames()
which isC:/Users/Image_name
it does not, then you have your difference/answer.cv2.imread()
, does not appear to like forward slashes under Windows (forward slashes would be correctly native under Mac/Linux), though that seems to go against the example quoted by @Pablo-J-Rogina ....In any case, try what @SGaist suggested:
nativeName = QDir.toNativeSeparators(name) pic = cv2.imread(nativeName, cv2.IMREAD_ANYDEPTH)
Does that indeed now work?
In any case, I would always use https://doc.qt.io/qt-5/qdir.html#toNativeSeparators when passing any Qt-generated path to anything non-Qt, unless the external documentation states it expects paths to be in forward-slash format.
Under Windows each program can deal with paths with
/
vs\
differently. For example, the following two OS commands differ:dir \w dir /w
The first lists a file named
w
in the root directory on the current drive (i.e.\w
). The second lists files in the current directory, using the/w
listing option. -
Hi, guys,
i apologize for the unnecessary confusion. the problem all along was not the back slash or the fordwar slash, but something completely different. I already mentioned that i get
C:/Users/Image_name
by usingprint(name)
. But apparently it has nothing to do with it. The bottom line is that the problem is the following:(Pre-information: my code is in the folder "GUI" and in "GUI" is also the folder with the images named "pics")
when i select an image from this path
C:/Users/Myname/Desktop/GUI/pics
--> it doesn work, i become forpic = cv2.imread(name, cv2.IMREAD_ANYDEPTH)
aprint(pic) = none
And here comes the weird part:
when i select the same image from another path like:
C:/Users/Myname/pics
--> it works ! But i have realy no idea why. -
Any chances that your Windows is not in English ?
-
@JonB Ok. I show you the
print(name)
. This is the path, which doesnt work:C:/Users/hrezaie/Desktop/Hamid Rezaie/GUI/pics/mocim_FPLERW_3_3CM_10mm_MPW_6.0_für_Ex_Versuch1.bmp
So, and if i choose the same image but from another path, it works. see here:
C:/Users/hrezaie/Desktop/Hamid Rezaie/pics/mocim_FPLERW_3_3CM_10mm_MPW_6.0_für_Ex_Versuch1.bmp
Again, to repeat. My Python code is in "GUI".
-
@elias_hh
Then show me from a Command Prompt :dir "C:\Users\hrezaie\Desktop\Hamid Rezaie\GUI\pics\mocim_FPLERW_3_3CM_10mm_MPW_6.0_für_Ex_Versuch1.bmp"
Please use copy & paste from what I have written. (In Command Prompt, right click should insert what you have copied to the clipboard, or click its menu in top-left-hand corner and Edit > Paste.)