Manipulating WMF Files

Create WMF MetaFile Image

Using Aspose.Imaging for Python via .NET, developers can create WMF metafile image. This topic explains in detail that how WMF metafile image can be created. Aspose.Imaging for Python via .NET provides the WmfRecorderGraphics2D class to create WMF metafile. The following code snippet shows you how to create WMF Metafile Image.

import aspose.pycore as aspycore
from aspose.imaging import Image, Rectangle, Size, SizeF, Pen, Color, Point, LineCap, LineJoin, \
HatchStyle, DashStyle, RasterImage, Font
from aspose.imaging.brushes import SolidBrush, HatchBrush
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
graphics = WmfRecorderGraphics2D(Rectangle(0, 0, 100, 100), 96)
# Define background color
graphics.background_color = Color.white_smoke
# Init Create an instance of Imaging Pen class, Brush class and mention its color.
pen = Pen(Color.blue)
brush = SolidBrush(Color.yellow_green)
# Polygon Fill polygon and then Draw a polygon
graphics.fill_polygon(brush, [Point(2, 2), Point(20, 20), Point(20, 2)])
graphics.draw_polygon(pen, [Point(2, 2), Point(20, 20), Point(20, 2)])
obj_init = HatchBrush()
obj_init.hatch_style = HatchStyle.CROSS
obj_init.background_color = Color.white
obj_init.foreground_color = Color.silver
brush = obj_init
# Fill ellipse and Draw an ellipse
graphics.fill_ellipse(brush, Rectangle(25, 2, 20, 20))
graphics.draw_ellipse(pen, Rectangle(25, 2, 20, 20))
# Arc Define pen style by setting DashStyle value, Set color of the pen
pen.dash_style = DashStyle.DOT
pen.color = Color.black
# Draw an Arc by calling DrawArc method and set CubicBezier
graphics.draw_arc(pen, Rectangle(50, 2, 20, 20), 0, 180)
pen.dash_style = DashStyle.SOLID
pen.color = Color.red
# Draw an CubicBezier
graphics.draw_cubic_bezier(pen, Point(10, 25), Point(20, 50), Point(30, 50), Point(40, 25))
# Image Create an Instance of Image class.
with Image.load(os.path.join(data_dir, "template.bmp")) as image:
# Cast the instance of image class to RasterImage.
raster_image = aspycore.as_of(image, RasterImage)
if raster_image is not None:
# Draw an image by calling DrawImage method and passing parameters rasterimage and point.
graphics.draw_image(raster_image, Point(50, 50))
# Line Draw a line by calling DrawLine method and passing x,y coordinates of 1st point and same for 2nd point along with color infor as Pen.
graphics.draw_line(pen, Point(2, 98), Point(2, 50))
# Pie Define settings of the brush object.
brush = SolidBrush(Color.green)
pen.color = Color.dark_goldenrod
# Fill pie by calling FillPie method and passing parameters brush and an instance of Imaging Rectangle class.
graphics.fill_pie(brush, Rectangle(2, 38, 20, 20), 0, 45)
# Draw pie by calling DrawPie method and passing parameters pen and an instance of Imaging Rectangle class.
graphics.draw_pie(pen, Rectangle(2, 38, 20, 20), 0, 45)
pen.color = Color.alice_blue
# Polyline Draw Polyline by calling DrawPolyline method and passing parameters pen and points.
graphics.draw_polyline(pen, [Point(50, 40), Point(75, 40), Point(75, 45), Point(50, 45)])
# For having Strings Create an instance of Font class.
font = Font("Arial", 16.0)
# Draw String by calling DrawString method and passing parameters string to display, color and X & Y coordinates.
graphics.draw_string("Aspose", font, Color.blue, 25, 75)
# Call end recording of graphics object and save WMF image by calling Save method.
with graphics.end_recording() as image:
image.save(os.path.join(data_dir, "result.wmf"))
if delete_output:
os.remove(os.path.join(data_dir, "result.wmf"))

Create EMF MetaFile Image

Using Aspose.Imaging for Python via .NET, developers can create EMF metafile image. This topic explains in detail that how EMF metafile image can be created. This topic also covers the test to convert the newly generated EMF metafile image to PDF format. Aspose.Imaging for Python via .NET provides the EmfRecorderGraphics2D class to create EMF metafile. Below is the code demonstration of the said functionality.

