Why I can't read all the images that are in tools_list

Question:

I’ve created a list which is called tools_list. That list contain all the existing .png photos from tools directory. The problem that I have is regarding to images_list.In images_list I want to store all the images and at the same time apply cv.imread to them. When I tried to run my I got

  • check file path/integrity
  • TypeError: int() argument must be a string, a bytes-like object or a number, not ‘NoneType’
mp_hands = mp.solutions.hands

tools_list = [elem for elem in os.listdir("tools")]
print(tools_list)
images_list = [cv.imread(img) for img in tools_list]
print(images_list)

mode = "selection mode"
brush_thickness = 15

mp_drawing = mp.solutions.drawing_utils
mp_hands = mp.solutions.hands
xp,yp = 0,0

capture = cv.VideoCapture(0)
black = (0,0,0)
white = (255,255,255)
blue = (255,0,0)
red = (0,0,255)
green = (0,255,0)
yellow = (0,255,255)
brown = (0, 75, 150)
image_number = 0
color = black
image_canvas = np.zeros((720, 1280, 3), np.uint8)
with mp_hands.Hands(min_detection_confidence = 0.8, min_tracking_confidence = 0.5, max_num_hands = 2) as hands:
    while True:
        flag, frame = capture.read()
        frame = cv.resize(frame, (1280, 720))
        frame[0:125, 0:1280] = images_list[image_number]

Answers:

Judging by your error, your file structure look like this:

  • tools
    • image1.png
    • image2.png
  • `the_script.py

The method os.listdir('tools') returns only the content of tools. E.g

  • image1.png
  • image2.png

It will not return toolsimage1.png which is what you need to read in the cv2.imread() method. Without proper path, cv2.imread() returns None hence the error TypeError: int() argument must be a string, a bytes-like object or a number, not 'NoneType

You need to replace the line

[cv.imread(img) for img in tools_list]

with

[cv.imread(os.path.join("tools",img)) for img in tools_list]
Answered By: JaK
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.