Check if image is already split

This commit is contained in:
Andreas Fahrecker 2024-03-20 06:47:36 +01:00
parent ae0dc72b16
commit 55583b3afc

37
main.py
View File

@ -4,6 +4,30 @@ import os
from PIL import Image 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): def split_image(image_path):
# Open the image file # Open the image file
img = Image.open(image_path) img = Image.open(image_path)
@ -27,20 +51,21 @@ def split_image(image_path):
os.makedirs(left_dir, exist_ok=True) os.makedirs(left_dir, exist_ok=True)
os.makedirs(right_dir, exist_ok=True) os.makedirs(right_dir, exist_ok=True)
# Prepare the file paths for the split images # Prepare the file paths for the split images using the function
base_filename, ext = os.path.splitext(os.path.basename(image_path)) left_image_path, right_image_path = prepare_file_paths(image_path)
img1_path = os.path.join(left_dir, base_filename + "_left" + ext)
img2_path = os.path.join(right_dir, base_filename + "_right" + ext)
# Save the split images # Save the split images
img1.save(img1_path) img1.save(left_image_path)
img2.save(img2_path) img2.save(right_image_path)
def main(): def main():
parser = argparse.ArgumentParser(description="Split an image into two halves") parser = argparse.ArgumentParser(description="Split an image into two halves")
parser.add_argument("image_path", help="Path to the image file") parser.add_argument("image_path", help="Path to the image file")
args = parser.parse_args() args = parser.parse_args()
if is_image_split(args.image_path):
print("The image is already split.")
else:
split_image(args.image_path) split_image(args.image_path)