Why can't I read these files? multiple libraries couldn't find them

73 Views Asked by At

Unsuccessfully trying to solve a seemingly simple problem: get dimensions of the image located in a folder using cv2.imread. The folders structure as follow:

Main Folder
    -- ...
    -- input
         -- Data1
               -- 001
                    100.png
                    101.png
                    102.png   
      -- VP_code
            -- test.py

Run from within "Main Foder" :

python VP_code/test.py --input_video_url ./input

from "test.py" following codes:

## Detect original image dimention

input_clip_url = os.path.join(opts.input_video_url, test_clip_par_folder, clip_name)


image_folder_path=input_clip_url        
image_subfolder_path=os.listdir(image_folder_path)
image_path=cv2.imread(image_subfolder_path[0])





print ("Folder path_"+str(image_folder_path))
print ("Image subfolder path_"+str(image_subfolder_path)) 
print ("Image path_"+str(image_subfolder_path[0])) 
print ("Image read_"+str(image_path))

    ## End of detect original image dimention 

As result it properly detects everything but "imread" :

Folder path_./input/data_1/001
Image subfolder path_['100.png','101.png','102.png']
Image path_100.png
Image read_None

I tried image.open (PIL) - no luck. So,it looks like there is a problem with "path" but I couldn't manage to resolve it. My great appreciation for any suggestions.from test,py

2

There are 2 best solutions below

3
Twenkid On

As slothrop comments, cv2.imread needs the full path [or just "correct" part as Christoph commented], not only the file name: if you call the script from the local directory of the image or if you change the current working directory, then it should work with "100.png" etc.

Yes, the parameter in the function is "filename" but it means a full path [or "correct" path], with "full" I mean including the folder component from your segmentation: https://docs.opencv.org/3.4/d4/da8/group__imgcodecs.html#ga288b8b3da0892bd651fce07b3bbd3a56

(Did you try it with code that opens the images with the full path for a sample image, e.g. print(cv2.imread("c:\\mainfolder\\input\\Data1\\001\\100.png").shape) etc.

For the dimensions of the first file:

fullpath = os.path.join(image_folder_path, image_path)
image_path=cv2.imread(fullpath)
dim = cv2.imread(fullpath).shape

or:

dim = cv2.imread(os.path.join(image_folder_path, image_path)).shape

Also you can use glob.glob and call it with the image_folder_path and a wildcard, "*.png", it will collect a list of full paths for the selected files.

import glob
import os   
files = glob.glob(os.path.join(image_folder_path,'*.png')) #or other extension or wildcard
dimensions = cv2.imread(files[0]).shape

Sample glob.glob result:
>>> files
['C:\\some\\pics\\3_n.jpg', 'C:\\some\\pics\\4_n.jpg', '... ]

https://docs.python.org/3/library/glob.html

0
Serg On

Solved by add the following:

full_path=os.path.join(image_folder_path, image_subfolder_path[0])

Thanks everybody for useful comments.