How to Generate Raster Images in Python

How to Generate Raster Images in Python

How to Generate Raster Images in Python

Pass ImageSaveOptions to to_image() on PsDocument or XpsDocument to export as a raster image. ImageSaveOptions accepts a format string ("png" or "jpeg") and a dpi integer. The method returns bytes containing the encoded image. The default DPI is 96; use 150 or 300 for higher-resolution output.

PNG Output from PS

from pathlib import Path
from aspose.page.ps.document import PsDocument
from aspose.page.ps.output import ImageSaveOptions

ps = PsDocument.from_file("input.ps")
opts = ImageSaveOptions(format="png", dpi=150)
Path("output.png").write_bytes(ps.to_image(opts))

JPEG Output from XPS

from pathlib import Path
from aspose.page.xps.document import XpsDocument
from aspose.page.ps.output import ImageSaveOptions

xps = XpsDocument.from_file("input.xps")
opts = ImageSaveOptions(format="jpeg", dpi=96)
Path("output.jpg").write_bytes(xps.to_image(opts))

Set DPI

Use the dpi property of ImageSaveOptions:

from aspose.page.ps.output import ImageSaveOptions

high_res = ImageSaveOptions(format="png", dpi=300)
web_res = ImageSaveOptions(format="png", dpi=96)

See Also