Python Pillow 简明教程

Python Pillow - Creating a Watermark

What is Watermark?

watermark 是可识别的且常见的透明图像或文字,其叠加在其他图像、文档或对象上,表明其所有权、作者或来源。水印通常用于保护知识产权和内容,防止其未经授权使用或分发,并提供归属。它们有以下所述的各种用途 −

A watermark is a recognizable and often transparent image or text that is superimposed onto another image, document or object to indicate ownership, authorship or origin. Watermarks are typically used to protect intellectual property and content which prevent unauthorized use or distribution and provide attribution. They serve various purposes as mentioned below −

  1. Copyright Protection − Artists, photographers and content creators often use watermarks to protect their intellectual property by marking their work with their name, logo or copyright information. This helps deter unauthorized use or distribution of their content.

  2. Branding − Companies and organizations use watermarks to brand their images or documents with their logos, names or slogans. This reinforces their brand identity and makes it clear where the content originated.

  3. Document Verification − Watermarks can be used on official documents such as certificates to prevent forgery or unauthorized reproduction. For example diplomas or notarized documents may have watermarks.

  4. Security − In currency and other security documents intricate and often invisible watermarks are used to deter counterfeiting. These watermarks are difficult to reproduce accurately making it easier to detect counterfeit bills or documents.

  5. Image Attribution − In the context of stock photography and image licensing watermarks can be used to display a preview of the image with a watermark. When a user purchases the image they receive a version without the watermark.

  6. Digital Media − In the digital world watermarks are often used on images and videos shared online to protect content. They can also be used to give credit to the original creator.

水印可以采用各种形式,例如文本、徽标、模式,甚至嵌入内容中的不可见数据。在放置时,绝大多数情况下不会损失内容的质量,其目的是提供真实性或所有权的可视或数字指示器。

Watermarks can take various forms such as text, logos, patterns or even invisible data embedded within the content. They are typically placed in a manner that is difficult to remove without compromising the quality of the content and their purpose is to provide a visual or digital indicator of authenticity or ownership.

Creating the text watermark

现在让我们探讨如何使用 pillow 库创建文字水印。在 pillow 中,没有直接创建水印的方法,但我们可以使用 ImageDraw, ImageFontImage 方法来实现。

Now let’s explore how to create a text watermark by using the pillow library. In pillow there is no direct method to create the watermarks but we can achieve it by using the ImageDraw, ImageFont and Image methods.

以下是本章所有示例中使用的输入图像。

Following is the input image used in all the examples of this chapter.

butterfly original image

Example

在此示例中,我们使用 pillow 库创建文本 Tutorialspoint 作为水印。

In this example we are creating the text Tutorialspoint as the watermark by using the pillow library.

from PIL import Image, ImageDraw, ImageFont
original_image = Image.open("Images/butterfly.jpg")
draw = ImageDraw.Draw(original_image)
watermark_text = "Tutorialspoint"
font_size = 20
font = ImageFont.truetype("arial.ttf", font_size)
text_color = (255, 255, 255)

#White color (RGB)
text_width, text_height = draw.textsize(watermark_text, font)
image_width, image_height = original_image.size
margin = 10

#Margin from the right and bottom edges
position = (image_width - text_width - margin, image_height - text_height - margin)
draw.text(position, watermark_text, font=font, fill=text_color)
original_image.save("output Image/watermarked_image.png")
original_image.show()
watermarked

Creating the image Watermark

之前我们在图像上创建了文字水印,同样,我们可以使用 pillow 中提供的 ImageDraw, copypaste 方法创建一个图像作为水印。

Previously we created the text watermark on an image, in the same way we can create an image as the watermark by using the ImageDraw, copy and paste methods available in pillow.

Example

在此示例中,我们通过 pillow 中提供的 Tutoriaslpoint 方法创建徽标图像作为水印。

In this example we are creating the Tutoriaslpoint logo image as the watermark by the methods available in pillow.

from PIL import Image
original_image = Image.open("Images/butterfly.jpg")
watermark = Image.open("Images/tutorialspoint.png")
#Use the appropriate image file for your watermark

#Resize the watermark image to a specific width or height
target_width = 200

#Adjust this to our preferred size
aspect_ratio = float(target_width) / watermark.width
target_height = int(watermark.height * aspect_ratio)
watermark = watermark.resize((target_width, target_height), Image.ANTIALIAS)
watermarked_image = original_image.copy()

#Adjust the position where you want to place the watermark (e.g., bottom right corner)
position = (original_image.width - watermark.width, original_image.height - watermark.height)
watermarked_image.paste(watermark, position, watermark)
watermarked_image.save("output Image/watermarked_image.jpg")
watermarked_image.show()
watermarked tp