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 convert_png_to_jpg(input_path: Path, output_path: Path): """Convert a PNG image to JPG format.""" try: with Image.open(input_path) as img: # Convert image to 'RGB' if it's not already in 'RGB' or 'L' mode if img.mode not in ('RGB', 'L'): img = img.convert('RGB')
jpg_path = output_path.with_suffix('.jpg') img.save(jpg_path, 'JPEG', quality=80, subsampling=0) input_path.unlink() except Exception as e: log_error(f"Error converting {input_path} to JPG: {e}") - Assistant
from pathlib import Path from PIL import Image import logging def convert_png_to_jpg(input_path: Path, output_path: Path) -> None: """Convert a PNG image to JPG format.""" try: with Image.open(input_path) as img: if img.mode not in ('RGB', 'L'): img = img.convert('RGB') jpg_path = output_path.with_suffix('.jpg') img.save(jpg_path, format='JPEG', quality=80, subsampling=0) input_path.unlink() except (IOError, OSError) as error: logging.error("Error converting %s to JPG: %s", input_path, error)Refactoring Changes: