import argparse import os from PIL import Image def prepare_file_paths(image_path): # Get the parent directory of the original image parent_directory = os.path.dirname(os.path.dirname(image_path)) # Prepare the file paths for the split images base_filename, ext = os.path.splitext(os.path.basename(image_path)) left_image_path = os.path.join( parent_directory, "left", base_filename + "_left" + ext ) right_image_path = os.path.join( parent_directory, "right", base_filename + "_right" + ext ) return left_image_path, right_image_path def is_image_split(image_path): # Prepare the file paths for the split images using the function left_image_path, right_image_path = prepare_file_paths(image_path) # Check if the split images exist return os.path.exists(left_image_path) and os.path.exists(right_image_path) def split_image(image_path): # Open the image file img = Image.open(image_path) # Get the image size width, height = img.size # Calculate the width of each split split_width = width // 2 img1 = img.crop((0, 0, split_width, height)) img2 = img.crop((split_width, 0, width, height)) # Get the parent directory of the original image parent_directory = os.path.dirname(os.path.dirname(image_path)) # Create a new directory to save the split images left_dir = os.path.join(parent_directory, "left") right_dir = os.path.join(parent_directory, "right") # Create the directories if they don't exist os.makedirs(left_dir, exist_ok=True) os.makedirs(right_dir, exist_ok=True) # Prepare the file paths for the split images using the function left_image_path, right_image_path = prepare_file_paths(image_path) # Save the split images img1.save(left_image_path) img2.save(right_image_path) def main(): parser = argparse.ArgumentParser(description="Split an image into two halves") parser.add_argument("image_path", help="Path to the image file") args = parser.parse_args() if is_image_split(args.image_path): print("The image is already split.") else: split_image(args.image_path) if __name__ == "__main__": main()