Search code examples
pythonpython-3.xnumpyscipyscikit-image

How to resize an image now that scipy.misc.imresize has been removed from scipy?


I want to use an old script which uses scipy.misc.imresize. But unfortunately it is removed entirely from scipy. I do not understand what it does so what code will perform the same action as the above line.

I tried using skimage.transform.resize(image, (num_px*num_px*3, 1), order = 3) But get the error - ValueError: cannot reshape array of size 12288 into shape (1200,1)

Edit: More Information

It's classifying pictures as belonging to one of two sets.

my_image = "anyimage.jpg" #any image form the internet

#Preprocessing the image
fname = "images/" + my_image
image = np.array(ndimage.imread(fname, flatten=False))
image = image/255.
my_image = scipy.misc.imresize(image, size=(num_px,num_px)).reshape((1, num_px*num_px*3)).T #The original code which worked perfectly

my_predicted_image = predict(d["w"], d["b"], my_image) #predict function uses 
#linear regression where the third parameter must be data of 
#size (num_px * num_px * 3, number of examples)

Solution

  • Instead of using the scipy image routines imread() and imresize() which have been deprecated and removed, you can do the same thing using pillow, which you should already have since it is required for the scipy functions to work.

    from PIL import Image
    import numpy as np
    
    my_image = "anyimage.jpg" #any image form the internet
    fname = "images/" + my_image
    
    num_px = 20  # a guess based on the shape of (1200, 1) in your error message
    
    #Preprocessing the image
    image = Image.open(fname).resize(size=(num_px, num_px))  # use PIL to open and reshape image
    my_image = np.array(image, dtype=float) / 255  # convert to numpy array and scale values
    my_image = my_image.reshape((1, num_px*num_px*3)).T  # reshape and transpose
    
    my_predicted_image = predict(d["w"], d["b"], my_image) #predict function uses 
    #linear regression where the third parameter must be data of 
    #size (num_px * num_px * 3, number of examples)