Drawing Raster Images on Vector Images

Draw Raster Image on SVG

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

  • Load a raster image using the factory method load(…) exposed by Image class.
  • Load a SVG image
  • Draw a rectangular part of the raster image within the specified bounds of the vector image
  • Save the results
import aspose.pycore as aspycore
from aspose.imaging import Image, RasterImage, Rectangle, GraphicsUnit
from aspose.imaging.fileformats.svg import SvgImage
from aspose.imaging.fileformats.svg.graphics import SvgGraphics2D
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
# Load the image to be drawn
with aspycore.as_of(Image.load(os.path.join(data_dir, "template.png")), RasterImage) as image_to_draw:
# Load the image for drawing on it (drawing surface)
with aspycore.as_of(Image.load(os.path.join(data_dir, "template.svg")), SvgImage) as canvas_image:
# Drawing on an existing Svg image.
graphics = SvgGraphics2D(canvas_image)
# Draw a rectagular part of the raster image within the specified bounds of the vector image (drawing surface).
# Note that because the source size is equal to the destination one, the drawn image is not stretched.
graphics.draw_image(Rectangle(0, 0, image_to_draw.width, image_to_draw.height), Rectangle(67, 67, image_to_draw.width, image_to_draw.height), image_to_draw)
# 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(os.path.join(data_dir, "result.svg"))

Draw Raster Image on EMF

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

  • Load a raster image using the factory method load(…) exposed by Image class.
  • Load a EMF image
  • Draw a rectangular part of the raster image within the specified bounds of the vector image
  • Save the results
import aspose.pycore as aspycore
from aspose.imaging import Image, RasterImage, Rectangle, GraphicsUnit
from aspose.imaging.fileformats.emf import EmfImage
from aspose.imaging.fileformats.emf.graphics import EmfRecorderGraphics2D
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
# Load the image to be drawn
with aspycore.as_of(Image.load(os.path.join(data_dir, "template.png")), RasterImage) as image_to_draw:
# Load the image for drawing on it (drawing surface)
with aspycore.as_of(Image.load(os.path.join(data_dir, "template.emf")), EmfImage) as canvas_image:
graphics = EmfRecorderGraphics2D.from_emf_image(canvas_image)
# Draw a rectagular part of the raster image within the specified bounds of the vector image (drawing surface).
# Note that because the source size is not equal to the destination one, the drawn image is stretched horizontally and vertically.
graphics.draw_image(image_to_draw, Rectangle(67, 67, canvas_image.width, canvas_image.height),
Rectangle(0, 0, image_to_draw.width, image_to_draw.height), GraphicsUnit.PIXEL)
# Save the result image
with graphics.end_recording() as result_image:
result_image.save(os.path.join(data_dir, "result.emf"))
if delete_output:
os.remove(os.path.join(data_dir, "result.emf"))

Draw Raster Image on WMF

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

  • Load a raster image using the factory method load(…) exposed by Image class.
  • Load a WMF image
  • Draw a rectangular part of the raster image within the specified bounds of the vector image
  • Save the results
import aspose.pycore as aspycore
from aspose.imaging import Image, RasterImage, Rectangle, GraphicsUnit
from aspose.imaging.fileformats.wmf import WmfImage
from aspose.imaging.fileformats.wmf.graphics import WmfRecorderGraphics2D
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
# Load the image to be drawn
with aspycore.as_of(Image.load(os.path.join(data_dir, "template.png")), RasterImage) as image_to_draw:
# Load the image for drawing on it (drawing surface)
with aspycore.as_of(Image.load(os.path.join(data_dir, "template.wmf")), WmfImage) as canvas_image:
graphics = WmfRecorderGraphics2D.from_wmf_image(canvas_image)
# Draw a rectagular part of the raster image within the specified bounds of the vector image (drawing surface).
# Note that because the source size is not equal to the destination one, the drawn image is stretched horizontally and vertically.
graphics.draw_image(image_to_draw, Rectangle(67, 67, canvas_image.width, canvas_image.height), Rectangle(0, 0, image_to_draw.width, image_to_draw.height), GraphicsUnit.PIXEL)
# Save the result image
with graphics.end_recording() as result_image:
result_image.save(os.path.join(data_dir, "result.wmf"))
if delete_output:
os.remove(os.path.join(data_dir, "result.wmf"))