【问题标题】:How to resize an image now that scipy.misc.imresize has been removed from scipy?既然 scipy.misc.imresize 已从 scipy 中删除,如何调整图像大小?
【发布时间】:2020-09-11 08:25:30
【问题描述】:

我想使用一个使用scipy.misc.imresize 的旧脚本。 但不幸的是,它完全从 scipy 中删除。 我不明白它做了什么,所以什么代码将执行与上述行相同的操作。

我尝试使用skimage.transform.resize(image, (num_px*num_px*3, 1), order = 3) 但得到错误 - ValueError: cannot reshape array of size 12288 into shape (1200,1)

编辑:更多信息

它将图片分类为属于两组之一。

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)

【问题讨论】:

标签: python python-3.x numpy scipy scikit-image


【解决方案1】:

您可以使用 pillow 执行相同的操作,而不是使用已弃用和删除的 scipy 图像例程 imread()imresize(),因为 @987654325 需要它@函数工作。

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)

【讨论】:

    【解决方案2】:
    from PIL import Image
    import numpy as np
    
    my_image = "anyimage.jpg" #any image form the internet
    fname = "images/" + my_image
    
    #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)
    

    【讨论】:

    • 只需从上述解决方案中删除 num_px = 20
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-05
    • 2021-11-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多