Check if image is already split
This commit is contained in:
		
							
								
								
									
										37
									
								
								main.py
									
									
									
									
									
								
							
							
						
						
									
										37
									
								
								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,20 +51,21 @@ 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() | ||||
|     if is_image_split(args.image_path): | ||||
|         print("The image is already split.") | ||||
|     else: | ||||
|         split_image(args.image_path) | ||||
|  | ||||
|  | ||||
|   | ||||
		Reference in New Issue
	
	Block a user