Refactor the code. The processfile function contains repetitive code for handling…
- User
Refactor the code. The process_file function contains repetitive code for handling different processing scenarios (resizing, auto-correction, etc.). Code: def process_file( path: Path, start_time, total_files, image_processor: 'ImageProcessor', temp_folder: Path, resize=False, auto_correct_and_resize=False ) -> None: """Process a single image file.""" if shared_state.stop_event.is_set(): return
codeimg = validate_and_load_image(path) if img is None: log_error(f"Invalid image file: {path}") with shared_state.processed_files_lock: shared_state.processed_files_count += 1 update_status(start_time, shared_state.processed_files_count, total_files) return try: img_array = np.array(img) except (OSError, ValueError) as e: log_error(f"Error reading image {path}: {e}") with shared_state.processed_files_lock: shared_state.processed_files_count += 1 update_status(start_time, shared_state.processed_files_count, total_files) return relative_path = path.relative_to(Config.INPUT_FOLDER) temp_output_path = temp_folder / relative_path.with_suffix('.png') temp_output_path.parent.mkdir(parents=True, exist_ok=True) if auto_correct_and_resize: # Adjust and save image, then resize intermediate_path, _, _ = adjust_and_save_image(img_array, path, image_processor, temp_folder) temp_output_path = resize_image(intermediate_path, temp_output_path) elif resize: # Only resize the original image temp_output_path = resize_image(path, temp_output_path) else: # Adjust and save image without resizing temp_output_path, _, _ = adjust_and_save_image(img_array, path, image_processor, temp_folder) final_output_path = Config.OUTPUT_FOLDER / relative_path.with_suffix( '.jpg' if app.RESIZE_FORMAT.get() == 'jpg' else '.png' ) final_output_path.parent.mkdir(parents=True, exist_ok=True) if app.RESIZE_FORMAT.get() == 'jpg': convert_png_to_jpg(temp_output_path, final_output_path) else: shutil.move(str(temp_output_path), str(final_output_path)) increment_processed_count(start_time, total_files) - Assistant