用python写一段代码,筛选出文件夹中图像尺寸满足图像长与图像宽的比值在(0.5,2)之间的图像,将满足以上要求的图像复制到另一文件夹中
时间: 2023-03-13 07:25:49 浏览: 52
import os
import shutilsrc_dir = '原文件夹'
dst_dir = '目标文件夹'# 判断比例
def check_ratio(width, height):
ratio = width / height
if ratio > 0.5 and ratio < 2:
return True
else:
return Falsefor root, dirs, files in os.walk(src_dir):
for file in files:
# 判断文件是否为图片
if file.endswith('.jpg') or file.endswith('.png'):
img_path = os.path.join(root, file)
# 获取图片尺寸
w, h = Image.open(img_path).size
if check_ratio(w, h):
# 符合要求的图片复制到指定文件夹
shutil.copy(img_path, dst_dir)