Home > Backend Development > Python Tutorial > How to add text watermark to pictures using Python

How to add text watermark to pictures using Python

PHPz
Release: 2023-08-27 09:57:28
Original
1630 people have browsed it

How to add text watermark to pictures using Python

How to use Python to add text watermarks to pictures

Introduction:
In image editing and processing, adding watermarks is a common way to protect Images are copyrighted and may be used commercially. As a popular programming language, Python provides a rich image processing library, making it very simple and convenient to add text watermarks to pictures. This article will introduce how to use Python to add text watermarks to pictures.

Step 1: Install the required libraries
Before we begin, we need to make sure that the required Python libraries are installed. In this example, we will use the Pillow library (an extended version of the Python Imaging Library). You can use the pip command to install the Pillow library:

pip install Pillow

Step 2: Import the required libraries
Before we start writing code, we need to import the required libraries. The following are the libraries that need to be imported:

from PIL import Image, ImageDraw, ImageFont

Step 3: Open the image file
First, we need to open an image file so that we can Add watermark. This can be achieved with the following code:

image = Image.open("image.jpg")

Please make sure to provide the correct image file path in the code.

Step 4: Create canvas and font objects
Next, we need to create a canvas object, which will be used to draw watermarks on the image. We also need to choose a suitable font to display the watermark. You can use the following code to achieve this:

draw = ImageDraw.Draw(image)
font = ImageFont.truetype("arial.ttf", size=50)

Please make sure to add Provide the correct font file path in .

Step 5: Draw the watermark
Once we have created the canvas and font objects, we can use them to draw the watermark. You can use the following code to draw a watermark:

text = "Watermark"
text_width, text_height = draw.textsize(text, font)
x = (image.width - text_width) // 2
y = (image.height - text_height) // 2
draw.text((x, y), text, font=font, fill=(255, 255, 255, 128))

In the above code, we selected a watermark text and calculated its position on the image. Then use the draw.text method to draw the watermark text on the image.

Step 6: Save the modified image
Finally, we need to save the modified image. The image can be saved to disk using the following code:

image.save("image_with_watermark.jpg")

Please make sure to provide the correct target save path in the code.

Complete code example:

from PIL import Image, ImageDraw, ImageFont

image = Image.open("image.jpg")
draw = ImageDraw.Draw (image)
font = ImageFont.truetype("arial.ttf", size=50)

text = "Watermark"
text_width, text_height = draw.textsize(text, font)
x = (image.width - text_width) // 2
y = (image.height - text_height) // 2
draw.text((x, y), text, font=font, fill=( 255, 255, 255, 128))

image.save("image_with_watermark.jpg")

Summary:
By using Python's Pillow library, we can quickly and easily Add text watermark. This article explains the basic steps for adding a text watermark and provides a complete code example. Readers can modify and expand it according to their own needs to achieve more personalized and advanced watermark effects.

The above is the detailed content of How to add text watermark to pictures using Python. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template