代码里面给httpp3itccnq_70images0320230112ef49053546cb44e5b849e145efa65480jpeg图片限制大小
要限制图片的大小,可以使用以下代码:
import requests
from PIL import Image
url = 'http://p3.itc.cn/q_70/images03/20230112/ef49053546cb44e5b849e145efa65480.jpeg'
# 下载图片
response = requests.get(url)
img = Image.open(BytesIO(response.content))
# 设置限制的大小
max_width = 500
max_height = 500
# 检查图片大小是否超过限制
if img.width > max_width or img.height > max_height:
# 调整图片大小
img.thumbnail((max_width, max_height))
# 保存调整后的图片
img.save('resized_image.jpeg')
这段代码使用了requests库来下载图片,并使用PIL库来操作图片。首先,通过requests.get方法下载图片,并使用Image.open方法打开图片。然后,设置了限制的宽度max_width和高度max_height。接着,通过比较图片的宽度和高度与限制的宽度和高度,判断是否需要调整图片的大小。如果图片的宽度或高度超过了限制,就使用thumbnail方法来调整图片的大小,然后使用save方法保存调整后的图片。
原文地址: https://www.cveoy.top/t/topic/i8M2 著作权归作者所有。请勿转载和采集!