Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using python load images from directory and reshape [closed]

I want to load same images from directory and reshape them using reshape function using python.

How can i do this?

like image 643
verdery Avatar asked May 21 '26 23:05

verdery


1 Answers

Assuming that you have scipy installed and assuming that with "reshape" you actually mean "resize", the following code should load all images from the directory /foo/bar, resize them to 64x64 and add them to the list images:

import os
from scipy import ndimage, misc

images = []
for root, dirnames, filenames in os.walk("/foo/bar"):
    for filename in filenames:
        if re.search("\.(jpg|jpeg|png|bmp|tiff)$", filename):
            filepath = os.path.join(root, filename)
            image = ndimage.imread(filepath, mode="RGB")
            image_resized = misc.imresize(image, (64, 64))
            images.append(image_resized)

If you need a numpy array (to call reshape) then just add images = np.array(images) at the end (with import numpy as np at the start).

like image 197
aleju Avatar answered May 23 '26 14:05

aleju