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: Only use the is_color_image function to determine if an image is color or grayscale. Code: def is_color_image( img: np.ndarray, diff_threshold: int = Config.COLORDIFF_THRESHOLD, fraction_threshold: float = Config.FRACTION_COLORED_PIXELS_THRESHOLD ) -> bool: """Determine if an image is color based on RGB channel differences.""" # Check if the image has three channels (RGB) if img.ndim != 3 or img.shape[2] != 3: return False
code# 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 return fraction_colored >= fraction_thresholddef move_color_image_file(path, start_time, total_files, image_processor: 'ImageProcessor'): """Move a color image file to the output folder.""" if is_valid_image(path): try: img = np.array(open_and_convert_image(path)) if img.ndim == 3 and img.shape[2] == 3: if is_color_image(img, Config.COLORDIFF_THRESHOLD, Config.FRACTION_COLORED_PIXELS_THRESHOLD): rel_path = path.relative_to(Config.INPUT_FOLDER) out_path = Config.OUTPUT_FOLDER / rel_path out_path.parent.mkdir(parents=True, exist_ok=True) shutil.move(str(path), str(out_path)) subfolder = str(path.parent.relative_to(Config.INPUT_FOLDER)) filename = path.name image_processor.color_results[subfolder].append(filename) except (OSError, ValueError) as e: log_error(f"Error processing image {path}: {e}")
codewith shared_state.processed_files_lock: shared_state.processed_files_count += 1 update_status(start_time, shared_state.processed_files_count, total_files) - Assistant