Skip to content

Instantly share code, notes, and snippets.

@aspose-com-gists
Last active June 11, 2023 16:59

Aspose.Imaging for Python via .NET API allows to easy put watermark on your images or photos in your Python application or Web service.

You can:

  • use various fonts, colors, styles for watermark text;
  • set watermark opacity;
  • set up string formatting.

Interested ?

You may go further at : https://products.aspose.com/imaging/python-net/

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 BMP with Image.Load
with imaging.Image.load(os.path.join(templates_folder, "template.bmp")) 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.bmp"))
if delete_output:
os.remove(os.path.join(templates_folder, "output.bmp"))
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 DIB with Image.Load
with imaging.Image.load(os.path.join(templates_folder, "template.dib")) 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.dib"))
if delete_output:
os.remove(os.path.join(templates_folder, "output.dib"))
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 DICOM with Image.Load
with imaging.Image.load(os.path.join(templates_folder, "template.dicom")) 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.dicom"))
if delete_output:
os.remove(os.path.join(templates_folder, "output.dicom"))
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"))
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 GIF with Image.Load
with imaging.Image.load(os.path.join(templates_folder, "template.gif")) 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.gif"))
if delete_output:
os.remove(os.path.join(templates_folder, "output.gif"))
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 J2K with Image.Load
with imaging.Image.load(os.path.join(templates_folder, "template.j2k")) 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.j2k"))
if delete_output:
os.remove(os.path.join(templates_folder, "output.j2k"))
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 JP2 with Image.Load
with imaging.Image.load(os.path.join(templates_folder, "template.jp2")) 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.jp2"))
if delete_output:
os.remove(os.path.join(templates_folder, "output.jp2"))
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 PNG with Image.Load
with imaging.Image.load(os.path.join(templates_folder, "template.png")) 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.png"))
if delete_output:
os.remove(os.path.join(templates_folder, "output.png"))
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 SVG with Image.Load
with imaging.Image.load(os.path.join(templates_folder, "template.svg")) as image:
# create and initialize an instance of Graphics class and Initialize an object of SizeF to store image Size
graphics = imaging.fileformats.svg.graphics.SvgGraphics2D(aspycore.as_of(image, imaging.fileformats.svg.SvgImage))
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(font, "CONFIDENTIAL", imaging.Point(0, size.height // 2), imaging.Color.red)
image2 = graphics.end_recording()
# save output to disc
image2.save(os.path.join(templates_folder, "output.svg"))
if delete_output:
os.remove(os.path.join(templates_folder, "output.svg"))
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"))
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 WEBP with Image.load
with imaging.Image.load(os.path.join(templates_folder, "template.webp")) 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.webp"))
if delete_output:
os.remove(os.path.join(templates_folder, "output.webp"))
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"))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment