def gen_jpg_tga(file_='', work_="", dds_list=None): try: if dds_list is None: dds_list = [] # f = open(file_, "r", encoding='utf-8-sig') # f.readline() for file in dds_list: file = file.replace("\n", "") low_file = file.lower() if low_file.find('_mre') > -1 or low_file.find('_nor') > -1: continue # 实例化 # file = file.replace("\n", "") a = PathUtils(_work=work_, dds_path=file) file_real_path = a.real_dds_path() # file_real_path = work_ + file if os.path.exists(file_real_path) is False: InfoNotifier.InfoNotifier.g_progress_info.append(f"{file_real_path} 不存在") continue jpg_path, tga_path = a.dds_to_jpg_path(), a.dds_to_tga_path() if os.path.exists(os.path.dirname(jpg_path)) is False: os.makedirs(os.path.dirname(jpg_path)) if os.path.exists(jpg_path) is False: main_cmd = f"{exe_dir} \"{file_real_path}\" \"{jpg_path}\" \"{tga_path}\"" main_cmd = main_cmd.replace("\n", "") print(main_cmd) # do real job os.system(main_cmd) else: print(jpg_path + ' 已存在,跳过') except BaseException as e: print(e)
def save_all(self): GlobalConfig.b_sync_block_op_in_progress = True QApplication.processEvents() InfoNotifier.InfoNotifier.g_progress_info.append( "开始保存图片··············") # gen_style_batch3.style_main3(self.content_list,self.chosen_style_pic) style_transfer.style_main(self.content_list, self.chosen_style_pic, self.project_base, False) for file in self.content_list: file_name = os.path.basename(file) get_path = PathUtils(self.project_base, self.chosen_style_pic, file) jpg_path = get_path.dds_to_jpg_path() tga_path = get_path.dds_to_tga_path() # lerp style_out_pic_path = get_path.get_style_path() if os.path.exists(style_out_pic_path) is False: InfoNotifier.InfoNotifier.g_progress_info.append( f"不存在对应风格化图片{style_out_pic_path}。跳过本张图片") continue lerp_out_path = get_path.get_jpg_lerp_path() if os.path.exists(os.path.dirname(lerp_out_path)) is False: os.makedirs(os.path.dirname(lerp_out_path)) lerp_ret, _ = gen_lerp_ret.lerp_img(jpg_path, style_out_pic_path, self.lerg_value) gen_lerp_ret.write_img(lerp_ret, lerp_out_path) # combine alpha c tga_img = Image.open(tga_path) jpg_img = Image.open(lerp_out_path) ir_tmp, ig_tmp, ib_tmp, ia = tga_img.split() ir, ig, ib = jpg_img.split() tga_img = Image.merge('RGBA', (ir, ig, ib, ia)) lerp_out_path = lerp_out_path.replace(".jpg", ".tga") tga_img.save(lerp_out_path, quality=100) print(f"generate tga image {lerp_out_path} after lerp op.") InfoNotifier.InfoNotifier.g_progress_info.append( f"生成插值操作后的tga图片: {lerp_out_path} ") # dds # 图片目录路径 dds_out = get_path.get_dds_output_path() if os.path.exists(dds_out) is False: os.makedirs(dds_out) main_cmd = f"{self.texconv_path} -dxt5 -file {lerp_out_path} -outdir {dds_out}" main_cmd.replace("\n", "") os.system(main_cmd) InfoNotifier.InfoNotifier.g_progress_info.append('生成DDS贴图:' + dds_out + file_name) InfoNotifier.InfoNotifier.g_progress_info.append("保存完成") GlobalConfig.b_sync_block_op_in_progress = False self._signal.emit()
def gen_expanded_pic(self): InfoNotifier.InfoNotifier.g_progress_info.append("开始生成DDS贴图············") GlobalConfig.b_sync_block_op_in_progress = True QApplication.processEvents() pad = 256 # 存放被选中的目录中图片的相对路径到pic_list中 pic_list = [] for file in self.chosen_content_file_list: file_path = self.file_dict[file] tmp = glob.glob(self.project_base + '/' + file_path + '/*.dds') for i in range(len(tmp)): tmp[i] = tmp[i].replace(self.project_base + '/', "") pic_list += tmp # 遍历文件目录 for file in pic_list: # 当前文件名 get_path = PathUtils(self.project_base, self.chosen_style_pic, file) expanded_jpg = get_path.get_expanded_jpg_path() expanded_tga = get_path.get_expanded_tga_path() if os.path.exists(expanded_jpg) is False: jpg_path = get_path.dds_to_jpg_path() tga_path = get_path.dds_to_tga_path() # expand img_jpg = Image.open(jpg_path) img_tga = Image.open(tga_path) width = img_jpg.width height = img_jpg.height assert width == img_tga.width and height == img_tga.height img_jpg_pad = Image.new("RGB", (width * 3, height * 3)) img_tga_pad = Image.new("RGBA", (width * 3, height * 3)) for i in range(3): for j in range(3): img_jpg_pad.paste(img_jpg, (i * width, j * height, (i + 1) * width, (j + 1) * height)) img_tga_pad.paste(img_tga, (i * width, j * height, (i + 1) * width, (j + 1) * height)) img_jpg_crop = img_jpg_pad.crop((width - pad, height - pad, 2 * width + pad, 2 * height + pad)) if os.path.exists(os.path.dirname(expanded_jpg)) is False: os.makedirs(os.path.dirname(expanded_jpg)) img_jpg_crop.save(expanded_jpg, quality=100) print(expanded_jpg) img_tga_crop = img_tga_pad.crop((width - pad, height - pad, 2 * width + pad, 2 * height + pad)) img_tga_crop.save(expanded_tga, quality=100) print(expanded_tga) InfoNotifier.InfoNotifier.g_progress_info.append( f'保存expand后图片:{expanded_tga},jpg') else: InfoNotifier.InfoNotifier.g_progress_info.append(expanded_tga + ' exists')
def gen_lerg(self, pic_list): InfoNotifier.InfoNotifier.g_progress_info.append("开始生成DDS贴图···········") for file_path in pic_list: file_name = os.path.basename(file_path) get_path = PathUtils(self.project_base, self.chosen_style_pic, file_path) # 原图 jpg_path = get_path.dds_to_jpg_path() tga_path = get_path.dds_to_tga_path() # 风格后图片 style_real_path = get_path.get_style_path() # 待保存-lerg lerp_real_path = get_path.get_jpg_lerp_path() # 待保存-dds dds_real_path = get_path.get_dds_output_path() ####################################################### if os.path.exists(jpg_path) is False: print("dds not transfered") continue if os.path.exists(style_real_path) is False: print('img not stylized') continue if os.path.exists(os.path.dirname(lerp_real_path)) is False: os.makedirs(os.path.dirname(lerp_real_path)) if os.path.exists(dds_real_path) is False: os.makedirs(dds_real_path) try: lerp_ret, ret = gen_lerp_ret.lerp_img(jpg_path, style_real_path, self.lerp_value) gen_lerp_ret.write_img(lerp_ret, lerp_real_path) except BaseException as be: print(be) # combine alpha channel tga_img = Image.open(tga_path) jpg_img = Image.open(lerp_real_path) ir_tmp, ig_tmp, ib_tmp, ia = tga_img.split() ir, ig, ib = jpg_img.split() tga_img = Image.merge('RGBA', (ir, ig, ib, ia)) lerp_real_path = lerp_real_path.replace(".jpg", ".tga") tga_img.save(lerp_real_path, quality=100) print(f"generate tga image {lerp_real_path} after lerp op.") InfoNotifier.InfoNotifier.g_progress_info.append(f"生成插值操作后的tga图片 {lerp_real_path} ") # dds main_cmd = f"{self.texconv_path} -dxt5 -file {lerp_real_path} -outdir {dds_real_path}" main_cmd.replace("\n", "") os.system(main_cmd) InfoNotifier.InfoNotifier.g_progress_info.append(f'将{dds_real_path}{file_name}转化为DDS格式···') InfoNotifier.InfoNotifier.g_progress_info.append("保存完成")
def expanded(self): InfoNotifier.InfoNotifier.g_progress_info.append( "开始保存图片··············") GlobalConfig.b_sync_block_op_in_progress = True QApplication.processEvents() pad = 256 for file in self.content_list: get_path = PathUtils(self.project_base, self.chosen_style_pic, file) jpg_path = get_path.dds_to_jpg_path() tga_path = get_path.dds_to_tga_path() expanded_jpg = get_path.get_expanded_jpg_path() expanded_tga = get_path.get_expanded_tga_path() # expand img_jpg = Image.open(jpg_path) img_tga = Image.open(tga_path) width = img_jpg.width height = img_jpg.height assert width == img_tga.width and height == img_tga.height img_jpg_pad = Image.new("RGB", (width * 3, height * 3)) img_tga_pad = Image.new("RGBA", (width * 3, height * 3)) for i in range(3): for j in range(3): img_jpg_pad.paste(img_jpg, (i * width, j * height, (i + 1) * width, (j + 1) * height)) img_tga_pad.paste(img_tga, (i * width, j * height, (i + 1) * width, (j + 1) * height)) img_jpg_crop = img_jpg_pad.crop( (width - pad, height - pad, 2 * width + pad, 2 * height + pad)) if os.path.exists(os.path.dirname(expanded_jpg)) is False: os.makedirs(os.path.dirname(expanded_jpg)) img_jpg_crop.save(expanded_jpg, quality=100) print(expanded_jpg) img_tga_crop = img_tga_pad.crop( (width - pad, height - pad, 2 * width + pad, 2 * height + pad)) img_tga_crop.save(expanded_tga, quality=100) print(expanded_tga) InfoNotifier.InfoNotifier.g_progress_info.append( f'保存expand后图片:{expanded_jpg},jpg')
def gen_lerp_pic(self): try: file_list = self.chosen_content_file_list # 存放被选中的目录中图片的相对路径 pic_list = [] for file in file_list: file_path = self.file_dict[file] tmp = glob.glob(self.project_base + '/' + file_path + '/*.dds') for i in range(len(tmp)): tmp[i] = tmp[i].replace(self.project_base + '/', "") pic_list += tmp for file in pic_list: file_name = os.path.basename(file) get_path = PathUtils(self.project_base, self.chosen_style_pic, file) jpg_path = get_path.get_expanded_jpg_path() tga_path = get_path.get_expanded_tga_path() tmp_style_in = jpg_path # lerp style_out_pic_path = get_path.get_expanded_style_path() if os.path.exists(style_out_pic_path) is False: InfoNotifier.InfoNotifier.g_progress_info.append("不存在对应风格化图片。跳过本张图片") continue lerp_out_path = get_path.get_expanded_lerp_path_jpg() if os.path.exists(os.path.dirname(lerp_out_path)) is False: os.makedirs(os.path.dirname(lerp_out_path)) lerp_ret, _ = gen_lerp_ret.lerp_img(tmp_style_in, style_out_pic_path, self.lerp_value) gen_lerp_ret.write_img(lerp_ret, lerp_out_path) # combine alpha c tga_img = Image.open(tga_path) jpg_img = Image.open(lerp_out_path) ir_tmp, ig_tmp, ib_tmp, ia = tga_img.split() ir, ig, ib = jpg_img.split() tga_img = Image.merge('RGBA', (ir, ig, ib, ia)) lerp_out_path = lerp_out_path.replace(".jpg", ".tga") tga_img.save(lerp_out_path, quality=100) print(f"generate tga image {lerp_out_path} after lerp op.") InfoNotifier.InfoNotifier.g_progress_info.append('生成插值操作后的tga图片' + lerp_out_path) # seamless seamless_path = os.path.dirname(get_path.get_seamless_path()) if os.path.exists(seamless_path) is False: os.makedirs(seamless_path) # print("seamless:"+PathUtils.get_expanded_lerp_path_tga()) img = Image.open(get_path.get_expanded_lerp_path_tga()) width = img.width height = img.height pad = 256 img_crop = img.crop((pad, pad, width - pad, height - pad)) img_crop.save(get_path.get_seamless_path(), quality=100) InfoNotifier.InfoNotifier.g_progress_info.append('生成无缝贴图' + get_path.get_seamless_path()) # dds dds_output = get_path.get_dds_output_path() if os.path.exists(dds_output) is False: os.makedirs(dds_output) main_cmd = f"{self.texconv_path} -dxt5 -file {get_path.get_seamless_path()} -outdir {dds_output}" main_cmd.replace("\n", "") os.system(main_cmd) InfoNotifier.InfoNotifier.g_progress_info.append(f'将{dds_output}{file_name}转化为DDS格式···') InfoNotifier.InfoNotifier.g_progress_info.append("保存完成") # InfoNotifier.InfoNotifier.g_progress_info.append("dds图片已转化完毕") except BaseException as be: print(be)
def save_all(self): # gen_seamless_style_batch3.style_main3(self.content_list,self.chosen_style_pic) style_transfer.style_main(self.content_list, self.chosen_style_pic, self.project_base, True) for file in self.content_list: file = file.replace("\n", "") file_name = os.path.basename(file) get_path = PathUtils(self.project_base, self.chosen_style_pic, file) jpg_path = get_path.get_expanded_jpg_path() tga_path = get_path.get_expanded_tga_path() tmp_style_in = jpg_path # lerp style_out_pic_path = get_path.get_expanded_style_path() if os.path.exists(style_out_pic_path) is False: InfoNotifier.InfoNotifier.g_progress_info.append( f"不存在对应风格化图片{style_out_pic_path}。跳过本张图片") continue lerp_out_path = get_path.get_expanded_lerp_path_jpg() if os.path.exists(os.path.dirname(lerp_out_path)) is False: os.makedirs(os.path.dirname(lerp_out_path)) lerp_ret, _ = gen_lerp_ret.lerp_img(tmp_style_in, style_out_pic_path, self.lerg_value) gen_lerp_ret.write_img(lerp_ret, lerp_out_path) # combine alpha c tga_img = Image.open(tga_path) jpg_img = Image.open(lerp_out_path) ir_tmp, ig_tmp, ib_tmp, ia = tga_img.split() ir, ig, ib = jpg_img.split() tga_img = Image.merge('RGBA', (ir, ig, ib, ia)) lerp_out_path = lerp_out_path.replace(".jpg", ".tga") tga_img.save(lerp_out_path, quality=100) print(f"generate tga image {lerp_out_path} after lerp op.") InfoNotifier.InfoNotifier.g_progress_info.append( f"生成插值操作后的tga图片 {lerp_out_path} ") # seamless seamless_path = get_path.get_seamless_path() if os.path.exists(os.path.dirname(seamless_path)) is False: os.makedirs(os.path.dirname(seamless_path)) img = Image.open(get_path.get_expanded_tga_path()) width = img.width height = img.height pad = 256 img_crop = img.crop((pad, pad, width - pad, height - pad)) img_crop.save(seamless_path, quality=100) InfoNotifier.InfoNotifier.g_progress_info.append("生成无缝贴图:" + seamless_path) dds_output = get_path.get_dds_output_path() if os.path.exists(dds_output) is False: os.makedirs(dds_output) main_cmd = f"{self.texconv_path} -dxt5 -file {seamless_path} -outdir {dds_output}" main_cmd.replace("\n", "") os.system(main_cmd) InfoNotifier.InfoNotifier.g_progress_info.append('生成DDS贴图:' + dds_output + file_name) InfoNotifier.InfoNotifier.g_progress_info.append("保存完成") GlobalConfig.b_sync_block_op_in_progress = False self._signal.emit()
def save_all(self): f = open(self.txt_path, "r", encoding='utf-8-sig') style_transfer.style_main_txt(self.txt_path, self.work_, self.chosen_style_pic, self.chosen_content_file_list, self.dir_dict, True) # gen_style_seamless_txt.style_txt_main2(self.txt_path,self.work_,self.chosen_style_pic,self.chosen_content_file_list,self.dir_dict) # gen_style_class.style_txt_main2(self.txt_path,self.work_,self.chosen_style_pic,self.chosen_content_file_list,self.dir_dict,True) for file in f: file = file.replace("\n", "").replace("\\", "/") # 判断该图片是否在选中目录中 flag = False for sub_file in self.chosen_content_file_list: if self.dir_dict[sub_file] == os.path.dirname(file): flag = True break if flag is True: get_path = PathUtils(self.work_, self.chosen_style_pic, file, self.txt_path) file_real_path = get_path.real_dds_path() file_name = os.path.basename(file_real_path) jpg_path = get_path.get_expanded_jpg_path() tga_path = get_path.get_expanded_tga_path() tmp_style_in = jpg_path # lerp style_out_pic_path = get_path.get_expanded_style_path() if os.path.exists(style_out_pic_path) is False: InfoNotifier.InfoNotifier.g_progress_info.append("不存在对应风格化图片。跳过本张图片") continue lerp_out_path = get_path.get_expanded_lerp_path_jpg() if os.path.exists(os.path.dirname(lerp_out_path)) is False: os.makedirs(os.path.dirname(lerp_out_path)) lerp_ret, _ = gen_lerp_ret.lerp_img(tmp_style_in, style_out_pic_path, self.lerg_value) gen_lerp_ret.write_img(lerp_ret, lerp_out_path) # combine alpha c tga_img = Image.open(tga_path) jpg_img = Image.open(lerp_out_path) ir_tmp, ig_tmp, ib_tmp, ia = tga_img.split() ir, ig, ib = jpg_img.split() tga_img = Image.merge('RGBA', (ir, ig, ib, ia)) lerp_out_path = lerp_out_path.replace(".jpg", ".tga") tga_img.save(lerp_out_path, quality=100) print(f"generate tga image {lerp_out_path} after lerp op.") InfoNotifier.InfoNotifier.g_progress_info.append('生成插值操作后的tga图片' + lerp_out_path) # seamless seamless_path = os.path.dirname(get_path.get_seamless_path()) if os.path.exists(seamless_path) is False: os.makedirs(seamless_path) # print("seamless:"+PathUtils.get_expanded_lerp_path_tga()) img = Image.open(get_path.get_expanded_lerp_path_tga()) width = img.width height = img.height pad = 256 img_crop = img.crop((pad, pad, width - pad, height - pad)) img_crop.save(get_path.get_seamless_path(), quality=100) InfoNotifier.InfoNotifier.g_progress_info.append('生成无缝贴图' + get_path.get_seamless_path()) # dds_output = get_path.get_seamless_dds_path() dds_output = get_path.get_dds_output_path_txt() if os.path.exists(dds_output) is False: os.makedirs(dds_output) main_cmd = f"{self.texconv_path} -dxt5 -file {get_path.get_seamless_path()} -outdir {dds_output}" main_cmd.replace("\n", "") os.system(main_cmd) InfoNotifier.InfoNotifier.g_progress_info.append(f'将{dds_output}{file_name}转化为DDS格式···') self.path_dic.set_src_dst_path_to_map(file, get_path.get_relative_dds_output_path()) self.path_dic.serilize_to_file(get_path.work_output, get_path.get_txt_name(), get_path.get_style_name()) InfoNotifier.InfoNotifier.g_progress_info.append("保存完成") GlobalConfig.b_sync_block_op_in_progress = False self._signal.emit()
def expanded(self): GlobalConfig.b_sync_block_op_in_progress = True QApplication.processEvents() pad = 256 f = open(self.txt_path, "r", encoding='utf-8-sig') for file in f: file = file.replace("\n", "").replace("\\", "/") get_path = PathUtils(self.work_, self.chosen_style_pic, file) # 判断该图片是否在选中目录中 flag = False for sub_file in self.chosen_content_file_list: if self.dir_dict[sub_file] == os.path.dirname(file): flag = True break if flag is True: if os.path.exists(get_path.get_expanded_tga_path()) is False: jpg_path = get_path.dds_to_jpg_path() tga_path = get_path.dds_to_tga_path() # expand img_jpg = Image.open(jpg_path) img_tga = Image.open(tga_path) width = img_jpg.width height = img_jpg.height assert width == img_tga.width and height == img_tga.height img_jpg_pad = Image.new("RGB", (width * 3, height * 3)) img_tga_pad = Image.new("RGBA", (width * 3, height * 3)) for i in range(3): for j in range(3): img_jpg_pad.paste(img_jpg, (i * width, j * height, (i + 1) * width, (j + 1) * height)) img_tga_pad.paste(img_tga, (i * width, j * height, (i + 1) * width, (j + 1) * height)) img_jpg_crop = img_jpg_pad.crop((width - pad, height - pad, 2 * width + pad, 2 * height + pad)) if os.path.exists(os.path.dirname(get_path.get_expanded_jpg_path())) is False: os.makedirs(os.path.dirname(get_path.get_expanded_jpg_path())) img_jpg_crop.save(get_path.get_expanded_jpg_path(), quality=100) print(get_path.get_expanded_jpg_path()) img_tga_crop = img_tga_pad.crop((width - pad, height - pad, 2 * width + pad, 2 * height + pad)) img_tga_crop.save(get_path.get_expanded_tga_path(), quality=100) print(get_path.get_expanded_tga_path()) InfoNotifier.InfoNotifier.g_progress_info.append( f'保存expand后图片:{get_path.get_expanded_tga_path()},jpg') else: InfoNotifier.InfoNotifier.g_progress_info.append(get_path.get_expanded_tga_path() + ' 已存在,跳过')
def save_all(self): GlobalConfig.b_sync_block_op_in_progress = True QApplication.processEvents() f = open(self.txt_path, "r", encoding='utf-8-sig') style_transfer.style_main_txt(self.txt_path, self.work_, self.chosen_style_pic, self.chosen_content_file_list, self.dir_dict, False) # style_txt_main2(self.txt_path,self.work_,self.chosen_style_pic,self.chosen_content_file_list,self.dir_dict) for file in f: file = file.replace("\n", "").replace("\\", "/") # 判断该图片是否在选中目录中 flag = False for sub_file in self.chosen_content_file_list: if self.dir_dict[sub_file] == os.path.dirname(file): flag = True break if flag is True: # file_real_path=self.work_+'/'+file # file_name = os.path.basename(file) get_path = PathUtils(_work=self.work_, _style_path=self.chosen_style_pic, dds_path=file, txt_file_=self.txt_path) jpg_path = get_path.dds_to_jpg_path() tga_path = get_path.dds_to_tga_path() # lerp style_out_pic_path = get_path.get_style_path() if os.path.exists(style_out_pic_path) is False: InfoNotifier.InfoNotifier.g_progress_info.append(f"不存在对应风格化图片{style_out_pic_path}。跳过本张图片") continue lerp_out_path = get_path.get_jpg_lerp_path() if os.path.exists(os.path.dirname(lerp_out_path)) is False: os.makedirs(os.path.dirname(lerp_out_path)) # 原图不存在则跳过 if os.path.exists(jpg_path) is False: print(jpg_path + " is not exist!") continue lerp_ret, _ = gen_lerp_ret.lerp_img(jpg_path, style_out_pic_path, self.lerg_value) gen_lerp_ret.write_img(lerp_ret, lerp_out_path) # combine alpha c tga_img = Image.open(tga_path) jpg_img = Image.open(lerp_out_path) ir_tmp, ig_tmp, ib_tmp, ia = tga_img.split() ir, ig, ib = jpg_img.split() tga_img = Image.merge('RGBA', (ir, ig, ib, ia)) lerp_out_path = lerp_out_path.replace(".jpg", ".tga") tga_img.save(lerp_out_path, quality=100) print(f"generate tga image {lerp_out_path} after lerp op.") InfoNotifier.InfoNotifier.g_progress_info.append(f"生成插值操作后的tga图片{lerp_out_path} ") dds_out = get_path.get_dds_output_path_txt() if os.path.exists(dds_out) is False: os.makedirs(dds_out) main_cmd = f"{self.texconv_path} -dxt5 -file {lerp_out_path} -outdir {dds_out}" main_cmd.replace("\n", "") try: os.system(main_cmd) InfoNotifier.InfoNotifier.g_progress_info.append(f"将{lerp_out_path}转化为DDS格式···") except BaseException as bec: InfoNotifier.InfoNotifier.g_progress_info.append(bec) self.path_dic.set_src_dst_path_to_map(file, get_path.get_relative_dds_output_path()) self.path_dic.serilize_to_file(get_path.work_output, get_path.get_txt_name(), get_path.get_style_name()) InfoNotifier.InfoNotifier.g_progress_info.append("保存完成") GlobalConfig.b_sync_block_op_in_progress = False self._signal.emit()
def style_main_txt(txt_path='', work_='', style_dir='', chosen_content_file_list=None, dir_dict=None, seamless=False): e_used = False if chosen_content_file_list is None: chosen_content_file_list = [] if dir_dict is None: dir_dict = {} if os.path.exists(txt_path) is not None: # content_name=pics_dir[0].replace("\\","/").split("/")[-2] # set device on GPU if available, else CPU device = Device() # set model d = load_model(device) s = Image.open(style_dir) s_tensor = trans(s).unsqueeze(0).to(device) # read txt # style_name=os.path.basename(style_dir).split('.')[0] f = open(txt_path, "r", encoding='utf-8-sig') for file_path in f: file_path = file_path.replace("\n", "").replace("\\", "/") flag = False # 判断该图片是否在选中目录中 for file in chosen_content_file_list: if dir_dict[file] == os.path.dirname(file_path): flag = True break if flag is True: # file_path=file_path.replace("\n","") # file_name=os.path.basename(file_path) # file_path=work_+'/'+file_path # parent_path=os.path.dirname(file_path) get_path = PathUtils(work_, style_dir, file_path) # get_path.work_ = work_ # get_path.style_path = style_dir # get_path.dds_path=file_path if seamless is False: jpg_path = get_path.dds_to_jpg_path() else: jpg_path = get_path.get_expanded_jpg_path() # jpg_path=parent_path+'/style_transfer/'+file_name.replace(".dds",".jpg") if os.path.exists(jpg_path) is False: print(jpg_path + "is not exist,jump from process") continue if seamless is False: style_output_path = get_path.get_style_path() else: style_output_path = get_path.get_expanded_style_path() style_outdir = os.path.dirname( os.path.dirname(style_output_path)) if os.path.exists(style_outdir) is False: os.makedirs(style_outdir) if jpg_path.endswith(".jpg") is False: continue try: # 文件名 # # file = os.path.basename(jpg_path) # c_name = os.path.splitext(os.path.basename(jpg_path))[0] # s_name = os.path.splitext(os.path.basename(style_dir))[0] file, c_name = get_c_name_and_file_name(jpg_path) s_name = get_style_name(style_dir) if os.path.exists(style_output_path) is False: # if os.path.exists(f'{style_outdir}{s_name}/' + file) is False: e = VGGEncoder().to(device) e_used = True tar = get_target_img(jpg_path, device, e, d, s_tensor, c_name, s_name, style_outdir=style_outdir) else: print("file exists") InfoNotifier.InfoNotifier.g_progress_info.append( style_output_path + '已存在,跳过') except RuntimeError: print( 'Images are too large to transfer. Size under 1000 are recommended ' + file_path) InfoNotifier.InfoNotifier.g_progress_info.append( f"{file_path}太大,无法迁移风格,推荐尝试1000×1000以下图片") try: if os.path.exists(style_output_path) is False: # save style transfer result if os.path.exists( os.path.dirname(style_output_path)) is False: os.makedirs(os.path.dirname(style_output_path)) tar.save(style_output_path, quality=100) print(f'result saved into files {style_output_path}/') InfoNotifier.InfoNotifier.g_progress_info.append( f'风格图保存到: {style_output_path}') else: print("exists") except BaseException as ec: print(ec) InfoNotifier.InfoNotifier.g_progress_info.append( 'error when saving stylized image') try: if e_used is True: del e except RuntimeError: pass
def style_main(pics_dir=None, style_dir='', base_dir='', seamless=False): e_used = False if pics_dir is None: pics_dir = [] s_name = os.path.splitext(os.path.basename(style_dir))[0] if pics_dir is not None: # content_name=pics_dir[0].replace("\\","/").split("/")[-2] # set device on GPU if available, else CPU # 判断是否存在预览图,若都已存在则不加载模型 flag = True for pic_dir in pics_dir: if os.path.exists( f'{os.path.dirname(pic_dir)}/temp/{s_name}/{os.path.basename(pic_dir)}' ) is False: flag = False break if flag is False: device = Device() # set model d = load_model(device) s = Image.open(style_dir) s_tensor = trans(s).unsqueeze(0).to(device) for file_path in pics_dir: file_path.replace("\\", "/") get_path = PathUtils(base_dir, style_dir, file_path) if seamless is False: jpg_path = get_path.dds_to_jpg_path() style_output = get_path.get_style_path() else: jpg_path = get_path.get_expanded_jpg_path() style_output = get_path.get_expanded_style_path() save_dir = os.path.dirname(os.path.dirname(style_output)) if os.path.exists(save_dir) is False: os.makedirs(save_dir) if os.path.exists(jpg_path) is False: print(jpg_path + "is not exist,jump from process") continue if jpg_path.endswith(".jpg") is False: continue try: # 文件名 # file=os.path.basename(jpg_path) # c_name = os.path.splitext(os.path.basename(jpg_path))[0] # s_name = os.path.splitext(os.path.basename(style_dir))[0] file, c_name = get_c_name_and_file_name(jpg_path) s_name = get_style_name(style_dir) print(s_name) if os.path.exists(style_output) is False: e = VGGEncoder().to(device) e_used = True tar = get_target_img(jpg_path, device, e, d, s_tensor, c_name, s_name, style_outdir=save_dir) else: print("file exists") InfoNotifier.InfoNotifier.g_progress_info.append( get_path.get_style_path() + '已存在,跳过') except RuntimeError: print( 'Images are too large to transfer. Size under 1000 are recommended ' + file_path) try: if os.path.exists(style_output) is False: # save style transfer result if os.path.exists( os.path.dirname(style_output)) is False: os.makedirs(os.path.dirname(style_output)) tar.save(style_output, quality=100) print(f'result saved into files {style_output}') InfoNotifier.InfoNotifier.g_progress_info.append( f'风格图保存到: {style_output}') else: # print('exists') InfoNotifier.InfoNotifier.g_progress_info.append( style_output + ' 已存在,跳过') except BaseException as ec: print(ec) try: if e_used is True: del e except RuntimeError: pass