This Python script is designed to detect and crop the borders of images stored in a specified folder. It can be useful for removing unwanted white or blank margins from scanned documents or images.
Before using this script, you need to have the following dependencies installed:
- OpenCV (cv2):
pip install opencv-python
-
Clone or download this repository to your local machine.
-
Place the images you want to process in a folder of your choice. You can set the input folder by modifying the
input_folder
variable in the script. -
Optionally, specify the output folder where the cropped images will be saved by modifying the output_folder variable in the script. If not provided, it defaults to a folder with the same name as the input folder but prefixed with "cropped_".
-
Optionally, you can set target dimensions for the cropped images by modifying the
target_width
andtarget_height
variables. If you want to keep the original dimensions, set these variables toNone
. -
Run the script:
python remove_black_borders.py
- The script will now process all the supported files in the input folder, detect and crop borders from all sides, and save the cropped images in the output folder.
- The
white_threshold
parameter in the BorderAnalyzer class has been set to 85 to enhance the detection of borders, offering improved separation between the actual content and the surrounding borders, even in images with intricate patterns or not very light details. - In image processing, pixel values typically range from 0 to 255, where 0 represents black and 255 represents white. The
white_threshold
parameter is used to determine how light a pixel must be to be considered part of a white border. For example,white_threshold
value closer to 255 would make the script more sensitive to white and very light colours.
In the provided script, the default input_folder
is set to "book," and the default output_folder
is set to "cropped_book." You can replace these values with your own folder paths.
input_folder = "your_input_folder"
output_folder = "your_output_folder"
Please replace "your_input_folder"
and "your_output_folder"
with the actual folder paths you want to use.
This script is licensed under the MIT License.