编写一个脚本来复制特定尺寸的图片
创始人
2024-12-06 23:01:39
0

下面是一个使用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变量设置为你想要复制的图片的目标尺寸。然后,运行脚本,它将遍历源目录中的所有文件,查找指定尺寸的图片,并将它们复制到目标目录中。

相关内容

热门资讯

安装apache-beam==... 出现此错误可能是因为用户的Python版本太低,而apache-beam==2.34.0需要更高的P...
避免在粘贴双引号时向VS 20... 在粘贴双引号时向VS 2022添加反斜杠的问题通常是由于编辑器的自动转义功能引起的。为了避免这个问题...
Android Recycle... 要在Android RecyclerView中实现滑动卡片效果,可以按照以下步骤进行操作:首先,在项...
omi系统和安卓系统哪个好,揭... OMI系统和安卓系统哪个好?这个问题就像是在问“苹果和橘子哪个更甜”,每个人都有自己的答案。今天,我...
原生ios和安卓系统,原生对比... 亲爱的读者们,你是否曾好奇过,为什么你的iPhone和安卓手机在操作体验上有着天壤之别?今天,就让我...
Android - 无法确定任... 这个错误通常发生在Android项目中,表示编译Debug版本的Java代码时出现了依赖关系问题。下...
Android - NDK 预... 在Android NDK的构建过程中,LOCAL_SRC_FILES只能包含一个项目。如果需要在ND...
Akka生成Actor问题 在Akka框架中,可以使用ActorSystem对象生成Actor。但是,当我们在Actor类中尝试...
Agora-RTC-React... 出现这个错误原因是因为在 React 组件中使用,import AgoraRTC from “ago...
Alertmanager在pr... 首先,在Prometheus配置文件中,确保Alertmanager URL已正确配置。例如:ale...