Refactor (is the else after return unnessecary?). def adjustandsaveimage( img:…
- User
Refactor (is the else after return unnessecary?). def adjust_and_save_image( img: np.ndarray, path: Path, image_processor: 'ImageProcessor', temp_folder: Path, diff_threshold: int = Config.COLORDIFF_THRESHOLD, fraction_threshold: float = Config.FRACTION_COLORED_PIXELS_THRESHOLD ) -> tuple[Path, Optional[int], Optional[int]]: """Process an image, adjusting and saving depending on whether it is color or grayscale.""" try: is_color = is_color_image(img, diff_threshold, fraction_threshold) relative_parent = path.parent.relative_to(Config.INPUT_FOLDER) out_path = temp_folder / relative_parent / f"{path.stem}.png" out_path.parent.mkdir(parents=True, exist_ok=True) subfolder = relative_parent.as_posix() filename = path.name
if is_color: # Save color image as PNG Image.fromarray(img).save(out_path, format='PNG', compress_level=5) image_processor.color_results[subfolder].append(filename) return out_path, None, None else: # Adjust grayscale values adjusted_img, modal_black, modal_white = adjust_values(img) Image.fromarray(adjusted_img).convert('L').save(out_path, format='PNG', compress_level=5) 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)) return out_path, modal_black, modal_white except (OSError, ValueError) as e: log_error(f"Error processing image {path}: {e}") return path, None, None - Assistant