How to binarize an image
How to binarize an image
To transform an image to a black-and-white binary form you can use the binarization method with fixed threshold BinarizeFixed, so, in this case, you need to indicate a threshold in a range of 0 and 255. All pixels of the image that have an intensity greater than the selected threshold will be assigned to value 255 (black color), or 0 (white color) otherwise. The more precise method is a `Bradly` method BinarizeBradley is used to improve binarization quality. The method calculates the threshold adaptively based on the average intensity estimation of the surrounding area with size `s x s`. The resulting picture will have more smooth edges in this case. Also, you can use Otsu method (`BinarizeOtsu`) with an automatic threshold.
from aspose.imaging import * | |
from aspose.imaging.fileformats.bmp import * | |
from aspose.imaging.fileformats.dicom import * | |
from aspose.imaging.fileformats.emf import * | |
from aspose.imaging.fileformats.jpeg import * | |
from aspose.imaging.fileformats.jpeg2000 import * | |
from aspose.imaging.fileformats.png import * | |
from aspose.imaging.fileformats.psd import * | |
from aspose.imaging.fileformats.tiff.enums import * | |
from aspose.imaging.imagefilters.filteroptions import * | |
from aspose.imaging.imageoptions import * | |
from aspose.imaging.masking import * | |
from aspose.imaging.masking.options import * | |
from aspose.imaging.masking.result import * | |
from aspose.imaging.sources import * | |
from aspose.pycore import as_of, is_assignable | |
import os | |
if 'TEMPLATE_DIR' in os.environ: | |
templates_folder = os.environ['TEMPLATE_DIR'] | |
else: | |
templates_folder = r"C:\Users\USER\Downloads\templates" | |
delete_output = 'SAVE_OUTPUT' not in os.environ | |
def binarize_otsu(): | |
filter_images(lambda image: image.binarize_otsu(), "binarizeotsu") | |
def binarize_bradley(): | |
filter_images(lambda image: image.binarize_bradley(0.5), "binarizebradley") | |
def binarize_fixed(): | |
filter_images(lambda image: image.binarize_fixed(70), "binarizefixed") | |
def filter_images(do_filter, filter_name): | |
obj_init = [] | |
obj_init.append("jpg") | |
obj_init.append("png") | |
obj_init.append("bmp") | |
obj_init.append("apng") | |
obj_init.append("dicom") | |
obj_init.append("jp2") | |
obj_init.append("j2k") | |
obj_init.append("tga") | |
obj_init.append("webp") | |
obj_init.append("tif") | |
obj_init.append("gif") | |
obj_init.append("ico") | |
raster_formats = obj_init | |
obj_init2 = [] | |
obj_init2.append("svg") | |
obj_init2.append("otg") | |
obj_init2.append("odg") | |
obj_init2.append("eps") | |
obj_init2.append("wmf") | |
obj_init2.append("emf") | |
obj_init2.append("wmz") | |
obj_init2.append("emz") | |
obj_init2.append("cmx") | |
obj_init2.append("cdr") | |
vector_formats = obj_init2 | |
all_formats = [] | |
all_formats.extend(raster_formats) | |
all_formats.extend(vector_formats) | |
for format_ext in all_formats: | |
input_file = os.path.join(templates_folder, f"template.{format_ext}") | |
if not os.path.exists(input_file): | |
continue | |
is_vector_format = format_ext in vector_formats | |
if is_vector_format: | |
input_file = rasterize_vector_image(format_ext, input_file) | |
output_file = os.path.join(templates_folder, f"{filter_name}_{format_ext}.png") | |
print(format_ext) | |
# explicit type casting from Image to RasterImage | |
with as_of(Image.load(input_file), RasterImage) as image: | |
do_filter(image) | |
multi_page = None | |
# if image implements an IMultipageImage interface | |
if is_assignable(image, IMultipageImage): | |
multi_page = as_of(image, IMultipageImage) | |
if multi_page is not None and multi_page.page_count > 1: | |
# for loop | |
for_first_step = True | |
page_index = 0 | |
for page in multi_page.pages: | |
file_name = f"{filter_name}_page{page_index}_{format_ext}.png" | |
page.save(templates_folder + file_name, PngOptions()) | |
os.remove(templates_folder + file_name) | |
page_index += 1 | |
else: | |
image.save(output_file, PngOptions()) | |
delete_file(output_file) | |
if is_vector_format: | |
delete_file(input_file) | |
def rasterize_vector_image(format_ext, input_file): | |
output_file = os.path.join(templates_folder, f"rasterized.{format_ext}.png") | |
with Image.load(input_file) as image: | |
image.save(output_file, PngOptions()) | |
return output_file | |
def delete_file(file): | |
if delete_output: | |
os.remove(file) | |
# Run filters | |
binarize_fixed() | |
# binarize_bradley() | |
# binarize_otsu() |