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 is_color_image( img: np.ndarray, diff_threshold: int = Config.COLORDIFF_THRESHOLD, fraction_threshold: float = Config.FRACTION_COLORED_PIXELS_THRESHOLD ) -> Tuple[bool, Optional[Tuple]]: """Determine if an image is color based on RGB channel differences or its dimensions.""" # Check if the image is inherently grayscale based on dimensions if img.ndim == 2 or (img.ndim == 3 and img.shape[2] == 1): adjusted, modal_black, modal_white = adjust_values(img) return False, (adjusted, modal_black, modal_white)
# Check if the image has three channels (RGB) if img.ndim != 3 or img.shape[2] != 3: return False, None # Or handle differently based on your requirements # Calculate the peak-to-peak (max - min) difference across the RGB channels for each pixel max_diff = np.ptp(img, axis=2) # Compute the fraction of pixels where the max difference exceeds the threshold fraction_colored = np.mean(max_diff > diff_threshold) # Determine if the image meets or exceeds the required fraction of colored pixels is_color = fraction_colored >= fraction_threshold if is_color: return True, None else: # If not color, treat as grayscale adjusted, modal_black, modal_white = adjust_values(img) return False, (adjusted, modal_black, modal_white) - Assistant
from typing import Optional, Tuple import numpy as np def is_color_image( img: np.ndarray, diff_threshold: int = Config.COLORDIFF_THRESHOLD, fraction_threshold: float = Config.FRACTION_COLORED_PIXELS_THRESHOLD ) -> Tuple[bool, Optional[Tuple]]: """Determine if an image is color based on RGB channel differences or its dimensions.""" if img.ndim == 2 or (img.ndim == 3 and img.shape[2] == 1): adjusted, modal_black, modal_white = adjust_values(img) return False, (adjusted, modal_black, modal_white) if img.ndim != 3 or img.shape[2] != 3: return False, None max_diff = np.ptp(img, axis=2) fraction_colored = np.mean(max_diff > diff_threshold) is_color = fraction_colored >= fraction_threshold if is_color: return True, None adjusted, modal_black, modal_white = adjust_values(img) return False, (adjusted, modal_black, modal_white)
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.