From a74be96f476c6d36c05711c2dac5f2819190c046 Mon Sep 17 00:00:00 2001 From: Andreas Fahrecker Date: Wed, 20 Mar 2024 06:55:39 +0100 Subject: [PATCH] split image function --- main.py | 33 ++++++++++++++++++++++++++++----- 1 file changed, 28 insertions(+), 5 deletions(-) diff --git a/main.py b/main.py index c402086..2ed2385 100644 --- a/main.py +++ b/main.py @@ -59,14 +59,37 @@ def split_image(image_path): img2.save(right_image_path) +def split_images_in_directory(directory): + # Get the list of files in the directory + files = os.listdir(directory) + + # Filter the list to get only the image files + image_files = [f for f in files if f.endswith(".jpg") or f.endswith(".png")] + + # Split each image + for image_file in image_files: + image_path = os.path.join(directory, image_file) + if not is_image_split(image_path): + split_image(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") + parser = argparse.ArgumentParser( + description="Split an image or all images in a directory into two halves" + ) + parser.add_argument("--image_path", help="Path to the image file") + parser.add_argument("--directory", help="Path to the directory containing images") args = parser.parse_args() - if is_image_split(args.image_path): - print("The image is already split.") + + if args.image_path: + if is_image_split(args.image_path): + print("The image is already split.") + else: + split_image(args.image_path) + elif args.directory: + split_images_in_directory(args.directory) else: - split_image(args.image_path) + print("Please provide either --image_path or --directory") if __name__ == "__main__":