Adding a Watermark to an Image
Adding a Watermark to an Image
This document explains how to add a watermark to an image using Aspose.Imaging. Adding a watermark to an image is a common requirement for image processing applications. This example uses the Graphics class to draw a string on the image surface.
Adding a Watermark
To demonstrate the operation, we will load a BMP image from disk and draw a string as the watermark on the image surface using the Graphics class' DrawString method. We’ll save the image to PNG format using the PngOptions class. Below is a code example that demonstrates how to add a watermark to an image. The example source code has been split into parts to make it easy to follow. Step by step, the examples show how to:
- Load an image.
- Create and initialize a Graphics object.
- Create and initialize Font and SolidBrush objects.
- Draw a string as watermark using the Graphics class' DrawString method.
- Save image to PNG.
The following code snippet shows you how to add watermark on the image.
Adding a Diagonal Watermark
Adding a diagonal watermark to an image is similar to adding a horizontal watermark as discussed above, with a few differences. To demonstrate the operation, we will load a JPG image from disk, add transformations using an object of Matrix class and draw a string as the watermark on the image surface using the Graphics class' DrawString method. Below is a code example that demonstrates how to add a diagonal watermark to an image. The example source code has been split into parts to make it easy to follow. Step by step, the examples show how to:
- Load an image.
- Create and initialize a Graphics object.
- Create and initialize Font and SolidBrush objects.
- Get size of image in SizeF object.
- Create an instance of Matrix class and perform composite transformation.
- Assign transformation to Graphics object.
- Create and initialize a StringFormat object.
- Draw a string as watermark using the Graphics class' DrawString method.
- Save resultant image.
The following code snippet shows you how to add a diagonal watermark.
import aspose.pycore as aspycore | |
from aspose.imaging import * | |
from aspose.imaging.brushes import * | |
from aspose.imaging.fileformats.jpeg import * | |
from aspose.pycore import as_of, is_assignable | |
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.tiff")) as image: | |
the_string = "45 Degree Rotated Text" | |
graphics = Graphics(image) | |
sz = graphics.image.size | |
font = Font("Times New Roman", 20.0, FontStyle.BOLD) | |
brush = SolidBrush() | |
brush.color = Color.red | |
brush.opacity = 0 | |
format_ = StringFormat() | |
format_.alignment = StringAlignment.CENTER | |
format_.format_flags = StringFormatFlags.MEASURE_TRAILING_SPACES | |
matrix = Matrix() | |
matrix.translate(sz.width / 2, sz.height / 2) | |
matrix.rotate(-45.0) | |
graphics.transform = matrix | |
graphics.draw_string(the_string, font, brush, 0, 0, format_) | |
image.save(os.path.join(data_dir, "result.jpg")) | |
if delete_output: | |
os.remove(os.path.join(data_dir, "result.jpg")) |