def run(self): if self.tip_input(): return reply = QMessageBox.question(self, '执行', '确认执行吗?', QMessageBox.No | QMessageBox.Yes, QMessageBox.No) if reply == QMessageBox.Yes: service_image_path = utils.analysis_input_path( self.service_image_path) work_image_path = utils.analysis_input_path(self.work_image_path) # 保存数据 databases.set_normal_json_data({ "service_dir_path": service_image_path, "work_dir_path": work_image_path }) log_file = open(path.get_cache_path() + path.RUN_LOG_NAME, mode='w', encoding='utf-8') self.init_progress_dialog() try: modify_success = True core.select_service_url = self.select_service_url core.run(service_image_path, work_image_path, log_file=log_file, callback=self.progress_callback) except Exception as e: modify_success = False utils.print_log(log_file, str(e)) self.progress_callback(100, 100) log_file.close() QMessageBox.information( self, '提示', '壁纸配置修改成功!' if modify_success else "壁纸配置修改失败")
def modify_xml(work_image_path, image_type, start_index, count, log_file=None): utils.print_log(log_file, "") utils.print_log(log_file, "") utils.print_log(log_file, "修改配置文件--开始") old_path = work_image_path + "/xml/skin_" + image_type + ".xml" old_file = open(old_path, mode='r', encoding='utf-8') new_path = work_image_path + "/xml/skin_" + image_type + ".xml.ijs" new_file = open(new_path, mode='w', encoding='utf-8') line = old_file.readline() while line: new_file.write(line) if line.replace("\n", "") == "<skin>": for i in range(count): item_string = create_item_xml(image_type, start_index + i) new_file.write(item_string) new_file.write("\n") utils.print_log(log_file, item_string) line = old_file.readline() old_file.close() new_file.close() os.remove(old_path) os.rename(new_path, old_path) utils.print_log(log_file, "修改配置文件--结束")
def analysis(self, file_name): recommend_image_im_read = cv2.imread(self.recommend_image_path + "/" + file_name) recommend_image_a_hash = a_hash(recommend_image_im_read) recommend_image_d_hash = d_hash(recommend_image_im_read) recommend_image_p_hash = p_hash(recommend_image_im_read) similar_value = {} for image_path in self.all_image_path_list: image_im_read = cv2.imread(image_path) image_a_hash = a_hash(image_im_read) image_d_hash = d_hash(image_im_read) image_p_hash = p_hash(image_im_read) value1 = cmp_hash(recommend_image_a_hash, image_a_hash) value2 = cmp_hash(recommend_image_d_hash, image_d_hash) value3 = cmp_hash(recommend_image_p_hash, image_p_hash) value4 = classify_hist_with_split(recommend_image_im_read, image_im_read) if len(similar_value) == 0: similar_value["image_path"] = image_path similar_value["value1"] = value1 similar_value["value2"] = value2 similar_value["value3"] = value3 similar_value["value4"] = value4 else: similar_count = 0 if value1 < similar_value["value1"]: similar_count += 1 if value2 < similar_value["value2"]: similar_count += 1 if value3 < similar_value["value3"]: similar_count += 1 if value4 > similar_value["value4"]: similar_count += 1 if similar_count >= 3: similar_value["image_path"] = image_path similar_value["value1"] = value1 similar_value["value2"] = value2 similar_value["value3"] = value3 similar_value["value4"] = value4 self.count += 1 self.my_signal.emit({ "current": self.count, "total": self.all_count }) self.add_recommend_image_name_list.append( os.path.basename(similar_value["image_path"])) self.lock.acquire() utils.print_log(self.log_file, "") utils.print_log(self.log_file, "") utils.print_log(self.log_file, "*" * 100) utils.print_log(self.log_file, "相似度最大的壁纸") utils.print_log( self.log_file, "推荐壁纸:%s" % (self.recommend_image_path + "/" + file_name)) utils.print_log(self.log_file, "原始壁纸:%s" % similar_value["image_path"]) utils.print_log(self.log_file, '均值哈希算法相似度: %d' % similar_value["value1"]) utils.print_log(self.log_file, '差值哈希算法相似度: %d' % similar_value["value2"]) utils.print_log(self.log_file, '感知哈希算法相似度: %d' % similar_value["value3"]) utils.print_log(self.log_file, '三直方图算法相似度: %f' % similar_value["value4"]) utils.print_log(self.log_file, "*" * 100) if self.count == self.all_count: modify_recommend_xml(self.work_image_path, self.add_recommend_image_name_list, log_file=self.log_file) self.log_file.close() self.lock.release()
def run(self): self.callback(1, 100, label="正在分析推荐壁纸...") if self.callback else "" if not os.path.exists(self.work_image_path + "/xml/") or len( os.listdir(self.work_image_path + "/xml/")) == 0: utils.print_log(self.log_file, "壁纸还没有处理!!!") utils.print_log(self.log_file, "") self.callback(100, 100) if self.callback else "" return if os.path.exists(self.work_image_path + "/xml/skin_recommend.xml"): utils.print_log(self.log_file, "推荐壁纸已经处理过!!!") utils.print_log(self.log_file, "") self.callback(100, 100) if self.callback else "" return if os.path.exists(self.service_image_path + "/xml/skin_recommend.xml"): shutil.copy(self.service_image_path + "/xml/skin_recommend.xml", self.work_image_path + "/xml/") utils.print_log(self.log_file, "推荐壁纸配置文件已拷贝") utils.print_log(self.log_file, "") else: create_empty_xml_file(self.work_image_path + "/xml/skin_recommend.xml") utils.print_log(self.log_file, "推荐壁纸空配置文件已创建") utils.print_log(self.log_file, "") self.recommend_image_list.clear() for file in natsorted(os.listdir(self.recommend_image_path), alg=ns.PATH): if os.path.isfile(self.recommend_image_path + "/" + file): self.recommend_image_list.append(file) utils.print_log(self.log_file, "操作的推荐壁纸列表:") utils.print_list_log(self.log_file, self.recommend_image_list, 4, 30) utils.print_log(self.log_file, "") self.all_image_path_list = core.get_all_image(self.work_image_path) utils.print_log(self.log_file, "所有壁纸壁纸路径列表:") utils.print_list_log(self.log_file, self.all_image_path_list, 1, 0) self.add_recommend_image_name_list.clear() self.count = 0 self.all_count = len(self.recommend_image_list) * len( self.all_image_path_list) ThreadPool(2).imap_unordered(self.analysis, self.recommend_image_list)
def modify_recommend_xml(work_image_path, add_recommend_image_name_list, log_file=None): utils.print_log(log_file, "") utils.print_log(log_file, "") utils.print_log(log_file, "*" * 100) utils.print_log(log_file, "修改推荐配置文件--开始") old_path = work_image_path + "/xml/skin_recommend.xml" old_file = open(old_path, mode='r', encoding='utf-8') new_path = work_image_path + "/xml/skin_recommend.xml.ijs" new_file = open(new_path, mode='w', encoding='utf-8') line = old_file.readline() while line: new_file.write(line) if line.replace("\n", "") == "<skin>": for name in add_recommend_image_name_list: image_type = str(name).split("_")[0] index = int(name.split("_")[1]) if "." not in name else int( name.split("_")[1].split(".")[0]) item_string = core.create_item_xml(image_type, index) new_file.write(item_string) new_file.write("\n") utils.print_log(log_file, item_string) line = old_file.readline() old_file.close() new_file.close() os.remove(old_path) os.rename(new_path, old_path) utils.print_log(log_file, "修改推荐配置文件--结束") utils.print_log(log_file, "*" * 100)
def modify_image(service_image_path, work_image_path, image_type, log_file=None): if os.path.exists(work_image_path + "/xml/skin_" + image_type + ".xml"): utils.print_log(log_file, "壁纸已经处理过!!!") return image_dir_path = work_image_path + "/skin_original/" + image_type + "/" image_file_list = [] for file in natsorted(os.listdir(image_dir_path), alg=ns.PATH): if os.path.isfile(image_dir_path + file): image_file_list.append(file) thumb_dir_path = work_image_path + "/skin_thumb/" + image_type + "/" if not os.path.exists(thumb_dir_path): utils.print_log(log_file, "缩略图不存在!!!") return thumb_file_list = [] for file in natsorted(os.listdir(thumb_dir_path), alg=ns.PATH): if os.path.isfile(thumb_dir_path + file): thumb_file_list.append(file) if len(image_file_list ) == 0 or len(image_file_list) != len(thumb_file_list): utils.print_log(log_file, "壁纸和缩略图的数量不一致!!!") return # 工作目录下不存在xml文件夹,则创建 if not os.path.exists(work_image_path + "/xml/"): os.makedirs(work_image_path + "/xml/") # 拷贝服务器配置的配置表,不存在时创建文件 if os.path.exists(service_image_path + "/xml/skin_" + image_type + ".xml"): shutil.copy(service_image_path + "/xml/skin_" + image_type + ".xml", work_image_path + "/xml/") utils.print_log(log_file, "配置文件已拷贝") start_index = len( os.listdir(service_image_path + "/skin_original/" + image_type + "/")) + 1 utils.print_log(log_file, "壁纸 Start Index = %d" % start_index) else: create_empty_xml_file(work_image_path + "/xml/skin_" + image_type + ".xml") utils.print_log(log_file, "新增类型,空配置文件已创建") start_index = 1 utils.print_log(log_file, "壁纸 Start Index = %d" % start_index) modify_xml(work_image_path, image_type, start_index, len(image_file_list), log_file=log_file) modify_image_name(work_image_path, image_type, start_index, image_file_list, log_file=log_file) modify_thumb_name(work_image_path, image_type, start_index, thumb_file_list, log_file=log_file)
def modify_thumb_name(work_image_path, image_type, start_index, thumb_file_list, log_file=None): utils.print_log(log_file, "") utils.print_log(log_file, "") utils.print_log(log_file, "重命名缩略图--开始") utils.print_log(log_file, "重命名前的缩略图:") utils.print_list_log(log_file, thumb_file_list, 6, 30) index = len(thumb_file_list) - 1 while index >= 0: new_index = start_index + index old_file = work_image_path + "/skin_thumb/" + image_type + "/" + thumb_file_list[ index] new_file = work_image_path + "/skin_thumb/" + image_type + "/" + image_type + "_" + int2str( new_index) utils.print_log(log_file, "缩略图原路径: %s" % old_file) utils.print_log(log_file, "缩略图新路径: %s" % new_file) if old_file == new_file: utils.print_log(log_file, "缩略图原路径 == 缩略图新路径") else: if os.path.exists(new_file): new_file += ".conflict" utils.print_log(log_file, "缩略图新路径已存在 修改为: %s" % new_file) os.rename(old_file, new_file) utils.print_log(log_file, "缩略图重命名成功") index -= 1 utils.print_log(log_file, "重命名缩略图--结束")
def modify_image_name(work_image_path, image_type, start_index, image_file_list, log_file=None): utils.print_log(log_file, "") utils.print_log(log_file, "") utils.print_log(log_file, "重命名壁纸--开始") utils.print_log(log_file, "重命名前的原始壁纸:") utils.print_list_log(log_file, image_file_list, 6, 30) index = len(image_file_list) - 1 while index >= 0: new_index = start_index + index old_file = work_image_path + "/skin_original/" + image_type + "/" + image_file_list[ index] new_file = work_image_path + "/skin_original/" + image_type + "/" + image_type + "_" + int2str( new_index) utils.print_log(log_file, "壁纸原路径: %s" % old_file) utils.print_log(log_file, "壁纸新路径: %s" % new_file) if old_file == new_file: utils.print_log(log_file, "壁纸原路径 == 壁纸新路径") else: if os.path.exists(new_file): new_file += ".conflict" utils.print_log(log_file, "壁纸新路径已存在 修改为: %s" % new_file) os.rename(old_file, new_file) utils.print_log(log_file, "壁纸重命名成功") index -= 1 utils.print_log(log_file, "重命名壁纸--结束")
def run(service_image_path, work_image_path, log_file=None, callback=None): if callback: callback(10, 100, label="正在修改壁纸...") # 解析需要操作的壁纸文件夹 modify_image_type_list.clear() for file in natsorted(os.listdir(work_image_path + "/skin_original/"), alg=ns.PATH): if os.path.isdir(work_image_path + "/skin_original/" + file) and file not in FILTER_DIR_LIST: modify_image_type_list.append(file) utils.print_log(log_file, "操作的壁纸类型列表:") utils.print_list_log(log_file, modify_image_type_list, 6, 20) count = 1 for image_type in modify_image_type_list: utils.print_log(log_file, "") utils.print_log(log_file, "") utils.print_log(log_file, "") utils.print_log(log_file, "") utils.print_log(log_file, "*" * 100) utils.print_log(log_file, "操作壁纸类型 : %s" % image_type) modify_image(service_image_path, work_image_path, image_type, log_file=log_file) utils.print_log(log_file, "*" * 100) if callback: callback(count, len(modify_image_type_list)) count += 1