把机器人照片全部删除
import glob
from PIL import Image
import pytesseract
def delete_robot_photos(directory):
删除指定目录中所有包含'机器人'文本的图片文件
:param directory: 要扫描的目录路径
支持的图片格式
image_extensions = ['.jpg', '.jpeg', '.png', '.bmp', '.gif']
deleted_count = 0
for ext in image_extensions:
for image_path in glob.glob(os.path.join(directory, ext)):
try:
使用OCR识别图片中的文字
text = pytesseract.image_to_string(Image.open(image_path), lang='chi_sim')
if '机器人' in text:
os.remove(image_path)
print(f"已删除: {image_path}")
deleted_count += 1
except Exception as e:
print(f"处理 {image_path} 时出错: {str(e)}")
print(f"操作完成,共删除了 {deleted_count} 张机器人照片")
if __name__ == "__main__":
target_dir = input("请输入要扫描的目录路径: ")
if os.path.isdir(target_dir):
delete_robot_photos(target_dir)
else:
print("错误: 指定的路径不是有效目录")