o1-mini-2024-09-12· OpenAI
WildChat-4.8M (AI2)
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 resize_image(input_path: Path, output_path: Path): """Resize an image using ImageMagick.""" output_path = output_path.with_suffix('.png')
identify_command = [ str(Config.IMAGEMAGICK), str(input_path), '-format', '%wx%h', 'info:' ] try: result = subprocess.run(identify_command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True, check=True) current_width, current_height = map(int, result.stdout.strip().split('x')) except subprocess.CalledProcessError as e: log_error(f"ImageMagick identify failed for {input_path}: {e.stderr.strip()}") return output_path resize_mode = app.RESIZE_MODE.get() if resize_mode == "Fit": target_width = int(app.RESIZE_WIDTH.get()) if app.RESIZE_WIDTH.get() else 0 target_height = int(app.RESIZE_HEIGHT.get()) if app.RESIZE_HEIGHT.get() else 0 if app.maintain_aspect_ratio.get(): if target_width == 0: target_width = int(target_height * (current_width / current_height)) elif target_height == 0: target_height = int(target_width * (current_height / current_width)) resize_arg = f'{target_width}x{target_height}!' else: # Shortest Side target_size = int(app.RESIZE_SIDE.get()) resize_arg = f'{target_size}x{target_size}^' is_upscaling = (resize_mode == "Fit" and (target_width > current_width or target_height > current_height)) or \ (resize_mode == "Shortest Side" and target_size > min(current_width, current_height)) if is_upscaling: command = [ str(Config.IMAGEMAGICK), str(input_path), '-filter', 'LanczosSharp', '-distort', 'Resize', resize_arg, ] else: command = [ str(Config.IMAGEMAGICK), str(input_path), '-colorspace', 'RGB', '-filter', 'Lanczos2Sharp', '-resize', resize_arg, '-colorspace', 'sRGB', ] command.extend(['-define', 'png:compression-level=5']) command.append(str(output_path)) try: result = subprocess.run(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True, check=True) if result.stderr: log_error(f"ImageMagick: {result.stderr.strip()}") except subprocess.CalledProcessError as e: log_error(f"ImageMagick resize failed for {input_path}: {e.stderr.strip()}") return output_path - Assistant