Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Qt for Python
  4. QFileDialog and OpenCV's imread()-Problem
Forum Updated to NodeBB v4.3 + New Features

QFileDialog and OpenCV's imread()-Problem

Scheduled Pinned Locked Moved Unsolved Qt for Python
14 Posts 5 Posters 2.0k Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • E elias_hh

    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 ?

    JonBJ Offline
    JonBJ Offline
    JonB
    wrote on last edited by JonB
    #3

    @elias_hh said in QFileDialog and OpenCV's imread()-Problem:

    Please print out the name you are passing to cv2.imread(name, which you ultimately get via np.asarray(QFileDialog.getOpenFileNames()), so that you & we can compare it to the whatever cv2.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 with filename, _ = QFileDialog.getOpenFileNames(self, 'Select Multi File', 'default', 'All Files (*)'). That returns a tuple in Python, and you are np.asarray()-ing that, so I'm not sure your filename is what you think it is? We'll know once you print out name.

    1 Reply Last reply
    0
    • Pablo J. RoginaP Offline
      Pablo J. RoginaP Offline
      Pablo J. Rogina
      wrote on last edited by
      #4

      @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')
      

      Upvote the answer(s) that helped you solve the issue
      Use "Topic Tools" button to mark your post as Solved
      Add screenshots via postimage.org
      Don't ask support requests via chat/PM. Please use the forum so others can benefit from the solution in the future

      1 Reply Last reply
      0
      • SGaistS Offline
        SGaistS Offline
        SGaist
        Lifetime Qt Champion
        wrote on last edited by
        #5

        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.

        Interested in AI ? www.idiap.ch
        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

        JonBJ 1 Reply Last reply
        0
        • SGaistS SGaist

          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.

          JonBJ Offline
          JonBJ Offline
          JonB
          wrote on last edited by
          #6

          @SGaist
          Once the OP just puts in print(name) we won't have to guess any more....

          E 1 Reply Last reply
          0
          • JonBJ JonB

            @SGaist
            Once the OP just puts in print(name) we won't have to guess any more....

            E Offline
            E Offline
            elias_hh
            wrote on last edited by
            #7

            @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.

            JonBJ 1 Reply Last reply
            0
            • E elias_hh

              @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.

              JonBJ Offline
              JonBJ Offline
              JonB
              wrote on last edited by JonB
              #8

              @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. value C:\Users\Image_name) it works but when you pass the result from QFileDialog.getOpenFileNames() which is C:/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.

              1 Reply Last reply
              1
              • E Offline
                E Offline
                elias_hh
                wrote on last edited by elias_hh
                #9

                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 using print(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 for pic = cv2.imread(name, cv2.IMREAD_ANYDEPTH) a print(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.

                1 Reply Last reply
                0
                • SGaistS Offline
                  SGaistS Offline
                  SGaist
                  Lifetime Qt Champion
                  wrote on last edited by
                  #10

                  Any chances that your Windows is not in English ?

                  Interested in AI ? www.idiap.ch
                  Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                  E 1 Reply Last reply
                  0
                  • SGaistS SGaist

                    Any chances that your Windows is not in English ?

                    E Offline
                    E Offline
                    elias_hh
                    wrote on last edited by
                    #11

                    @SGaist i dont thing so

                    JonBJ 1 Reply Last reply
                    0
                    • E elias_hh

                      @SGaist i dont thing so

                      JonBJ Offline
                      JonBJ Offline
                      JonB
                      wrote on last edited by JonB
                      #12

                      @elias_hh
                      Show the actual paths printed out via the print(name), copied & pasted to here not typed in, of what does work and what does not.

                      E 1 Reply Last reply
                      0
                      • JonBJ JonB

                        @elias_hh
                        Show the actual paths printed out via the print(name), copied & pasted to here not typed in, of what does work and what does not.

                        E Offline
                        E Offline
                        elias_hh
                        wrote on last edited by
                        #13

                        @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".

                        JonBJ 1 Reply Last reply
                        0
                        • E elias_hh

                          @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".

                          JonBJ Offline
                          JonBJ Offline
                          JonB
                          wrote on last edited by JonB
                          #14

                          @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.)

                          1 Reply Last reply
                          0

                          • Login

                          • Login or register to search.
                          • First post
                            Last post
                          0
                          • Categories
                          • Recent
                          • Tags
                          • Popular
                          • Users
                          • Groups
                          • Search
                          • Get Qt Extensions
                          • Unsolved