下面是一个使用Python编写的脚本示例,用于复制特定尺寸的图片:
import os
from PIL import Image
def copy_images(source_dir, dest_dir, target_size):
# 遍历源目录中的所有文件
for filename in os.listdir(source_dir):
# 拼接源文件的完整路径
source_path = os.path.join(source_dir, filename)
# 检查文件是否为图片
if os.path.isfile(source_path) and any(filename.lower().endswith(ext) for ext in ['.jpg', '.jpeg', '.png']):
# 打开图片文件
image = Image.open(source_path)
# 检查图片尺寸是否满足要求
if image.size == target_size:
# 拷贝图片到目标目录
dest_path = os.path.join(dest_dir, filename)
image.save(dest_path)
print(f"复制图片:{filename}")
# 源目录
source_directory = '/path/to/source/directory'
# 目标目录
destination_directory = '/path/to/destination/directory'
# 目标尺寸
target_image_size = (800, 600)
# 调用函数复制图片
copy_images(source_directory, destination_directory, target_image_size)
请确保在运行脚本之前安装了Pillow库(pip install pillow
)以便处理图像。
在脚本中,你需要将source_directory
变量设置为你想要复制图片的源目录的路径,将destination_directory
变量设置为目标目录的路径,并将target_image_size
变量设置为你想要复制的图片的目标尺寸。然后,运行脚本,它将遍历源目录中的所有文件,查找指定尺寸的图片,并将它们复制到目标目录中。