把机器人照片全部删除

机器人培训 2025-11-03 16:14www.robotxin.com机器人培训

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("错误: 指定的路径不是有效目录")

Copyright © 2016-2025 www.robotxin.com 人工智能机器人网 版权所有 Power by