用python把图片改为同一尺寸
要将图片改为同一尺寸,可以使用Pillow库来实现。下面是一个示例代码,将图片统一改为宽度为500像素,高度按比例缩放。
from PIL import Image
def resize_image(image_path, width):
img = Image.open(image_path)
w_percent = (width / float(img.size[0]))
h_size = int((float(img.size[1]) * float(w_percent)))
img = img.resize((width, h_size), Image.ANTIALIAS)
return img
# 输入图片路径
image_path = "input.jpg"
# 设置目标宽度
target_width = 500
# 调用函数进行图片尺寸调整
resized_image = resize_image(image_path, target_width)
# 保存调整后的图片
resized_image.save("output.jpg")
在上述代码中,resize_image函数接受两个参数:图片路径和目标宽度。函数中通过计算目标宽度与原始宽度的比例,来确定目标高度。然后使用resize方法来进行尺寸调整,最后保存调整后的图片
原文地址: https://www.cveoy.top/t/topic/h6PX 著作权归作者所有。请勿转载和采集!