How to insert watermark to tiff file
Contents
[
Hide
]
How to insert watermark to tiff file
Issue : I want to insert gray watermark characters in a tiff file.
Tips : To watermark tiff image there can be used graphics object to draw exactly needed characters.
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 TIFF with Image.load | |
with imaging.Image.load(os.path.join(templates_folder, "template.tiff")) as image: | |
# create and initialize an instance of Graphics class and Initialize an object of SizeF to store image Size | |
graphics = imaging.Graphics(image) | |
size = graphics.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) | |
# create an instance of SolidBrush and set Color & Opacity | |
brush = imaging.brushes.SolidBrush() | |
brush.color = imaging.Color.red | |
brush.opacity = 0.0 | |
# initialize an object of StringFormat class and set its various properties | |
format_ = imaging.StringFormat() | |
format_.alignment = imaging.StringAlignment.CENTER | |
format_.format_flags = imaging.StringFormatFlags.MEASURE_TRAILING_SPACES | |
# draw the string on image | |
graphics.draw_string("CONFIDENTIAL", font, brush, image.width / 2, image.height / 2, format_) | |
# save output to disc | |
image.save(os.path.join(templates_folder, "output.tiff")) | |
if delete_output: | |
os.remove(os.path.join(templates_folder, "output.tiff")) |