Adding image watermarks

GroupDocs.Watermark supports adding the following image formats as watermarks:

  • BMP
  • PNG
  • GIF
  • JPEG

Add an image watermark from a local file

The following example adds an ImageWatermark to a document. If the document consists of multiple parts (pages, worksheets, slides, frames, etc.), the watermark is added to each of them.

from groupdocs.watermark import Watermarker
from groupdocs.watermark.watermarks import ImageWatermark, SizingType
from groupdocs.watermark.common import HorizontalAlignment, VerticalAlignment

def add_image_watermark():
    with Watermarker("./sample.docx") as watermarker:
        with ImageWatermark("./logo.png") as watermark:
            watermark.horizontal_alignment = HorizontalAlignment.CENTER
            watermark.vertical_alignment = VerticalAlignment.CENTER
            watermark.sizing_type = SizingType.SCALE_TO_PARENT_DIMENSIONS
            watermark.scale_factor = 0.5
            watermark.opacity = 0.7
            watermarker.add(watermark)
        watermarker.save("./output.docx")

if __name__ == "__main__":
    add_image_watermark()

sample.docx and logo.png are the sample files used in this example. Download sample.docx and logo.png.

Binary file (DOCX, 125 KB)

Download full output

Add an image watermark from a stream

You can also initialize an ImageWatermark from a stream — pass it as the stream argument.

import io
from groupdocs.watermark import Watermarker
from groupdocs.watermark.watermarks import ImageWatermark

def add_image_watermark_from_stream():
    with open("./logo.png", "rb") as f:
        data = f.read()

    with Watermarker("./sample.docx") as watermarker:
        with ImageWatermark(stream=io.BytesIO(data)) as watermark:
            watermarker.add(watermark)
        watermarker.save("./output.docx")

if __name__ == "__main__":
    add_image_watermark_from_stream()

sample.docx and logo.png are the sample files used in this example. Download sample.docx and logo.png.

Binary file (DOCX, 120 KB)

Download full output

For advanced watermark properties (tiling, alignment, sizing, rotation, and margins), the same techniques shown for text watermarks apply to image watermarks. See Adding text watermarks.

Close
Loading

Analyzing your prompt, please hold on...

An error occurred while retrieving the results. Please refresh the page and try again.