最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

python3使用PIL添加中文文本水印背景方法詳解

 更新時間:2023年07月05日 08:44:54   作者:Mike_Zhang  
這篇文章主要介紹了python3使用PIL添加中文文本水印背景方法詳解的相關(guān)資料,需要的朋友可以參考下

環(huán)境:Windows10_x64 

Python版本 :3.9.2

Pillow版本:9.1.1

寫的博客文章被轉(zhuǎn)載且不注明出處的情況時有發(fā)生,甚至有部分轉(zhuǎn)載者將文章配圖添加自己的水??!為了保護(hù)作者勞動成果,添加水印是一個可選項(xiàng)。

今天記錄下Windows10環(huán)境下使用python3.9簡單實(shí)現(xiàn)批量添加中文文本水印背景的過程,并提供示例代碼

一、背景描述

python的PIL庫可進(jìn)行圖片處理,十分強(qiáng)大,可使用該庫實(shí)現(xiàn)圖片添加水印背景的需求。

可通過pip進(jìn)行安裝(默認(rèn)安裝最新版),命令如下:

pip install Pillow

pypi地址: https://pypi.org/project/Pillow/

文檔地址: https://pillow.readthedocs.io/en/stable/

二、具體實(shí)現(xiàn)

這里列舉下實(shí)現(xiàn)文本水印背景的關(guān)鍵點(diǎn)。

1、生成文本背景

可通過ImageDraw.text實(shí)現(xiàn):

https://pillow.readthedocs.io/en/stable/reference/ImageDraw.html

中文文本可通過設(shè)置正確的字體實(shí)現(xiàn):

font = ImageFont.truetype("simsun.ttc", fontSize,encoding="utf-8")

文本顏色可通過RGB值設(shè)置,示例如下:

fill=(106,106,106)

2、旋轉(zhuǎn)文本

 可通過rotate函數(shù)實(shí)現(xiàn):

https://pillow.readthedocs.io/en/stable/reference/Image.html

 3、設(shè)置水印

可通過Image.paste函數(shù)實(shí)現(xiàn):

https://pillow.readthedocs.io/en/stable/reference/Image.html

 4、生成水印背景

1)需要通過循環(huán)控制,多次設(shè)置背景圖片;

i,j = 0,0
while True:
    x,y = i*step,i*step
    if y < height : 
        x = 0
    if y > height : 
        x = j*step
        j = j + 1
        y = height - 10
    #print(i,"xy :",x,y)
    draw_text(img,(x,y),fill,mask,rotated_mask)
    if (x + step > width ) and (y + step > height ) : break
    i = i + 1

2)導(dǎo)出時需要添加質(zhì)量參數(shù),避免導(dǎo)出的圖片失真;

img.save(dstFile,optimize=True, quality=100)

5、多進(jìn)程加速

批量添加文本水印背景時,可使用進(jìn)程池進(jìn)行加速。

pool = Pool(processes=8)    # set the processes max number 
for root, dirs, files in os.walk(srcDir):
    for name in files:
        srcFile = os.path.join(root, name)
        dstFile = os.path.join(dstDir, name)
        print("%s => %s" % (srcFile,dstFile))
        # add_watermark(srcFile,dstFile,fontSize,myText,angle,fill,step)
        result = pool.apply_async(add_watermark,(srcFile,dstFile,fontSize,myText,angle,fill,step))
pool.close()
pool.join()

 

三、運(yùn)行效果

這里演示下python3使用PIL添加中文文本水印背景的運(yùn)行效果,具體如下:

四、資源下載

到此這篇關(guān)于python3使用PIL添加中文文本水印背景方法詳解的文章就介紹到這了,更多相關(guān)python3使用PIL添加水印背景內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論

洛隆县| 德庆县| 阜南县| 贡觉县| 清镇市| 桂林市| 曲周县| 东方市| 石家庄市| 平定县| 东莞市| 万山特区| 惠来县| 桂东县| 潼关县| 九台市| 如东县| 奉化市| 兰西县| 阿尔山市| 抚松县| 古交市| 科尔| 灵武市| 咸阳市| 忻州市| 垫江县| 蒙阴县| 侯马市| 佛山市| 邢台县| 滁州市| 大厂| 阳山县| 故城县| 邵东县| 新竹县| 黔南| 铜陵市| 曲松县| 岑巩县|