import aspose.pycore as aspycore
from aspose.imaging import Image, Rectangle, Size, SizeF, Pen, Color, Point, LineCap, LineJoin, HatchStyle
from aspose.imaging.imageoptions import EmfRasterizationOptions, PdfOptions
from aspose.imaging.brushes import SolidBrush, HatchBrush
from aspose.imaging.fileformats.emf.graphics import EmfRecorderGraphics2D
from aspose.imaging.fileformats.emf.emf.consts import EmfBackgroundMode
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
# EmfRecorderGraphics2D class provides you the frame or canvas to draw shapes on it.
graphics = EmfRecorderGraphics2D(Rectangle(0, 0, 1000, 1000), Size(1000, 1000), Size(100, 100))
# Create an instance of Imaging Pen class and mention its color.
pen = Pen(Color.bisque)
# Draw a line by calling DrawLine method and passing x,y coordinates of 1st point and same for 2nd point along with color infor as Pen.
graphics.draw_line(pen, 1, 1, 50, 50)
# Reset the Pen color Specify the end style of the line.
pen = Pen(Color.blue_violet, 3)
pen.end_cap = LineCap.ROUND
# Draw a line by calling DrawLine method and passing x,y coordinates of 1st point and same for 2nd point along with color infor as Pen and end style of line.
graphics.draw_line(pen, 15, 5, 50, 60)
# Specify the end style of the line.
pen.end_cap = LineCap.SQUARE
graphics.draw_line(pen, 5, 10, 50, 10)
pen.end_cap = LineCap.FLAT
# Draw a line by calling DrawLine method and passing parameters.
graphics.draw_line(pen, Point(5, 20), Point(50, 20))
# Create an instance of HatchBrush class to define rectanglurar brush with with different settings.
hatch_brush = HatchBrush()
hatch_brush.background_color = Color.alice_blue
hatch_brush.foreground_color = Color.red
hatch_brush.hatch_style = HatchStyle.CROSS
# Draw a line by calling DrawLine method and passing parameters.
pen = Pen(hatch_brush, 7)
graphics.draw_rectangle(pen, 50, 50, 20, 30)
# Draw a line by calling DrawLine method and passing parameters with different mode.
graphics.background_mode = EmfBackgroundMode.OPAQUE
graphics.draw_line(pen, 80, 50, 80, 80)
# Draw a polygon by calling DrawPolygon method and passing parameters with line join setting/style.
pen = Pen(SolidBrush(Color.aqua), 3)
pen.line_join = LineJoin.MITER_CLIPPED
graphics.draw_polygon(pen, [Point(10, 20), Point(12, 45), Point(22, 48), Point(48, 36), Point(30, 55)])
# Draw a rectangle by calling DrawRectangle method.
pen.line_join = LineJoin.BEVEL
graphics.draw_rectangle(pen, 50, 10, 10, 5)
pen.line_join = LineJoin.ROUND
graphics.draw_rectangle(pen, 65, 10, 10, 5)
pen.line_join = LineJoin.MITER
graphics.draw_rectangle(pen, 80, 10, 10, 5)
# Call EndRecording method to produce the final shape. EndRecording method will return the final shape as EmfImage. So create an instance of EmfImage class and initialize it with EmfImage returned by EndRecording method.
with graphics.end_recording() as image:
# Create an instance of PdfOptions class.
options = PdfOptions()
# Create an instance of EmfRasterizationOptions class and define different settings.
rasterization_options = EmfRasterizationOptions()
rasterization_options.page_size = aspycore.cast(SizeF, image.size)
options.vector_rasterization_options = rasterization_options
out_path = os.path.join(data_dir, "result.pdf")
image.save(out_path, options)
if delete_output:
os.remove(out_path)

Resize WMF file while converting to PNG

Using Aspose.Imaging for Python via .NET, developers can resize the WMF metafile while converting it to raster format. This topic explains the approach to load existing metafiles, resize it and convert it to raster format. Aspose.Imaging for Python via .NET provides the Image class to load WMF files and same can be used to resize and save the image to PNG format. The following code snippet shows you how to resize the WMF file while converting it to PNG.

import aspose.pycore as aspycore
from aspose.imaging import Image, Color, SizeF
from aspose.imaging.imageoptions import WmfRasterizationOptions, 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
# Load an existing WMF image
with Image.load(os.path.join(data_dir, "template.wmf")) as image:
# Call the resize method of Image class and width,height values and Calculate new PNG image height
image.resize(100, 100)
k = (image.width * 1.00) / image.height
# Create an instance of EmfRasterizationOptions class and set different properties
emf_rasterization = WmfRasterizationOptions()
emf_rasterization.background_color = Color.white_smoke
emf_rasterization.page_width = 100.0
emf_rasterization.page_height = 100.0 / k
emf_rasterization.border_x = 5.0
emf_rasterization.border_y = 10.0
emf_rasterization.page_size = aspycore.cast(SizeF, image.size)
# Create an instance of PngOptions class and provide rasterization option
image_options = PngOptions()
image_options.vector_rasterization_options = emf_rasterization
# Call the save method, provide output path and PngOptions to convert the WMF file to PNG and save the output
image.save(os.path.join(data_dir, "result.png"), image_options)
if delete_output:
os.remove(os.path.join(data_dir, "result.png"))

