A script for cropping words or characters from text-based images.
Can be used for both printed texts and handwritten texts.
For example:
# Creates a new image object.
img = tis('example.png')
# Crops words from text and saves them to 'cropped_words' directory.
img.crop_text(output_dir='cropped_words', iterations=5)
# Draws rectangles around all words in the text, and saves the result to
# 'result_rec.png'.
img.draw_rectangles(output_path='result_rec.png', iterations=5)
This script uses cv2 package.
The algorithm for finding the words in a text-based image is demonstrated in the following chart:
draw_rectangles(output_path, iterations=5, dilate=True)
This function draws rectangles around the words in the text. With this function, you can see how ‘iterations’ variables affect the scripts’ segmentation performance. Can be convenient for long texts.
Parameters:
-
output_path (string): A path to save the image to. If None is given as a parameter, image will be saved in the original image parent directory.
-
iterations (int): Number of dilation iterations that will be done on the image. Default value is set to 5.
-
dilate (bool): Whether to dilate the text in the image or not. Default is set to 'True'. It is recommended to dilate the image for better segmentation.
Returns: None. Saves the image in the output path.
For example:
# Creates a new image object.
img = tis(‘example.png')
# Draws rectangles around all words in the text, and saves the result to
# 'result_rec.png'.
img.draw_rectangles(output_path='result_rec.png', iterations=5)
Result:
Higher number of iteration is used for text segmentation, while lower number is used for characters segmentation.
For example:
# Creates a new image object.
img = tis('example.png')
# Draws rectangles around all words in the text, and saves the result to
# 'result_rec.png'.
img.draw_rectangles(output_path='result_rec.png', iterations=1)
Result:
crop_text(output_dir, iterations=5, dilate=True):
This function crops the words from the text and saves them in separate png images.
Parameters:
-
output_dir (string): A path to save the image to. If None is given as a parameter, images will be saved in a directory called "text_segmentation" inside the original image's parent directory.
-
iterations (int): Number of dilation iterations that will be done on the image. Default value is set to 5.
-
dilate (bool): Whether to dilate the text in the image or not. Default is set to 'True'. It is recommended to dilate the image for better segmentation.
Returns: None. Saves images of all words from the text in the output path.
For example:
# Creates a new image object.
img = tis('example.png')
# Crops words from text and saves them to 'cropped_words' directory.
img.crop_text(output_dir='cropped_words', iterations=5)
Result:
- NumPy
- cv2