This Python program automates the process of cropping multiple images in a folder. It uses the Python Imaging Library (Pillow) to crop images based on specific criteria and provides several customizable options.
- Processes images in multiple formats:
.jpg
,.jpeg
,.png
. - Crops images into two parts:
- The left part, excluding a margin on the left.
- The right part, excluding a margin on the right.
- Allows customization of the margin percentage for cropping.
- Saves cropped images in a specified output folder.
- Skips processing if cropped files already exist.
- Provides detailed logging for processed files and errors.
- Command-line arguments for flexibility:
- Specify the input folder path (defaults to the current directory if not provided).
- Specify the output folder path.
- Set the margin percentage for cropping.
- The program scans a folder for images containing the letter "a" in their filenames.
- For each image:
- It calculates a margin based on the specified percentage.
- Crops the image into two parts:
- The left crop: From the margin to the middle of the image.
- The right crop: From the middle of the image to the width minus the margin.
- Saves the cropped images with new filenames:
- Replaces "a" with "b" for the left crop.
- Replaces "a" with "c" for the right crop.
-
Install the required library:
pip install pillow
-
Run the script with the following command:
python crop_images.py [folder_path] --output_folder <output_folder> --margin_percentage <margin_percentage>
[folder_path]
: (Optional) Path to the folder containing images. Defaults to the current directory if not provided.--output_folder
: (Optional) Path to save cropped images. Default isoutput
.--margin_percentage
: (Optional) Margin percentage for cropping. Default is7
.
-
Examples:
- Run the script in the current directory:
python crop_images.py
- Specify a custom folder path:
python crop_images.py "C:\Users\DANISH LAPTOP\Desktop\Crop Multiple Images Using Python"
- Specify a custom output folder:
python crop_images.py --output_folder "C:\Users\DANISH LAPTOP\Desktop\Cropped Images"
- Change the margin percentage:
python crop_images.py --margin_percentage 10
- Run the script in the current directory:
If the folder contains an image named examplea.jpg
, the program will generate:
exampleb.jpg
(left crop)examplec.jpg
(right crop)
- Python 3.x
- Pillow library
The program logs all actions, including:
- Files processed successfully.
- Errors encountered during processing.
Logs are displayed in the terminal for easy debugging.
This project is licensed under the MIT License.