0

Hi: I would like load some 2400 images from a folder into python 3.6 for Neural Networks, the following code works, however, it loads images in its original size, which is (2443, 320, 400, 3), after converting into an array. How do I resize it to 64x96? so it is (2443, 64, 96, 3) and less of a load on memory. Separately, how do I do it using parallel processing, so it is efficient.

Thank you!

IMAGE_PATH = 'drive/xyz/data/'
file_paths = glob.glob(path.join(IMAGE_PATH, '*.gif'))

# Load the images
images = [misc.imread(path) for path in file_paths]
images = np.asarray(images)

Inspired by this link, I tried to do the following:

from PIL import Image

basewidth = 96
IMAGE_PATH = 'drive/xyz/data/' 
file_paths = glob.glob(path.join(IMAGE_PATH, '*.gif'))

# Load the images img = [misc.imread(path) for path in file_paths]

wpercent = (basewidth/float(img.size[0])) 
hsize = int((float(img.size[1])*float(wpercent))) 
img = img.resize((basewidth,hsize), Image.ANTIALIAS) 
images = np.asarray(img)

however, it resulted in an error, written below. Any suggestions, would be greatly appreciated. Thank you.

AttributeError                            Traceback (most recent call last)
<ipython-input-7-56ac1d841c56> in <module>()
      9 img = [misc.imread(path) for path in file_paths]
     10 
---> 11 wpercent = (basewidth/float(img.size[0]))
     12 hsize = int((float(img.size[1])*float(wpercent)))
     13 img = img.resize((basewidth,hsize), Image.ANTIALIAS)

AttributeError: 'list' object has no attribute 'size'
SM_
  • 41
  • 6

1 Answers1

0

First you may want to resize your images then you can use your same method to initialize the arrays given the output image folder.

Resizing the image

This uses PIL package for resizing images but any library should do the same as long as it provides a resize method.

You can read further down discussions from here How do I resize an image using PIL and maintain its aspect ratio?

import os, sys
import Image

size = 128, 128

for infile in sys.argv[1:]:
    outfile = os.path.splitext(infile)[0] + ".gif"
    if infile != outfile:
        try:
            im = Image.open(infile)
            im.thumbnail(size, Image.ANTIALIAS)
            im.save(outfile, "GIF")
        except IOError:
            print "cannot create thumbnail for '%s'" % infile

Parallelism Example

Regarding parallelism you can use this example as a base and move on from there.

This is from the python docs https://docs.python.org/3/library/multiprocessing.html

from multiprocessing import Process

    def f(name):
        print('hello', name)

    if __name__ == '__main__':
        p = Process(target=f, args=('bob',))
        p.start()
        p.join()
Zeromika
  • 347
  • 2
  • 12
  • Hi @UmutGüler, thanks. Where do I specify the path? filename := "dir/to/myfile/somefile.gif" – SM_ Sep 15 '18 at 03:05