|
|
@@ -1,11 +1,11 @@
|
|
|
-import os,re
|
|
|
+import os, re
|
|
|
|
|
|
-from natsort import natsorted,ns
|
|
|
+from natsort import natsorted, ns
|
|
|
import shutil
|
|
|
import exifread
|
|
|
import time
|
|
|
import datetime
|
|
|
-from databases import PhotoRecord,SqlQuery,CRUD
|
|
|
+from databases import PhotoRecord, SqlQuery, CRUD
|
|
|
import settings
|
|
|
import copy
|
|
|
import json
|
|
|
@@ -17,6 +17,7 @@ from PIL import Image
|
|
|
from utils.utils_func import check_path
|
|
|
from .base_deal import BaseDealImage
|
|
|
from middleware import UnicornException
|
|
|
+
|
|
|
_Type = ['.png', '.PNG', '.jpg', '.JPG', '.gif', '.GIF', ".jpge", ".JPGE"]
|
|
|
|
|
|
|
|
|
@@ -26,16 +27,20 @@ class DealImage(BaseDealImage):
|
|
|
super().__init__()
|
|
|
self.image_dir = image_dir
|
|
|
self.header = None
|
|
|
+
|
|
|
def check_path(self, image_dir: str):
|
|
|
if not os.path.exists(image_dir):
|
|
|
os.mkdir(image_dir)
|
|
|
return True
|
|
|
+
|
|
|
def list_dir(self, path):
|
|
|
listdir = os.listdir(path)
|
|
|
- sort_result = sorted(listdir, key=lambda x: int(re.findall(r'^(\d+)_', x)[0]) if re.findall(r'^(\d+)_', x) else 0)
|
|
|
+ sort_result = sorted(listdir,
|
|
|
+ key=lambda x: int(re.findall(r'^(\d+)_', x)[0]) if re.findall(r'^(\d+)_', x) else 0)
|
|
|
# print("listdir 排序排序排序排序排序排序", sort_result)
|
|
|
# return natsorted(listdir, alg=ns.PATH)
|
|
|
return sort_result
|
|
|
+
|
|
|
def get_date_time_original(self, file_path):
|
|
|
with open(file_path, 'rb') as file_data:
|
|
|
tags = exifread.process_file(file_data)
|
|
|
@@ -54,7 +59,7 @@ class DealImage(BaseDealImage):
|
|
|
configModel = CRUD(PhotoRecord)
|
|
|
result = configModel.read(
|
|
|
session,
|
|
|
- conditions={"goods_art_no": goods_art_no,"delete_time": None},
|
|
|
+ conditions={"goods_art_no": goods_art_no, "delete_time": None},
|
|
|
order_by="id",
|
|
|
ascending=True,
|
|
|
)
|
|
|
@@ -63,6 +68,7 @@ class DealImage(BaseDealImage):
|
|
|
return result.goods_art_no, result.image_index, result.image_deal_mode
|
|
|
else:
|
|
|
return None
|
|
|
+
|
|
|
def get_goods_art_no_files(self, goods_art_no):
|
|
|
# time_array = time.strptime(date_time_original, "%Y:%m:%d %H:%M:%S")
|
|
|
|
|
|
@@ -73,15 +79,16 @@ class DealImage(BaseDealImage):
|
|
|
configModel = CRUD(PhotoRecord)
|
|
|
result = configModel.read_all(
|
|
|
session,
|
|
|
- conditions={"goods_art_no": goods_art_no,"delete_time": None},
|
|
|
+ conditions={"goods_art_no": goods_art_no, "delete_time": None},
|
|
|
order_by="id",
|
|
|
ascending=True,
|
|
|
)
|
|
|
session.close()
|
|
|
if result:
|
|
|
- return [{"image_path":item.image_path,"id":item.id,"image_index":item.image_index} for item in result]
|
|
|
+ return [{"image_path": item.image_path, "id": item.id, "image_index": item.image_index} for item in result]
|
|
|
else:
|
|
|
return []
|
|
|
+
|
|
|
def get_data_from_hqt_with_goods_art_no(self, goods_art_no_list):
|
|
|
_goods_art_no_list = copy.deepcopy(goods_art_no_list)
|
|
|
_list = []
|
|
|
@@ -196,6 +203,7 @@ class DealImage(BaseDealImage):
|
|
|
shutil.move(old_image_path, original_image_path)
|
|
|
except Exception as e:
|
|
|
print(f"文件操作异常:{e}")
|
|
|
+
|
|
|
def copy_images(self, goods_art_no, goods_art_no_path, old_image_path):
|
|
|
"""
|
|
|
步骤:
|
|
|
@@ -221,18 +229,19 @@ class DealImage(BaseDealImage):
|
|
|
original_image_path = "{}/原始图/{}{}".format(goods_art_no_path, new_file_name, e)
|
|
|
try:
|
|
|
shutil.copy2(old_image_path, original_image_path)
|
|
|
- return True,"移动成功"
|
|
|
+ return True, "移动成功"
|
|
|
except Exception as e:
|
|
|
print(f"文件操作异常:{e}")
|
|
|
- return False,f"货号[{goods_art_no}]原始图{old_image_path}不存在或已被删除"
|
|
|
- def dealMoveImage(self, image_dir: str, callback_func=None,goods_art_no=None) -> dict:
|
|
|
+ return False, f"货号[{goods_art_no}]原始图{old_image_path}不存在或已被删除"
|
|
|
+
|
|
|
+ def dealMoveImage(self, image_dir: str, callback_func=None, goods_art_no=None) -> dict:
|
|
|
if not self.check_path(image_dir=image_dir + "/历史"):
|
|
|
raise UnicornException("文件夹创建失败")
|
|
|
|
|
|
# 遍历目标文件夹,获取有拍摄信息的图片,并按拍摄时间排序
|
|
|
files = self.list_dir(image_dir)
|
|
|
original_photo_list = [] # 原始图片列表
|
|
|
- for file_idx,file in enumerate(files):
|
|
|
+ for file_idx, file in enumerate(files):
|
|
|
# -----图片清洗
|
|
|
file_path = image_dir + "/" + file
|
|
|
if os.path.isdir(file_path): # 忽略文件夹
|
|
|
@@ -249,7 +258,7 @@ class DealImage(BaseDealImage):
|
|
|
if _data:
|
|
|
# 能匹配上数据库
|
|
|
goods_art_no, image_index, image_deal_mode = _data
|
|
|
- print("832 与数据库匹配goods_art_no", file_path,file_name, date_time_original, goods_art_no)
|
|
|
+ print("832 与数据库匹配goods_art_no", file_path, file_name, date_time_original, goods_art_no)
|
|
|
original_photo_list.append({"file_path": file_path,
|
|
|
"file": file,
|
|
|
"date_time_original": date_time_original,
|
|
|
@@ -280,14 +289,15 @@ class DealImage(BaseDealImage):
|
|
|
total_num = len(original_photo_list)
|
|
|
# 当天日期作为文件夹
|
|
|
seconds = time.time()
|
|
|
- output_path = "{}/{f_name}".format(settings.OUTPUT_DIR,f_name=time.strftime("%Y-%m-%d", time.localtime(seconds)))
|
|
|
+ output_path = "{}/{f_name}".format(settings.OUTPUT_DIR,
|
|
|
+ f_name=time.strftime("%Y-%m-%d", time.localtime(seconds)))
|
|
|
|
|
|
# 遍历每个匹配好的数据进行处理
|
|
|
- print(f"original_photo_list:",original_photo_list)
|
|
|
+ print(f"original_photo_list:", original_photo_list)
|
|
|
n = 0
|
|
|
for photo_dict in original_photo_list:
|
|
|
n += 1
|
|
|
- print("处理dict:",photo_dict)
|
|
|
+ print("处理dict:", photo_dict)
|
|
|
# 进度条
|
|
|
goods_art_no = photo_dict["goods_art_no"]
|
|
|
original_image_path = photo_dict["file_path"]
|
|
|
@@ -325,13 +335,61 @@ class DealImage(BaseDealImage):
|
|
|
# 完成处理
|
|
|
# self.set_state(state_value=2)
|
|
|
return True, output_path
|
|
|
- def dealMoveImageV2(self,goods_art_no=None) -> dict:
|
|
|
+
|
|
|
+ def dealMoveImageV2(self, goods_art_no=None) -> dict:
|
|
|
+ # 遍历目标文件夹,获取有拍摄信息的图片,并按拍摄时间排序
|
|
|
+ original_photo_list = [] # 原始图片列表
|
|
|
+ _data = self.get_goods_art_no_files(goods_art_no)
|
|
|
+ if _data:
|
|
|
+ for file_idx, dataItem in enumerate(_data):
|
|
|
+ # 能匹配上数据库
|
|
|
+ file_path = dataItem['image_path']
|
|
|
+ original_photo_list.append({"file_path": file_path,
|
|
|
+ "goods_art_no": goods_art_no,
|
|
|
+ "image_index": file_idx,
|
|
|
+ })
|
|
|
+ if not original_photo_list:
|
|
|
+ raise UnicornException("没有任何匹配的图片")
|
|
|
+ # 当天日期作为文件夹
|
|
|
+ seconds = time.time()
|
|
|
+ output_path = "{}/{f_name}".format(settings.OUTPUT_DIR,
|
|
|
+ f_name=time.strftime("%Y-%m-%d", time.localtime(seconds)))
|
|
|
+
|
|
|
+ # 遍历每个匹配好的数据进行处理
|
|
|
+ print(f"original_photo_list:", original_photo_list)
|
|
|
+ n = 0
|
|
|
+ for photo_dict in original_photo_list:
|
|
|
+ n += 1
|
|
|
+ print("处理dict:", photo_dict)
|
|
|
+ # 进度条
|
|
|
+ goods_art_no = photo_dict["goods_art_no"]
|
|
|
+ original_image_path = photo_dict["file_path"]
|
|
|
+ goods_art_no_path = "{output_path}/{goods_art_no}".format(output_path=output_path,
|
|
|
+ goods_art_no=goods_art_no)
|
|
|
+ # 创建货号下的一系列文件夹
|
|
|
+ self.create_folder(goods_art_no_path)
|
|
|
+ # 重命名并进行移动
|
|
|
+ print("开始移动:{} {} 命名为:{}".format(goods_art_no, original_image_path, goods_art_no_path))
|
|
|
+ result, message = self.copy_images(goods_art_no, goods_art_no_path, original_image_path) # 货号、货号文件路径、原始图路径
|
|
|
+ if not result:
|
|
|
+ return result, message
|
|
|
+ time.sleep(0.1)
|
|
|
+ print("已完成移动处理")
|
|
|
+ if n != 0:
|
|
|
+ # 自动生成一个货号表
|
|
|
+ print("output_path", output_path)
|
|
|
+ self.deal(output_path)
|
|
|
+ # 完成处理
|
|
|
+ # self.set_state(state_value=2)
|
|
|
+ return True, output_path
|
|
|
+
|
|
|
+ def dealImagePath(self, goods_art_no=None) -> dict:
|
|
|
# 遍历目标文件夹,获取有拍摄信息的图片,并按拍摄时间排序
|
|
|
original_photo_list = [] # 原始图片列表
|
|
|
_data = self.get_goods_art_no_files(goods_art_no)
|
|
|
if _data:
|
|
|
for file_idx, dataItem in enumerate(_data):
|
|
|
- # 能匹配上数据库
|
|
|
+ # 能匹配上数据库
|
|
|
file_path = dataItem['image_path']
|
|
|
original_photo_list.append({"file_path": file_path,
|
|
|
"goods_art_no": goods_art_no,
|
|
|
@@ -341,14 +399,15 @@ class DealImage(BaseDealImage):
|
|
|
raise UnicornException("没有任何匹配的图片")
|
|
|
# 当天日期作为文件夹
|
|
|
seconds = time.time()
|
|
|
- output_path = "{}/{f_name}".format(settings.OUTPUT_DIR,f_name=time.strftime("%Y-%m-%d", time.localtime(seconds)))
|
|
|
+ output_path = "{}/{f_name}".format(settings.OUTPUT_DIR,
|
|
|
+ f_name=time.strftime("%Y-%m-%d", time.localtime(seconds)))
|
|
|
|
|
|
# 遍历每个匹配好的数据进行处理
|
|
|
- print(f"original_photo_list:",original_photo_list)
|
|
|
+ print(f"original_photo_list:", original_photo_list)
|
|
|
n = 0
|
|
|
for photo_dict in original_photo_list:
|
|
|
n += 1
|
|
|
- print("处理dict:",photo_dict)
|
|
|
+ print("处理dict:", photo_dict)
|
|
|
# 进度条
|
|
|
goods_art_no = photo_dict["goods_art_no"]
|
|
|
original_image_path = photo_dict["file_path"]
|
|
|
@@ -370,6 +429,7 @@ class DealImage(BaseDealImage):
|
|
|
# 完成处理
|
|
|
# self.set_state(state_value=2)
|
|
|
return True, output_path
|
|
|
+
|
|
|
def deal(cls, dir_path):
|
|
|
|
|
|
# print("dir_path", dir_path)
|
|
|
@@ -412,7 +472,9 @@ class DealImage(BaseDealImage):
|
|
|
continue
|
|
|
|
|
|
all_original_images = os.listdir(_path) # 遍历货号原始图文件夹
|
|
|
- sort_result = sorted(all_original_images, key=lambda x: int(re.findall(r'((\d+))', x)[0]) if re.findall(r'((\d+))', x) else 0)
|
|
|
+ sort_result = sorted(all_original_images,
|
|
|
+ key=lambda x: int(re.findall(r'((\d+))', x)[0]) if re.findall(r'((\d+))',
|
|
|
+ x) else 0)
|
|
|
goods_pic_total = len(sort_result)
|
|
|
if not sort_result:
|
|
|
continue
|
|
|
@@ -492,9 +554,9 @@ class DealImage(BaseDealImage):
|
|
|
"y_scale": y_scale,
|
|
|
"x_offset": 5,
|
|
|
"y_offset": 5,
|
|
|
- "object_position":1,
|
|
|
+ "object_position": 1,
|
|
|
},
|
|
|
- )
|
|
|
+ )
|
|
|
|
|
|
except:
|
|
|
pass
|