From 55583b3afcb7739681dbc8fac1de81c678aeff95 Mon Sep 17 00:00:00 2001 From: Andreas Fahrecker Date: Wed, 20 Mar 2024 06:47:36 +0100 Subject: [PATCH] Check if image is already split --- main.py | 39 ++++++++++++++++++++++++++++++++------- 1 file changed, 32 insertions(+), 7 deletions(-) diff --git a/main.py b/main.py index c4f1c07..c402086 100644 --- a/main.py +++ b/main.py @@ -4,6 +4,30 @@ 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) @@ -27,21 +51,22 @@ def split_image(image_path): os.makedirs(left_dir, exist_ok=True) os.makedirs(right_dir, exist_ok=True) - # Prepare the file paths for the split images - base_filename, ext = os.path.splitext(os.path.basename(image_path)) - img1_path = os.path.join(left_dir, base_filename + "_left" + ext) - img2_path = os.path.join(right_dir, base_filename + "_right" + ext) + # 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(img1_path) - img2.save(img2_path) + 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() - split_image(args.image_path) + if is_image_split(args.image_path): + print("The image is already split.") + else: + split_image(args.image_path) if __name__ == "__main__":