gpt-4o-mini-2024-07-18· OpenAI
WildChat-4.8M (AI2)
给你一个路径下面有很多jpg和txt两者一一对应,判断一下如果图片的宽和高都大于336,则保留,否则删除该图片和对应的txt
- User
给你一个路径下面有很多jpg和txt两者一一对应,判断一下如果图片的宽和高都大于336,则保留,否则删除该图片和对应的txt
- Assistant
要实现这个功能,您可以使用Python和一些常见的库(如
os和PIL)来遍历目录,检查每个图像的尺寸并决定是否保留该图像及其对应的文本文件。以下是一个示例代码,您可以根据需要进行修改:import os from PIL import Image def clean_images_and_texts(directory): # 遍历该目录下的所有文件 for filename in os.listdir(directory): if filename.endswith('.jpg'): # 构造图片的完整路径 img_path = os.path.join(directory, filename) # 读取图片尺寸 with Image.open(img_path) as img: width, height = img.size # 检查宽高 if width <= 336 or height <= 336: # 如果宽或高小于或等于336,删除图片和对应的txt文件 os.remove(img_path) # 删除对应的txt文件 txt_filename = filename.replace('.jpg', '.txt') txt_path = os.path.join(directory, txt_filename) if os.path.exists(txt_path): os.remove(txt_path) print(f"Deleted: {img_path} and {txt_path}") # 输入您要处理的目录路径 directory_path = 'your_directory_path_here' clean_images_and_texts(directory_path)