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