QFileDialog and OpenCV's imread()-Problem
-
@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.
-
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.
-
@JonB If i do
print(name)
then i get the following path like:C:/Users/Image_name
. I think the problem is "/". Windows or OpenCV cant handle path with "/". If i use the same code with my macbook, it works fine.@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 ?
-
@elias_hh
Show the actual paths printed out via theprint(name)
, copied & pasted to here not typed in, of what does work and what does not.@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".
-
@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.)