Drawing Vector Images

Draw Vector Image

Using Aspose.Imaging for Python via .NET you can draw vector image on another vector image. With a few simple steps, you will be able to draw a vector image on another vector image.

  • Load a SVG image
  • Rasterize SVG to PNG and write the result to a file
  • Load a PNG image from file for further drawing
  • Draw PNG image on existing SVG image
  • Save the results
import aspose.pycore as aspycore
from aspose.imaging import RasterImage, Image, Point, Size, SizeF
from aspose.imaging.fileformats.svg import SvgImage
from aspose.imaging.fileformats.svg.graphics import SvgGraphics2D
from aspose.imaging.imageoptions import SvgRasterizationOptions, PngOptions
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
data_dir = templates_folder
out_file = os.path.join(data_dir, "output_temp.png")
# First, rasterize Svg to Png and write the result to a file.
with aspycore.as_of(Image.load(os.path.join(data_dir, "template.svg")), SvgImage) as svg_image:
rasterization_options = SvgRasterizationOptions()
rasterization_options.page_size = aspycore.cast(SizeF, svg_image.size)
save_options = PngOptions()
save_options.vector_rasterization_options = rasterization_options
svg_image.save(out_file, save_options)
with aspycore.as_of(Image.load(out_file), RasterImage) as image_to_draw:
# Drawing on the existing Svg image.
graphics = SvgGraphics2D(svg_image)
# Scale down the entire drawn image by 2 times and draw it to the center of the drawing surface.
width = image_to_draw.width // 2
height = image_to_draw.height // 2
origin = Point((svg_image.width - width) // 2, (svg_image.height - height) // 2)
size = Size(width, height)
graphics.draw_image(image_to_draw, origin, size)
# Save the result image
with graphics.end_recording() as result_image:
result_image.save(os.path.join(data_dir, "result.svg"))
if delete_output:
os.remove(out_file)
os.remove(os.path.join(data_dir, "result.svg"))