欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

使用Python中PIL庫(kù)給圖片添加文本水印

 更新時(shí)間:2023年04月20日 08:39:19   作者:空空star  
有時(shí)候我們需要添加一定的水印以給自己的圖片添加先關(guān)的標(biāo)記,在Python中有相關(guān)的計(jì)算函數(shù),下面這篇文章主要給大家介紹了關(guān)于使用Python中PIL庫(kù)給圖片添加文本水印的相關(guān)資料,需要的朋友可以參考下

前言

大家好,本篇給大家分享一下通過(guò)Python的PIL庫(kù)給圖片添加文本水印。

一、PIL是什么?

PIL是Python Imaging Library的縮寫(xiě),它是Python語(yǔ)言中常用的圖像處理庫(kù)之一。它提供了豐富的圖像處理功能,包括打開(kāi)、保存、裁剪、旋轉(zhuǎn)、縮放等操作,并支持多種圖像格式。

二、安裝PIL

pip install pillow

三、查看PIL版本

pip show pillow

Name: Pillow
Version: 9.4.0
Summary: Python Imaging Library (Fork)
Home-page: https://python-pillow.org
Author: Alex Clark (PIL Fork Author)
Author-email: aclark@python-pillow.org
License: HPND
Requires:
Required-by: image, imageio, matplotlib, pytesseract, wordcloud

四、使用PIL庫(kù)給圖片添加文本水印

1.引入庫(kù)

from PIL import Image, ImageDraw, ImageFont

2.打開(kāi)圖片文件

local = '/Users/kkstar/Downloads/video/pic/'
image = Image.open(local+"demo.jpg")

3.新建一個(gè)Draw對(duì)象

draw = ImageDraw.Draw(image)

4.設(shè)置水印文字、字體、大小

text = '@空空star'
font = ImageFont.truetype('STHeitiMedium.ttc', size=80)

5.設(shè)置水印顏色

5.1通過(guò)名稱設(shè)置顏色

# 通過(guò)名稱設(shè)置顏色-黃色
color = 'yellow'

5.2通過(guò)RGB值設(shè)置顏色

# 通過(guò)RGB值設(shè)置顏色-紅色
color = (255, 0, 0)

5.3通過(guò)RGBA值設(shè)置顏色

# 通過(guò)RGBA值設(shè)置顏色-白色
color = (255,255,255,0)

5.4通過(guò)十六進(jìn)制設(shè)置顏色

# 通過(guò)十六進(jìn)制設(shè)置顏色-綠色
color = '#6FE000'

6.獲取水印文字的尺寸

text_width, text_height = draw.textsize(text, font)

7.設(shè)置水印位置

7.1左上

x = 30
y = 30

7.2右下

x = image.width-text_width-30
y = image.height-text_height-30

其他位置調(diào)整x、y的值即可。這個(gè)30是我這樣設(shè)置的,你也可以根據(jù)自己的喜好來(lái)調(diào)整。

8.添加水印

draw.text((x, y), text, font=font, fill=color)

9.保存圖片

image.save(local+'image_with_watermark.jpg')

總結(jié)

到此這篇關(guān)于使用Python中PIL庫(kù)給圖片添加文本水印的文章就介紹到這了,更多相關(guān)Python PIL庫(kù)添加文本水印內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論