python 读取图片成ndarray,resize,显示并回传

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python 读取图片成ndarray,resize,显示并回传相关的知识,希望对你有一定的参考价值。

# install dependencies by conda
conda install -c conda-forge imageio
conda install scikit-image


from imageio import imread
from skimage.transform import resize

img = imread(file_path)
img_resized = resize(img, (128, 128))
import matplotlib.pyplot as plt
from scipy.misc import imread, imsave, imresize
%matplotlib inline

def read_image_and_resize(path, size=(128, 128), verbose=False):
    """
    Read a image file as a numpy.ndarray, resize it and return the resized images.
    """
    img = imread(path)
    
    img_resized = imresize(img, size)
    if verbose:
        print('Image resized from {} to {}'.format(img.shape, img_resized.shape))
        plt.figure()
        plt.subplot(1, 2, 1);plt.imshow(img)
        plt.subplot(1, 2, 2);plt.imshow(img_resized)
    
    return img_resized
    
# invoke example
img = read_image_and_resize('datasets/train/cat.0.jpg', verbose=True)

以上是关于python 读取图片成ndarray,resize,显示并回传的主要内容,如果未能解决你的问题,请参考以下文章

python的imreadnewaxis

cv2.resize的坑

cv2.resize的坑

如何将 python ndarray 转换为 c++ char*?

Series中每条存储的数据格式为ndarray,怎样将Series也转换成ndarray的格式

Python之cv2