42 lines
1.2 KiB
Python
42 lines
1.2 KiB
Python
import sys
|
|
from PIL import Image
|
|
|
|
def split_image(image_path, output_prefix):
|
|
try:
|
|
img = Image.open(image_path)
|
|
width, height = img.size
|
|
|
|
target_height = width // 3
|
|
if target_height > height:
|
|
target_height = height
|
|
target_width = height * 3
|
|
left = (width - target_width) // 2
|
|
top = 0
|
|
right = left + target_width
|
|
bottom = height
|
|
else:
|
|
target_width = width
|
|
left = 0
|
|
top = (height - target_height) // 2
|
|
right = width
|
|
bottom = top + target_height
|
|
|
|
img_cropped = img.crop((left, top, right, bottom))
|
|
|
|
sq_size = target_width // 3
|
|
|
|
for i in range(3):
|
|
box = (i * sq_size, 0, (i + 1) * sq_size, sq_size)
|
|
part = img_cropped.crop(box)
|
|
part.save(f"{output_prefix}_{i+1}.png")
|
|
|
|
print("Success")
|
|
except Exception as e:
|
|
print(f"Error: {str(e)}")
|
|
|
|
if __name__ == "__main__":
|
|
if len(sys.argv) != 3:
|
|
print("Usage: split_image.py <input_img> <output_prefix>")
|
|
else:
|
|
split_image(sys.argv[1], sys.argv[2])
|