| import os |
| from PIL import Image |
|
|
| def split_images(): |
| control_dir = "control_imgs" |
| if not os.path.exists(control_dir): |
| print(f"Error: Directory '{control_dir}' does not exist.") |
| return |
|
|
| |
| front_dir = os.path.join(control_dir, "front") |
| back_dir = os.path.join(control_dir, "back") |
| |
| |
| os.makedirs(front_dir, exist_ok=True) |
| os.makedirs(back_dir, exist_ok=True) |
|
|
| |
| files = os.listdir(control_dir) |
| image_extensions = ('.png', '.jpg', '.jpeg', '.webp') |
| |
| |
| target_files = [ |
| f for f in files |
| if os.path.isfile(os.path.join(control_dir, f)) |
| and f.lower().endswith(image_extensions) |
| ] |
| |
| if not target_files: |
| print("No target images found to split.") |
| return |
|
|
| print(f"Found {len(target_files)} images to split.") |
| |
| success_count = 0 |
| for filename in target_files: |
| img_path = os.path.join(control_dir, filename) |
| try: |
| with Image.open(img_path) as img: |
| width, height = img.size |
| |
| |
| left_half = img.crop((0, 0, width // 2, height)) |
| right_half = img.crop((width // 2, 0, width, height)) |
| |
| |
| front_path = os.path.join(front_dir, filename) |
| back_path = os.path.join(back_dir, filename) |
| |
| |
| left_half.save(front_path) |
| right_half.save(back_path) |
| |
| |
| os.remove(img_path) |
| print(f"Successfully split: {filename} -> front/{filename} and back/{filename}") |
| success_count += 1 |
| except Exception as e: |
| print(f"Error processing {filename}: {e}") |
| |
| print(f"\nProcessing complete! Successfully split {success_count}/{len(target_files)} images.") |
|
|
| if __name__ == "__main__": |
| split_images() |
|
|