How to watermark emf/wmf image
Contents
[
Hide
]
How to watermark emf/wmf image
Issue : How to watermark emf/wmf image.
Tips : To properly watermark emf/wmf image, it is needed to use WmfRecorderGraphics2D and EmfRecorderGraphics2D classes to work with graphics in those formats.
Example :
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import aspose.imaging as imaging | |
import aspose.pycore as aspycore | |
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 | |
# load an existing EMF with Image.load | |
with imaging.Image.load(os.path.join(templates_folder, "template.emf")) as image: | |
# create and initialize an instance of Graphics class and Initialize an object of SizeF to store image Size | |
graphics = imaging.fileformats.emf.graphics.EmfRecorderGraphics2D.from_emf_image(aspycore.as_of(image, imaging.fileformats.emf.EmfImage)) | |
size = image.size | |
# create an instance of Font. Initialize it with Font Face, Size and Style | |
font = imaging.Font("Times New Roman", 20.0, imaging.FontStyle.BOLD) | |
# draw the string on image | |
graphics.draw_string("CONFIDENTIAL", font, imaging.Color.red, image.width // 2, image.height // 2) | |
image2 = graphics.end_recording() | |
# save output to disc | |
image2.save(os.path.join(templates_folder, "output.emf")) | |
if delete_output: | |
os.remove(os.path.join(templates_folder, "output.emf")) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import aspose.imaging as imaging | |
import aspose.pycore as aspycore | |
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 | |
# load an existing WMF with Image.load | |
with imaging.Image.load(os.path.join(templates_folder, "template.wmf")) as image: | |
wmf_graphics = imaging.fileformats.wmf.graphics.WmfRecorderGraphics2D.from_wmf_image(aspycore.as_of(image, imaging.fileformats.wmf.WmfImage)) | |
size = image.size | |
# create an instance of Font. Initialize it with Font Face, Size and Style | |
font = imaging.Font("Times New Roman", 20.0, imaging.FontStyle.BOLD) | |
# draw the string on image | |
wmf_graphics.draw_string("CONFIDENTIAL", font, imaging.Color.red, 50, 50) | |
im2 = wmf_graphics.end_recording() | |
im2.save(os.path.join(templates_folder, "output.wmf")) | |
if delete_output: | |
os.remove(os.path.join(templates_folder, "output.wmf")) |