0

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

Christoph Rackwitz
  • 11,317
  • 4
  • 27
  • 36
Serg
  • 151
  • 10
  • 1
    Think u need to join your Filename with the full path: ./input/data_1/001/path_100.png – Christoph Jun 29 '23 at 19:50
  • 1
    `image_subfolder_path[0]` is `100.png`, but it looks like it needs to be `./input/data_1/001/100.png`. i.e. you need to prepend `image_folder_path` to the values in `image_subfolder_path`. – slothrop Jun 29 '23 at 19:50
  • I'm new in Python, so, haw I can prepend/join image_folder_path to the values in image_subfolder_path? – Serg Jun 29 '23 at 20:16
  • One way: `image_subfolder_path=[os.path.join(image_folder_path, p) for p in os.listdir(image_folder_path)]`. For a few other options see: https://stackoverflow.com/questions/25657705/make-os-listdir-list-complete-paths – slothrop Jun 29 '23 at 20:42

2 Answers2

1

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

Twenkid
  • 825
  • 7
  • 15
  • 1
    "full" sounds like "absolute". an absolute path is not required. a *correct* path is required. correct paths can be absolute or relative. "just the file name" may be a correct path, if there's a file of that name in the current working directory. if there isn't, it's simply an invalid path. in this case, the directory containing that file needs to be part of the path specifying that file. – Christoph Rackwitz Jun 29 '23 at 23:00
  • Yes, right, with "full" I mean including the "folder" path as in the problem and the segmentation of the path that is given there. – Twenkid Jun 29 '23 at 23:11
  • 1
    @ChristophRackwitz getting a "correct" path is surprisingly difficult. I always recommend using absolute paths just to eliminate a possible source of errors. – Mark Ransom Jun 29 '23 at 23:12
0

Solved by add the following:

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

Thanks everybody for useful comments.

Serg
  • 151
  • 10