From f80074a9ab19152d444c2007edc8f22dd049c6cd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Robert=20H=C3=BCmmer?= Date: Tue, 20 Aug 2024 17:38:43 +0200 Subject: [PATCH] crop image helper added --- helpers/crop_images.py | 57 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 helpers/crop_images.py diff --git a/helpers/crop_images.py b/helpers/crop_images.py new file mode 100644 index 0000000..109a00b --- /dev/null +++ b/helpers/crop_images.py @@ -0,0 +1,57 @@ +"""crops all the images in a given directory (input_dir) into an output_dir +placed above the input_dir folder: + +Usage: + python crop_images.py /path/to/your/images --crop_box 50 50 200 200 + +""" + +import argparse +import os + +from PIL import Image + + +class ImageCropper: + def __init__(self, input_dir, x, y, width, height): + self.input_dir = input_dir + self.output_dir = os.path.abspath(os.path.join(input_dir, os.pardir)) + self.output_dir = os.path.join(self.output_dir, "cropped") + + # Convert x, y, width, height to (left, upper, right, lower) + self.crop_box = (x, y, x + width, y + height) + + # Create output directory if it doesn't exist + if not os.path.exists(self.output_dir): + os.makedirs(self.output_dir) + + def crop_images(self): + for filename in os.listdir(self.input_dir): + if filename.lower().endswith((".jpg", ".jpeg", ".png")): + image_path = os.path.join(self.input_dir, filename) + with Image.open(image_path) as img: + # Perform the crop + cropped_image = img.crop(self.crop_box) + + # Save the cropped image + cropped_image_path = os.path.join(self.output_dir, filename) + cropped_image.save(cropped_image_path) + + print(f"Cropped and saved: {filename}") + +def main(): + parser = argparse.ArgumentParser(description="Crop images in a directory and save them in a 'cropped' subdirectory.") + parser.add_argument("input_dir", type=str, help="Path to the input directory containing images.") + parser.add_argument("--crop_box", type=int, nargs=4, default=[100, 100, 400, 400], + help="Properties for the crop box (x, y, width, height).") + + args = parser.parse_args() + + cropper = ImageCropper(args.input_dir, *args.crop_box) + cropper.crop_images() + print(f"{cropper.input_dir=}") + print(f"{cropper.output_dir=}") + print(f"{cropper.crop_box=}") + +if __name__ == "__main__": + main()