Wen28
Wen28

Reputation: 31

How to load multiple image in openCV one by one?

I have a question on about OpenCV when using imread() I had successfully parse my path into a list When running the function it will show as below TypeError: Can't convert object to 'str' for 'filename'

here is my code that show me the above error *some code i have to cover up due to privacy

import cv2
import numpy as np
from pytesseract import pytesseract
import glob

def parse_folder(path):
    images = glob.glob(f'{path}/*.jpg') + glob.glob(f'{path}/*.png')
    return images
path = "D:\\Path\\to\\100+images"
images = parse_folder(path)

def image_to_text(image_path):
    # Opening the image & storing it in an image object
    img = cv2.imread(image_path)
    *the rest of the code*

arr_data = image_to_text(images)

parsing the whole folder path is fine

i'm able to see all the images in the list Is there something that i'm not familiar with the imread() method ?

Upvotes: 1

Views: 821

Answers (2)

Sai_MSP
Sai_MSP

Reputation: 154

all_images = [cv2.imread(each_file) for each_file in list_of_image_paths]

Use these collected images(numpy arrays) and process one by one.

Please refer cv2.imread

Upvotes: 1

Nawal
Nawal

Reputation: 336

cv2.imread loads a single file, not a list of files.

you can use a for loop to load images.

def image_to_text(image_path):
    # Opening the image & storing it in an image object
    for file in image_path:
        img = cv2.imread(file)
        *do something with the single image*

You can load all images at once and store in a list, however it might not be a good idea if #images is large.

Upvotes: 0

Related Questions