pixel_to_new_dpi method

pixel_to_new_dpi(pixels, old_dpi, new_dpi)

Converts pixels from one resolution to another.

def pixel_to_new_dpi(self, pixels: float, old_dpi: float, new_dpi: float):
    ...
ParameterTypeDescription
pixelsfloatThe value to convert.
old_dpifloatThe current dpi (dots per inch) resolution.
new_dpifloatThe new dpi (dots per inch) resolution.

Examples

Shows how to use convert points to pixels with default and custom resolution.

doc = aw.Document()
builder = aw.DocumentBuilder(doc)

# Define the size of the top margin of this section in pixels, according to a custom DPI.
my_dpi = 192

page_setup = builder.page_setup
page_setup.top_margin = aw.ConvertUtil.pixel_to_point(100, my_dpi)

self.assertAlmostEqual(37.5, page_setup.top_margin, delta=0.01)

# At the default DPI of 96, a pixel is 0.75 points.
self.assertEqual(0.75, aw.ConvertUtil.pixel_to_point(1))

builder.writeln(
    f"This Text is {page_setup.top_margin} points/{aw.ConvertUtil.point_to_pixel(page_setup.top_margin, my_dpi)} " +
    f"pixels (at a DPI of {my_dpi}) from the top of the page.")

# Set a new DPI and adjust the top margin value accordingly.
new_dpi = 300
page_setup.top_margin = aw.ConvertUtil.pixel_to_new_dpi(page_setup.top_margin, my_dpi, new_dpi)
self.assertAlmostEqual(59.0, page_setup.top_margin, delta=0.01)

builder.writeln(
    f"At a DPI of {new_dpi}, the text is now {page_setup.top_margin} points/{aw.ConvertUtil.point_to_pixel(page_setup.top_margin, my_dpi)} " +
    "pixels from the top of the page.")

doc.save(ARTIFACTS_DIR + "UtilityClasses.points_and_pixels_dpi.docx")

See Also