69 lines
2.6 KiB
Python
69 lines
2.6 KiB
Python
# -*- coding: utf-8 -*-
|
|
# @Time : 2024/4/22 11:12
|
|
# @Author : len
|
|
# @File : 批量识别二维码内容-移动位置.py
|
|
# @Software: PyCharm
|
|
# @Comment :
|
|
|
|
import os
|
|
import shutil
|
|
|
|
from PIL import Image
|
|
from pyzbar.pyzbar import decode
|
|
|
|
# 设置包含图片的文件夹路径
|
|
folder_path = r'D:\Waste\嵌入式\数据集\二维码\颜色分类\qr'
|
|
|
|
# 设置目标文件夹路径
|
|
destination_folders = {
|
|
'yellow': r'D:\Waste\嵌入式\数据集\二维码\颜色分类\220(1)\yellow',
|
|
'red': r'D:\Waste\嵌入式\数据集\二维码\颜色分类\220(1)\red',
|
|
'green': r'D:\Waste\嵌入式\数据集\二维码\颜色分类\220(1)\green',
|
|
'blue': r'D:\Waste\嵌入式\数据集\二维码\颜色分类\220(1)\blue'
|
|
}
|
|
|
|
# 遍历文件夹内的每个文件
|
|
for filename in os.listdir(folder_path):
|
|
if filename.lower().endswith(('.png', '.jpg', '.jpeg', '.gif', '.bmp')):
|
|
# 构建完整的文件路径
|
|
file_path = os.path.join(folder_path, filename)
|
|
|
|
# 使用Pillow读取图片
|
|
image = Image.open(file_path)
|
|
|
|
# 使用pyzbar解码二维码
|
|
decoded_objects = decode(image)
|
|
|
|
# 检查二维码数据并根据内容分类复制文件
|
|
for obj in decoded_objects:
|
|
data = obj.data.decode('utf-8').lower()
|
|
destination = None
|
|
if 'yellow' in data:
|
|
destination = destination_folders['yellow']
|
|
elif 'red' in data:
|
|
destination = destination_folders['red']
|
|
elif 'green' in data:
|
|
destination = destination_folders['green']
|
|
elif 'blue' in data:
|
|
destination = destination_folders['blue']
|
|
|
|
# # 如果找到目的地,尝试移动文件
|
|
# if destination:
|
|
# destination_file = os.path.join(destination, filename)
|
|
# if not os.path.exists(destination_file):
|
|
# shutil.move(file_path, destination)
|
|
# print(f"移动 '{filename}' 到 '{destination}'")
|
|
# else:
|
|
# print(f"'{filename}' 已存在于 '{destination}',跳过移动。")
|
|
|
|
# 如果找到目的地,尝试移动文件
|
|
if destination:
|
|
destination_file = os.path.join(destination, filename)
|
|
# 如果目标文件存在,先删除
|
|
if os.path.exists(destination_file):
|
|
os.remove(destination_file)
|
|
print(f"'{destination_file}' 已存在,将被覆盖。")
|
|
shutil.move(file_path, destination)
|
|
print(f"移动 '{filename}' 到 '{destination}'")
|
|
|