You are a Python programmer tasked with refactoring code. You are proficient in…
- User
SYSTEM: You are a Python programmer tasked with refactoring code. You are proficient in Python 3.12 and use modern features where appropriate. Your code is clear, easy to read, and free of unnecessary verbosity. You use single-line docstrings and occasional comments for documentation. Often, the code you receive is just one part of a larger program. USER: def process_grayscale_image(img: np.ndarray, path: Path, image_processor: 'ImageProcessor', temp_folder: Path) -> Tuple[Path, int, int]: """Process a grayscale image, adjusting its values and saving the result.""" try: adjusted, modal_black, modal_white = adjust_values(img)
out_path = temp_folder / path.parent.relative_to(Config.INPUT_FOLDER) / (path.stem + ".png") out_path.parent.mkdir(parents=True, exist_ok=True) Image.fromarray(adjusted).convert('L').save(out_path, format='PNG', compress_level=5) subfolder = str(path.parent.relative_to(Config.INPUT_FOLDER)) filename = path.name if modal_black: image_processor.black_results[subfolder].append((filename, modal_black)) if modal_white != 255: image_processor.white_results[subfolder].append((filename, modal_white)) return out_path, modal_black, modal_white except (ValueError, OSError) as e: log_error(f"Error processing grayscale image {path}: {e}") return path, 0, 255 - Assistant
def process_grayscale_image( img: np.ndarray, path: Path, image_processor: 'ImageProcessor', temp_folder: Path ) -> Tuple[Path, int, int]: """Adjust grayscale image values and save the processed image.""" try: adjusted, modal_black, modal_white = adjust_values(img) relative_parent = path.parent.relative_to(Config.INPUT_FOLDER) out_path = temp_folder / relative_parent / f"{path.stem}.png" out_path.parent.mkdir(parents=True, exist_ok=True) Image.fromarray(adjusted).convert('L').save(out_path, format='PNG', compress_level=5) subfolder = relative_parent.as_posix() filename = path.name if modal_black: image_processor.black_results[subfolder].append((filename, modal_black)) if modal_white != 255: image_processor.white_results[subfolder].append((filename, modal_white)) return out_path, modal_black, modal_white except (ValueError, OSError) as e: log_error(f"Error processing grayscale image {path}: {e}") return path, 0, 255