Clean Up Drawing Scans with Python
This Python script serves as a tool to clean up scanned pen and ink illustrations. It applies a series of image processing techniques to enhance the quality of the input image and remove imperfections.
Requirements
To run the script, you need to have the following dependencies installed:
- OpenCV (
cv2
): A popular computer vision library. - NumPy (
numpy
): A powerful library for numerical computing. - argparse (
argparse
): A module for command-line argument parsing. - logging (
logging
): A module for logging messages during execution.
Usage
To use the script, execute it with the following command-line arguments:
python script_name.py input_image output_image [--debug]
input_image
: The path to the input pen and ink illustration image that requires cleaning.output_image
: The path where the cleaned image will be saved.--debug
: (Optional) Enable debug mode to receive more detailed logging information.
Image Cleaning Steps
- Image Loading: The script loads the input image in grayscale mode using OpenCV.

Test Image
- Gaussian Blur: A Gaussian blur is applied to the image to smoothen it and reduce noise.

Gaussian Blur
- Adaptive Thresholding: The script uses adaptive thresholding to dynamically threshold the blurred image. This technique helps to separate the foreground (pen and ink lines) from the background.

Otsu Threshold
- Morphological Operations:
- Opening: Morphological opening is performed to remove small dots and artifacts from the image.
- Closing: A closing operation is applied to close small gaps in the ink lines.

Opened

Closed
-
Image Inversion: The cleaned image is inverted to preserve the pen and ink lines while discarding the background.
-
Combining Images: The script performs a bitwise OR operation between the inverted cleaned image and the original image. This combines the cleaned ink lines with the original content, resulting in a cleaned and enhanced image.

Result
Example
python image_cleaning_script.py input.png output_cleaned.png --debug
This command will load the input.png
image, clean it using the defined techniques, and save the resulting cleaned image as output_cleaned.png
. Debugging messages will be displayed during the process.
Note: Ensure that you have the necessary permissions to read the input image and write the output cleaned image in the specified locations.