How to dither an image

How to dither an image

         For editing an image with Dithering method you can specify `ThresholdDithering` or `FloydSteinbergDithering` options. The `ThresholdDitherig` is a more simple and fast dithering method, but the `FloydSteinbergDithering` is a more complex method that calculates nearest neighbors' pixels' intensity values for more smooth results. You can additionally indicate the color palette in bits for use for dithering. More bits mean a higher quality of the resulting image, but at the same time a larger image size:

from aspose.imaging import Image, RasterImage, DitheringMethod, IMultipageImage
from aspose.imaging.imageoptions import PngOptions
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 delete_file(file):
if delete_output:
os.remove(file)
def floyd_steinberg_dithering():
filter_images(lambda image: image.dither(DitheringMethod.FLOYD_STEINBERG_DITHERING, 4), "floydsteinbergdithering")
def threshold_dithering():
filter_images(lambda image: image.dither(DitheringMethod.THRESHOLD_DITHERING, 4), "thresholddithering")
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("tiff")
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 = raster_formats
all_formats.extend(vector_formats)
for format_ext in all_formats:
input_file = os.path.join(templates_folder, f"template.{format_ext}")
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 = os.path.join(
templates_folder,
f"{filter_name}_page{page_index}_{format_ext}.png")
page.save(file_name, PngOptions())
delete_file(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
# run
floyd_steinberg_dithering()
threshold_dithering()