Converting EMF to WMF

Using Aspose.Imaging for Python via .NET, developers can convert EMF to WMF metafile format. This topic explains the approach to load existing EMF metafiles and convert it to WMF format. Aspose.Imaging for Python via .NET provides the Image class to load EMF files and same can be used to save the image to WMF format. The following code snippet shows you how to convert EMF to WMF.

from aspose.imaging import Image
from aspose.imaging.imageoptions import EmfRasterizationOptions, WmfOptions
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 an existing EMF file as Image.
with Image.load(os.path.join(data_dir, "template.emf")) as image:
rasterization_options = EmfRasterizationOptions()
rasterization_options.page_width = float(image.width)
rasterization_options.page_height = float(image.height)
export_options = WmfOptions()
export_options.vector_rasterization_options = rasterization_options
# Call the Save method of Image class & Pass instance of WmfOptions class to Save method.
image.save(os.path.join(data_dir, "result.wmf"), export_options)
if delete_output:
os.remove(os.path.join(data_dir, "result.wmf"))

Exporting Text as Shape While Converting EMF MetaFile

Using Aspose.Imaging for Python via .NET, developers can get text as shapes while converting EMF to SVG format. This topic explains in detail how to convert text into shape while converting EMF to SVG format. Aspose.Imaging for Python via .NET provides the TextAsShapes property to get text as shape while converting EMF metafile. Below is the code demonstration of the said functionality.

import aspose.pycore as aspycore
from aspose.imaging import Image, Color
from aspose.imaging.imageoptions import EmfRasterizationOptions, SvgOptions
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
with Image.load(os.path.join(data_dir, "template.emf")) as image:
emf_rasterization_options = EmfRasterizationOptions()
emf_rasterization_options.background_color = Color.white
emf_rasterization_options.page_width = float(image.width)
emf_rasterization_options.page_height = float(image.height)
svg_opts = SvgOptions()
svg_opts.vector_rasterization_options = emf_rasterization_options
svg_opts.text_as_shapes = True
image.save(os.path.join(data_dir, "result.svg"), svg_opts)
svg_opts = SvgOptions()
svg_opts.vector_rasterization_options = emf_rasterization_options
svg_opts.text_as_shapes = False
image.save(os.path.join(data_dir, "result2.svg"), svg_opts)
if delete_output:
os.remove(os.path.join(data_dir, "result.svg"))
os.remove(os.path.join(data_dir, "result2.svg"))

Get Last Modified Date of A Raster Image

This article demonstrates the usage of Aspose.Imaging for Python via .NET to get last modified date of an image. Aspose.Imaging APIs have exposed efficient & easy to use methods to achieve this goal.

Get Last Modified Date

Aspose.Imaging for Python via .NET has exposed the get_modify_date method of Image class to get the last modified date of an image from its metadata. get_modify_date method needs a Boolean value to get the modified date accordingly. The steps to get last modified date are as simple as below:

  1. Load an image using the factory method Load exposed by Image class.
  2. Convert the image into RasterImage.
  3. Call the RasterImage.get_modify_date method by passing a Boolean true or false value.

The following code snippet shows you how to get last modified date of the image.

import aspose.pycore as aspycore
from aspose.imaging import Image, RasterImage
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
with aspycore.as_of(Image.load(os.path.join(data_dir, "template.jpg")), RasterImage) as image:
# Gets the date from [FileInfo]
modify_date = str(image.get_modify_date(True))
print("Last modify date using [FileInfo]:", modify_date)
# Gets the date from XMP metadata of [FileInfo] as long as it's not default case
modify_date = str(image.get_modify_date(False))
print("Last modify date using info from [FileInfo] and XMP metadata:", modify_date)

Crop WMF Image

Image cropping usually refers to the removal of the outer parts of an image to help improve the framing. Cropping may also be used for to cut out some portion of an image to increase the focus on a particular area. The WmfImage class provides crop method that accepts an instance of the Rectangle class. You can cut out any portion of an image by providing the desired boundaries to the Rectangle object.

The code snippet below demonstrates how to Crop any image by Rectangle.

import aspose.pycore as aspycore
from aspose.imaging.fileformats.wmf import WmfImage
from aspose.imaging import Image, Rectangle
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
with aspycore.as_of(Image.load(os.path.join(data_dir, "template.wmf")), WmfImage) as image:
image.crop(Rectangle(10, 10, 20, 20))
print("image.width:", image.width, "image.height:", image.height)
image.save(os.path.join(data_dir, "result.wmf"))
if delete_output:
os.remove(os.path.join(data_dir, "result.wmf"))