亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

使用Python中PIL庫給圖片添加文本水印

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

前言

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

一、PIL是什么?

PIL是Python Imaging Library的縮寫,它是Python語言中常用的圖像處理庫之一。它提供了豐富的圖像處理功能,包括打開、保存、裁剪、旋轉(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庫給圖片添加文本水印

1.引入庫

from PIL import Image, ImageDraw, ImageFont

2.打開圖片文件

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

3.新建一個Draw對象

draw = ImageDraw.Draw(image)

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

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

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

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

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

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

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

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

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

5.4通過十六進制設(shè)置顏色

# 通過十六進制設(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的值即可。這個30是我這樣設(shè)置的,你也可以根據(jù)自己的喜好來調(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庫給圖片添加文本水印的文章就介紹到這了,更多相關(guān)Python PIL庫添加文本水印內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論