Value Error: Too many dimensions: 3 > 2
Question: I've tried to resize image with scipy and everything seems to work fine until
I try to save the image. When I try to save image I get error that you can see
in title. Full traceback is available below.

    
    
    import numpy as np
    import scipy.misc
    from PIL import Image
    
    image_path = "img0.jpg"
    
    
    def load_image(img_path):
        img = Image.open(img_path)
        img.load()
        data = np.asarray(img, dtype="int32")
        return data
    
    
    def save_image(npdata, outfilename):
        img = Image.fromarray(np.asarray(np.clip(npdata, 0, 255), dtype="uint8"), "L")
        img.save(outfilename)
    
    array_image = load_image(image_path)
    
    array_resized_image = scipy.misc.imresize(array_image, (320, 240), interp='nearest', mode=None)
    
    save_image(array_resized_image, "i1.jpg")
    

Full traceback of the error:

    
    
    Traceback (most recent call last):
      File "D:/Python/Playground/resize image with scipy.py", line 26, in <module>
        save_image(array_resized_image, "i1.jpg")
      File "D:/Python/Playground/resize image with scipy.py", line 16, in save_image
        img = Image.fromarray(np.asarray(np.clip(npdata, 0, 255), dtype="uint8"), "L")
      File "C:\Anaconda2\lib\site-packages\PIL\Image.py", line 2154, in fromarray
        raise ValueError("Too many dimensions: %d > %d." % (ndim, ndmax))
    ValueError: Too many dimensions: 3 > 2.
    


Answer: don't you need to convert it to a two dimensional array before doing the
fromarray(... 'L')?

You can do that using a scipy function or, actually quicker, to multiply the
RGB by factors. Like this

    
    
    npdata = (npdata[:,:,:3] * [0.2989, 0.5870, 0.1140]).sum(axis=2)
    

