1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133
| def write_images_to_excel(dir1, dir2, output_excel, progress_var, progress_bar, progress_label, ps_root, instruction_txt_path): """Write images and filenames to Excel.""" files_dir1 = get_all_files(dir1) files_dir2 = get_all_files(dir2)
images_map_dir1 = {} images_map_dir2 = {}
for file in files_dir1: if is_image(file): identifier = extract_identifier_de(os.path.basename(file)) or os.path.basename(file) images_map_dir1[identifier] = file else: logger.warning(f"file path not image!!! {file}")
for file in files_dir2: if is_image(file): identifier = extract_identifier_de(os.path.basename(file)) or os.path.basename(file) images_map_dir2[identifier] = file else: logger.warning(f"file path not image!!! {file}")
if not images_map_dir1 and not images_map_dir2: logger.warning("No image files found.") return False
common_identifiers = list(set(images_map_dir1.keys()) & set(images_map_dir2.keys())) total_images = len(common_identifiers) logger.info(f"total image: {total_images}!!!")
if dev_tools_mode: logger.info("dev tools mode enable!!!") compare_and_info_dicts(images_map_dir1, images_map_dir2)
if not common_identifiers: logger.warning("No common identifiers found.") return False
common_identifiers.sort( key=lambda x: (int(extract_identifier(x)) if extract_identifier(x).isdigit() else float('inf'), x))
output_dir = os.path.dirname(output_excel) base_name, ext = os.path.splitext(os.path.basename(output_excel)) os.makedirs(output_dir, exist_ok=True)
with tqdm(total=total_images, desc="Processing Images", unit="pair", dynamic_ncols=True) as pbar: for start_index in range(0, total_images, 500): end_index = min(start_index + 500, total_images) current_identifiers = common_identifiers[start_index:end_index]
sheet_data, temp_files = process_image_group(current_identifiers, images_map_dir1, images_map_dir2, instruction_txt_path, start_index)
workbook = openpyxl.Workbook() sheet = workbook.active sheet.title = "Image Comparison"
headers = ["序号", "原图文件名", "原图", "修改图文件名", "修改图", "修改内容"] for col_num, header in enumerate(headers, 1): cell = sheet.cell(row=1, column=col_num, value=header) cell.alignment = Alignment(horizontal='center')
row = 2 original_filenames = [] processed_filenames = [] modification_instructions = []
for item in sheet_data: try: sheet.cell(row=row, column=1, value=item['index']).alignment = Alignment(horizontal='center') original_filenames.append(item['original_filename']) sheet.cell(row=row, column=2, value=item['original_filename'])
if item['img1']: excel_img1 = ExcelImage(item['temp_img1_path']) sheet.add_image(excel_img1, f'C{row}') sheet.row_dimensions[row].height = 259.8
processed_filenames.append(item['processed_filename']) sheet.cell(row=row, column=4, value=item['processed_filename'])
if item['img2']: excel_img2 = ExcelImage(item['temp_img2_path']) sheet.add_image(excel_img2, f'E{row}') sheet.row_dimensions[row].height = 259.8
modification_instructions.append(item['instruction_text']) sheet.cell(row=row, column=6, value=item['instruction_text']).alignment = Alignment(vertical='top')
row += 1 except Exception as e: logger.exception(f"Error adding row: {e}")
adjust_column_width(sheet, 'B', original_filenames) adjust_column_width(sheet, 'D', processed_filenames) adjust_column_width(sheet, 'F', modification_instructions) adjust_column_width(sheet, 'A', range(start_index + 1, start_index + len(current_identifiers) + 1)) sheet.column_dimensions['C'].width = 80.79 sheet.column_dimensions['E'].width = 80.79
file_counter = start_index // 500 + 1 new_output_excel = os.path.join(output_dir, f"{base_name}_{file_counter}{ext}")
try: workbook.save(new_output_excel) logger.info(f"Saved batch: {new_output_excel}") except Exception as e: logger.error(f"Failed to save {new_output_excel}: {e}") return False finally: for temp_file in temp_files: try: os.remove(temp_file) except FileNotFoundError: pass except Exception as e: logger.error(f"Error deleting {temp_file}: {e}")
pbar.update(len(current_identifiers)) progress_percentage = int((pbar.n / total_images) * 100) progress_var.set(progress_percentage) progress_label.config(text=f"{progress_percentage}%") ps_root.update_idletasks()
progress_var.set(100) progress_label.config(text="100%") ps_root.update_idletasks() return True
|