1

I have a directory containing 4 folder (1,2,3,4). Each folder has jpg images in them. I used the code below to read the images. The problem is all images are in different shapes. So, now I have a list of images each with different shape.

1) Is there a better way to read img files from a directory? (maybe assign directly to a numpy array)

2) How can I resize the images so that they all have the same shape?

Thanks!

import imageio
import os.path

images = []

for folder in os.listdir('images'):
    for filename in os.listdir('images/'+folder):
        if filename.endswith(".jpg"): 
            img = imageio.imread('images/'+folder+'/'+filename)
            img.reshape((1,img.flatten().shape[0])).shape
            images.append(img)
Patrick Artner
  • 50,409
  • 9
  • 43
  • 69
Abid Orucov
  • 93
  • 1
  • 4
  • tags are used to filter your question. tagging with tags that have 0 watchers ([tag:imread]) does nothing for you. The other tags I removed are not reflecting what happens in your code and questions - only use what signigfies your question - if you use it for ML fine - if it doenst use MLstuff, dont tag as such. Thanks. – Patrick Artner Nov 03 '18 at 14:02
  • Please clarify what you mean by "resize" - there are several ways you can do that: simply crop all to the lowest size - add some border to all with color X so they are all of the max size, ... resize all images (if they are of fixed ration) to a certain size and then load all of them , ... thats bread and butter for image processing - you can f.e. use PIL, cv2, ... also add what you tried and researched in that direction. There are tons on Q and A on that on this very site: [here is one](https://stackoverflow.com/questions/37631611/python-how-to-resize-an-image-using-pil-module) – Patrick Artner Nov 03 '18 at 14:05
  • thanks, regarding the resize, I was actually looking for ideas similar to what you mentioned. resize all images (if they are of fixed ration) to a certain size and then load all of them --> this is what i need. – Abid Orucov Nov 03 '18 at 14:36

0 Answers0