1

I have the follow method to get all the images from a dir:

def ReadImages(Path):
    LabelList = list()
    ImageCV = list()
    classes = ["nonPdr", "pdr"]
    width = 605
    height = 700
    dim = (width, height)


    # Get all subdirectories
    FolderList = os.listdir(Path)

    # Loop over each directory
    for File in FolderList:
        if(os.path.isdir(os.path.join(Path, File))):
            for Image in os.listdir(os.path.join(Path, File)):
                # Convert the path into a file
                ImageCV.append(cv2.imread(os.path.join(Path, File) + os.path.sep + Image))
                # Add a label for each image and remove the file extension
                LabelList.append(classes.index(os.path.splitext(File)[0]))
        else:
            ImageCV.append(cv2.imread(os.path.join(Path, File) + os.path.sep + Image))    
            # Add a label for each image and remove the file extension
            LabelList.append(classes.index(os.path.splitext(File)[0]))
    return ImageCV, LabelList

But my images are bigger and a want to reduce its wxh to 605x700, then I tried to do something like This:

imgR = cv2.resize(ImageCV[Image])

And it not works.. What can I do to resize all these images? I appreciate any help.

3
  • Are all the images that you read of the same size? Commented Sep 2, 2019 at 18:18
  • there are 22 imgs (605x700) and 49 imgs (4288x2848) Commented Sep 2, 2019 at 18:19
  • It always helps to read the documentation for a method such as cv2.resize(). See the arguments needed. See tutorialkart.com/opencv/python/opencv-python-resize-image, for example. Commented Sep 2, 2019 at 19:03

1 Answer 1

1

You may have to pass in the shape to resize() function of cv2.

This will resize the image to have 605 cols (width) and 700 rows (height):

imgR = cv2.resize(ImageCV[Image], (605, 700)) 

For more options you can read the documentation for cv2.resize.

Also, I would suggest using python coding conventions like imgR -> img_r, Image -> image, ImageCV -> image_cv etc. Hope this helps.

Sign up to request clarification or add additional context in comments.

4 Comments

TypeError: list indices must be integers or slices, not str
It's something to do with your for loop. Try for index, value in enumerate(list): I can help if you post the code for it.
Must I delete this : for Image in os.listdir(os.path.join(Path, File)):?
I'm not sure as to where you are adding resize() to your code. But if its in that loop change it to for index, Image in enumerate(os.listdir(os.path.join(Path, File))): . Now, you could access list items by ImageCV[index].

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.