From 55583b3afcb7739681dbc8fac1de81c678aeff95 Mon Sep 17 00:00:00 2001 From: Andreas Fahrecker Date: Wed, 20 Mar 2024 06:47:36 +0100 Subject: [PATCH 1/5] 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__": -- 2.47.2 From a74be96f476c6d36c05711c2dac5f2819190c046 Mon Sep 17 00:00:00 2001 From: Andreas Fahrecker Date: Wed, 20 Mar 2024 06:55:39 +0100 Subject: [PATCH 2/5] 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__": -- 2.47.2 From cc786484d71a1da91ae0abfc819a942b0ef19d73 Mon Sep 17 00:00:00 2001 From: Andreas Fahrecker Date: Wed, 20 Mar 2024 07:07:58 +0100 Subject: [PATCH 3/5] Improved and Updated README.md --- README.md | 31 +++++++++++++++++++++++++++++-- 1 file changed, 29 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 55d63ee..a9c9ee9 100644 --- a/README.md +++ b/README.md @@ -37,14 +37,41 @@ pip install -r requirements.txt ``` ## Usage -Run the script with the path to the image you want to split as an argument: +### As Windows Executable +You can run the program with either the path to a single image you want to split or a directory containing multiple images. + +#### Single Image Mode ```sh -python main.py path_to_your_image.jpg +main.exe --image_path path_to_your_image.jpg +``` +The program will create two new images in the "left" and "right" directories with the suffixes _left and _right added to the original file name. + +#### Directory Mode +```sh +main.exe --directory path_to_your_directory ``` +The program will iterate over all images in the directory, and for each image, it will create two new images in the "left" and "right" directories with the suffixes _left and _right added to the original file name. + +### As a Python Script +You can run the script with either the path to a single image you want to split or a directory containing multiple images. + +To split a single image: + +```sh +python main.py --image_path path_to_your_image.jpg +``` The script will create two new images in the "left" and "right" directories with the suffixes _left and _right added to the original file name. +To split all images in a directory: + +```sh +python main.py --directory path_to_your_directory +``` + +The script will iterate over all images in the directory, and for each image, it will create two new images in the "left" and "right" directories with the suffixes _left and _right added to the original file name. + ## License This project is licensed under the terms of the MIT license. -- 2.47.2 From 042152031683ffe1da6513cc3d94e2ef9591dbd4 Mon Sep 17 00:00:00 2001 From: Andreas Fahrecker Date: Wed, 20 Mar 2024 07:12:23 +0100 Subject: [PATCH 4/5] Updated README.md --- README.md | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/README.md b/README.md index a9c9ee9..c0d60ec 100644 --- a/README.md +++ b/README.md @@ -29,6 +29,11 @@ The "left" directory contains the left half of the original image, and the "righ ## Installation +### For Usage as a Windows Executable +You can either download a prebuilt executable from the releases section of the repository, or you can build it yourself the building instructions are further down the `README.md` + +### For Usage as a Python Script + 1. Clone this repository. 2. Install the required Python packages using pip: -- 2.47.2 From 920c637409e6cd4b74e35052ac3326f0f2e3d380 Mon Sep 17 00:00:00 2001 From: Andreas Fahrecker Date: Wed, 20 Mar 2024 17:42:18 +0100 Subject: [PATCH 5/5] Added Build from Source Section in README.md --- README.md | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/README.md b/README.md index c0d60ec..3015bcb 100644 --- a/README.md +++ b/README.md @@ -77,6 +77,24 @@ python main.py --directory path_to_your_directory The script will iterate over all images in the directory, and for each image, it will create two new images in the "left" and "right" directories with the suffixes _left and _right added to the original file name. +## Build from Source + +### Windows Executable + +1. Clone this repository. +2. Navigate into the cloned repository. +```sh +cd image-splitter +``` +3. Install the required Python packages. +```sh +pip install -r requirements.txt +``` +4. Build the Executable with PyInstaller +```sh +pyinstaller --onefile --name image-splitter main.py +``` + ## License This project is licensed under the terms of the MIT license. -- 2.47.2