From 86ed4932b88a4f6226d0a3e536c89ae1c5175f0e Mon Sep 17 00:00:00 2001 From: Bigsk Date: Sun, 29 Sep 2024 17:23:39 +0800 Subject: [PATCH] tidy up and added a script to help you cut img into grid --- https:/git.ghink.net/bigsk => age_calc.py | 0 split_img_to_grid.py | 34 +++++++++++++++++++++++ 2 files changed, 34 insertions(+) rename https:/git.ghink.net/bigsk => age_calc.py (100%) create mode 100644 split_img_to_grid.py diff --git a/https:/git.ghink.net/bigsk b/age_calc.py similarity index 100% rename from https:/git.ghink.net/bigsk rename to age_calc.py diff --git a/split_img_to_grid.py b/split_img_to_grid.py new file mode 100644 index 0000000..a9699dc --- /dev/null +++ b/split_img_to_grid.py @@ -0,0 +1,34 @@ +import os + +from PIL import Image + +def split_image_to_grid(image_path, output_folder): + img = Image.open(image_path) + basename = os.path.basename(image_path).split(".")[0] + + width, height = img.size + if width != height: + raise ValueError("The picture must be square!") + + grid_size = width // 3 + + for row in range(3): + for col in range(3): + left = col * grid_size + top = row * grid_size + right = left + grid_size + bottom = top + grid_size + + grid_img = img.crop((left, top, right, bottom)) + os.makedirs(output_folder, exist_ok=True) + grid_img.save(f"{output_folder}/{basename}_{row}_{col}.png") + + print("Success!") + +def main(): + image_path = input("Please type in input file path:\n").replace("'", "") + output_folder = input("Please type in output dir path:\n").replace("'", "") + split_image_to_grid(image_path, output_folder) + +if __name__ == "__main__": + main() \ No newline at end of file