python 压缩图片为指定大小

转自:scipy笔记—scipy.misc.imresize用法(方便训练图像数据)

from skimage.transform import resize  # 压缩图片
def predict_my_image(image_name, y, parameters=parameters):
    image = plt.imread(r'..\data\L1w4_data\%s.png'%image_name)
    
    # 重塑形状 1.转为64*64*3; 2.转为12288*1一个样本
    my_image = resize(image, output_shape=(num_px, num_px))
    # 将图片压缩为指定尺寸
    my_image = my_image.reshape((y, num_px * num_px * 3)).T
    
    my_image_pre = predict(my_image, 1, parameters).squeeze()
    cat_dic = {'0':'no-cat', '1':'cat'}
    plt.title(f"it's {cat_dic[str(int(my_image_pre))]}")
    plt.imshow(image)
import matplotlib.pyplot as plt
image = plt.imread('..\data\L1w2_data\手写猫猫1.jpg')
plt.imshow(image)

python 压缩图片为指定大小_第1张图片

from skimage.transform import resize  # 压缩图片

# 将图片压缩为指定尺寸
num_px = 64
my_image = resize(image, output_shape=(num_px, num_px))
plt.imshow(my_image)
my_image = my_image.reshape((1, num_px * num_px * 3)).T
my_image.shape

python 压缩图片为指定大小_第2张图片

你可能感兴趣的:(图片处理,python)