Converting WMF and EMF to Other Image Formats

Converting EMF or WMF to PDF

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

import aspose.pycore as aspycore
from aspose.imaging import Image, Color
from aspose.imaging.imageoptions import PdfOptions, EmfRasterizationOptions
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.emf")) as image:
# Create an instance of EmfRasterizationOptions class and set different properties
emf_rasterization_options = EmfRasterizationOptions()
emf_rasterization_options.background_color = Color.white_smoke
emf_rasterization_options.page_width = float(image.width)
emf_rasterization_options.page_height = float(image.height)
# Create an instance of PdfOptions class and provide rasterization option
pdf_options = PdfOptions()
pdf_options.vector_rasterization_options = emf_rasterization_options
# Call the save method, provide output path and PdfOptions to convert the WMF file to PDF and save the output
image.save(os.path.join(data_dir, "result.pdf"), pdf_options)
if delete_output:
os.remove(os.path.join(data_dir, "result.pdf"))

Converting WMF To Webp

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

import aspose.pycore as aspycore
from aspose.imaging import Image, Color
from aspose.imaging.imageoptions import WebPOptions, EmfRasterizationOptions
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 image
with Image.load(os.path.join(data_dir, "template.emf")) as image:
# Calculate new Webp image height
k = (image.width * 1.00) / image.height
# Create an instance of EmfRasterizationOptions class and set different properties
emf_rasterization = EmfRasterizationOptions()
emf_rasterization.background_color = Color.white_smoke
emf_rasterization.page_width = 400.0
emf_rasterization.page_height = 400.0 / k
emf_rasterization.border_x = 5.0
emf_rasterization.border_y = 10.0
# Create an instance of WebPOptions class and provide rasterization option
image_options = WebPOptions()
image_options.vector_rasterization_options = emf_rasterization
# Call the save method, provide output path and WebPOptions to convert the WMF file to Webp and save the output
image.save(os.path.join(data_dir, "result.webp"), image_options)
if delete_output:
os.remove(os.path.join(data_dir, "result.webp"))

Converting WMF to PNG

Using Aspose.Imaging for Python via .NET, developers can convert WMF metafile to raster format. This topic explains the approach to load existing metafiles 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 save the image to PNG format. The following code snippet shows you how convert WMF To Raster Format.

import aspose.pycore as aspycore
from aspose.imaging import Image, Color
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
with Image.load(os.path.join(data_dir, "template.wmf")) as image:
rasterization_options = WmfRasterizationOptions()
rasterization_options.background_color = Color.white_smoke
rasterization_options.page_width = float(image.width)
rasterization_options.page_height = float(image.height)
options = PngOptions()
options.vector_rasterization_options = rasterization_options
image.save(os.path.join(data_dir, "result.png"), options)
if delete_output:
os.remove(os.path.join(data_dir, "result.png"))

Converting WMF MetaFile To SVG

Using Aspose.Imaging for Python via .NET, developers can convert WMF to SVG format. This topic explains in detail how to convert WMF to SVG format. Aspose.Imaging for Python via .NET provides the SvgOptions class to create SVG format image. The following code snippet shows you how to convert WMF To SVG format.

import aspose.pycore as aspycore
from aspose.imaging import Image, Color
from aspose.imaging.imageoptions import WmfRasterizationOptions, 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.wmf")) as image:
rasterization_options = WmfRasterizationOptions()
rasterization_options.background_color = Color.white_smoke
rasterization_options.page_width = float(image.width)
rasterization_options.page_height = float(image.height)
options = SvgOptions()
options.vector_rasterization_options = rasterization_options
image.save(os.path.join(data_dir, "result.svg"), options)
if delete_output:
os.remove(os.path.join(data_dir, "result.svg"))

Converting EMF to WMF Format

Using Aspose.Imaging for Python via .NET, developers can convert EMF to WMF format. This topic explains in detail how to convert WMF to WMF format. The following code snippet shows you how to convert WMF To SVG format.

import aspose.pycore as aspycore
from aspose.imaging import Image
from aspose.imaging.imageoptions import WmfOptions, EmfRasterizationOptions
from aspose.imaging.fileformats.emf import MetaImage
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 EMF image as image and convert it to MetaImage object.
with aspycore.as_of(Image.load(os.path.join(data_dir, "template.emf")), MetaImage) as image:
# Convert the EMF image to WMF image by creating and passing WMF image options class object.
obj_init = WmfOptions()
obj_init.vector_rasterization_options = EmfRasterizationOptions()
image.save(os.path.join(data_dir, "result.wmf"), obj_init)
if delete_output:
os.remove(os.path.join(data_dir, "result.wmf"))

Cropping WMF file while converting to PNG

Aspose.Imaging for Python via .NET provides the Image class to load WMF files and same can be used to crop and save the image to PNG format.

Below provided sample code demonstrate how to crop the WMF file while converting it to PNG.

import aspose.pycore as aspycore
from aspose.imaging import Image, Color, Rectangle
from aspose.imaging.imageoptions import WmfRasterizationOptions, PngOptions
from aspose.imaging.fileformats.wmf import WmfImage
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 aspycore.as_of(Image.load(os.path.join(data_dir, "template.wmf")), WmfImage) as image:
image.crop(Rectangle(0, 0, 100, 100))
# Create an instance of WmfRasterizationOptions class and set different properties
wmf_rasterization = WmfRasterizationOptions()
wmf_rasterization.background_color = Color.white_smoke
wmf_rasterization.page_width = 1000.0
wmf_rasterization.page_height = 1000.0
# Create an instance of PngOptions class and provide rasterization option
image_options = PngOptions()
image_options.vector_rasterization_options = wmf_rasterization
# Call the save method, provide output path and PngOptions to convert the cropped 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"))

Support For saving EMF and EMF+ format to File

Using Aspose.Imaging for Python via .NET, developers can save EMF and EMF plus format to file. This topic explains in detail how to save emf graphics files. The code snippet has been provided below.

import aspose.pycore as aspycore
from aspose.imaging import Image
from aspose.imaging.fileformats.emf import MetaImage
from aspose.imaging.imageoptions import EmfOptions
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
path = os.path.join(data_dir, "template.emf")
with aspycore.as_of(Image.load(path), MetaImage) as image:
image.save(os.path.join(data_dir, "result.emf"), EmfOptions())
if delete_output:
os.remove(os.path.join(data_dir, "result.emf"))
import aspose.pycore as aspycore
from aspose.imaging import Image, Color, Font, FontStyle, Rectangle, Size
from aspose.imaging.imageoptions import EmfOptions
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
def get_font_style_name(style):
names = []
for it in FontStyle:
if it.value & style != 0:
names.append(it.name.capitalize())
return ", ".join(names)
graphics = EmfRecorderGraphics2D(Rectangle(0, 0, 5000, 5000), Size(5000, 5000), Size(1000, 1000))
font = Font("Arial", 10.0, FontStyle.BOLD | FontStyle.UNDERLINE)
graphics.draw_string(font.name + " " + str(font.size) + " " + get_font_style_name(font.style),
font, Color.brown, 10, 80)
graphics.draw_string("some text", font, Color.brown, 10, 100)
font = Font("Arial", 24.0, FontStyle.ITALIC | FontStyle.STRIKEOUT)
graphics.draw_string(font.name + " " + str(font.size) + " " + get_font_style_name(font.style), font, Color.brown, 20, 120)
graphics.draw_string("some text", font, Color.brown, 20, 150)
with graphics.end_recording() as image:
path = os.path.join(data_dir, "result.emf")
image.save(path, EmfOptions())
if delete_output:
os.remove(os.path.join(data_dir, "result.emf"))