# -*- coding:utf8 -*-
path = r'd:\Users\1-PC\Desktop\a.jpg'
savedpath = r'd:\Users\1-PC\Desktop\b.jpg'
from PIL import Image
import numpy as np
import matplotlib.pyplot as plt
#设置生成的参数
file_in = path
width = 358
height = 441
file_out = savedpath
image = Image.open(file_in) #打开文件
img = np.array(image) #将图片转为数组
print(img.shape) #展示图片像素
# rows, cols, dims = img.shape
# image.show()
resized_image = image.resize((width, height), Image.ANTIALIAS) #将图片缩放后已二维数组保存
resized_image.save(file_out)#导出
plt.figure("dog") # 保存图片
plt.imshow(resized_image)
plt.axis('off')
plt.show()
#裁剪图片
# box = image.copy()
box = (0, 0, 500, 100)
region = image.crop(box)
region.save(savedpath, "JPEG")
plt.figure("love")
plt.imshow(region, cmap='gray')
plt.axis('off')
plt.show()
python 实现图片的缩放,裁剪
最新推荐文章于 2024-08-15 01:55:01 发布