from PIL import Image # 1寸照片的尺寸(单位:像素,这里以300dpi为例) width = 295 height = 413 # 打开原始图片 image = Image.open('your_image.jpg') # 计算缩放比例 image_width, image_height = image.size scale_width = width / image_width scale_height = height / image_height scale = min(scale_width, scale_height) # 计算新的尺寸 new_width = int(image_width * scale) new_height = int(image_height * scale) # 缩放图片 resized_image = image.resize((new_width, new_height), Image.ANTIALIAS) # 创建一个新的白色背景图像(尺寸为1寸) new_image = Image.new('RGB', (width, height), (255, 255, 255)) # 将缩放后的图片粘贴到新图像的中心位置 offset = ((width - new_width) // 2, (height - new_height) // 2) new_image.paste(resized_image, offset) # 保存处理后的图片 new_image.save('resized_1inch_image.jpg')
标签:scale,image,height,照片,width,转成,new,Image,图片 From: https://www.cnblogs.com/air/p/18534707