[docs]defsave_image(src:str,directory:str,i:int)->Optional[str]:"""Save an image file with proper format handling. Args: src: Source path of the image. directory: Directory to save the image to. i: Index of the image. Returns: Format of the image ('PNG' or 'TIFF') or None if format not supported. """withImage.open(src)asimg:format=img.formatifformat=="PNG":suffix="png"elifformat=="TIFF":suffix="tif"else:returnNonedest=os.path.join(directory,f"{i}.{suffix}")try:# Try to create a hard link firstos.link(src,dest)exceptOSError:# If linking fails, try to copy the filetry:shutil.copy2(src,dest)exceptExceptionase:raiseRuntimeError(f"failed to copy from '{src}' to '{dest}': {str(e)}")returnformat