AttributeError: module 'scipy.misc' has no attribute 'toimage'

后端 未结 5 821
失恋的感觉
失恋的感觉 2021-01-05 06:47

While executing the below code:

scipy.misc.toimage(output * 255, high=255, low=0, cmin=0, cmax=255).save(
    params.result_dir + \'final/%5d_00_%d_out.png\'         


        
5条回答
  •  心在旅途
    2021-01-05 06:58

    @Martijn Pieters worked for me but I also found another solution that may suit some people better. You can also use the code below that imports keras.preprocessing.image, array_to_img instead of scipy.misc.toimage which was deprecated in Scipy 1.0.0 as @Martijn Pieters has already mentioned.

    So as an example of using keras API to handle converting images:

    # example of converting an image with the Keras API
    from keras.preprocessing.image import load_img
    from keras.preprocessing.image import img_to_array
    from keras.preprocessing.image import array_to_img
    
    # load the image
    img = load_img('image.jpg')
    print(type(img))
    
    # convert to numpy array
    img_array = img_to_array(img)
    print(img_array.dtype)
    print(img_array.shape)
    
    # convert back to image
    img_pil = array_to_img(img_array)
    print(type(img_pil))
    
    # show image
    fig = plt.figure()
    ax = fig.add_subplot()
    ax.imshow(img_pil)
    

    and to save an image with keras:

    from keras.preprocessing.image import save_img
    from keras.preprocessing.image import load_img
    from keras.preprocessing.image import img_to_array
    
    # load image
    img = load_img('image.jpg')
    
    # convert image to a numpy array
    img_array = img_to_array(img)
    
    # save the image with a new filename
    save_img('image_save.jpg', img_array)
    
    # load the image to confirm it was saved correctly
    img = load_img('image_save.jpg')
    
    print(type(img))
    print(img.format)
    print(img.mode)
    print(img.size)
    

提交回复
热议问题