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 analyze_file(path, start_time, total): """Analyze a single image file using RGB differences to determine color status.""" if is_valid_image(path): try: img = np.array(open_and_convert_image(path)) if img.ndim == 2 or (img.ndim == 3 and img.shape[2] == 1): # Grayscale image adjusted, modal_black, modal_white = adjust_values(img)
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)) else: # Determine if the image is color or grayscale based on RGB differences if is_color_image(img, Config.COLORDIFF_THRESHOLD, Config.FRACTION_COLORED_PIXELS_THRESHOLD): subfolder = str(path.parent.relative_to(Config.INPUT_FOLDER)) filename = path.name image_processor.color_results[subfolder].append(filename) else: adjusted, modal_black, modal_white = adjust_values(img) 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)) except (OSError, ValueError) as e: log_error(f"Error processing image {path}: {e}") with shared_state.processed_files_lock: shared_state.processed_files_count += 1 update_status(start_time, shared_state.processed_files_count, total) - Assistant
def analyze_file(path, start_time, total): """Analyze a single image file to determine its color status based on RGB differences.""" if not is_valid_image(path): return try: img = np.array(open_and_convert_image(path)) subfolder = str(path.parent.relative_to(Config.INPUT_FOLDER)) filename = path.name if is_grayscale_image(img): adjusted, modal_black, modal_white = adjust_values(img) 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)) elif is_color_image(img, Config.COLORDIFF_THRESHOLD, Config.FRACTION_COLORED_PIXELS_THRESHOLD): image_processor.color_results[subfolder].append(filename) else: adjusted, modal_black, modal_white = adjust_values(img) 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)) except (OSError, ValueError) as e: log_error(f"Error processing image {path}: {e}") finally: with shared_state.processed_files_lock: shared_state.processed_files_count += 1 update_status(start_time, shared_state.processed_files_count, total) def is_grayscale_image(img: np.ndarray) -> bool: """Check if the image is grayscale based on its dimensions.""" return img.ndim == 2 or (img.ndim == 3 and img.shape[2] == 1)