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

使用python PIL庫批量對圖片添加水印的過程詳解

 更新時間:2023年11月22日 08:56:49   作者:蟲無涯  
平常我們想給某些圖片添加文字水印,方法有很多,也有很多的工具可以方便的進(jìn)行,今天主要是對PIL庫的應(yīng)用,結(jié)合Python語言批量對圖片添加水印,文章通過代碼示例給大家介紹的非常詳細(xì),感興趣的同學(xué)可以參考一下

1 簡單引入

  • 平常我們想給某些圖片添加文字水印,方法有很多,也有很多的工具可以方便的進(jìn)行;
  • 今天主要是對PIL庫的應(yīng)用,結(jié)合Python語言批量對圖片添加水印;
  • 這里需要注意的是圖片的格式,不能為JPG或JPEG,因為這兩種格式的圖片不支持透明度設(shè)置。

2 關(guān)于PIL

  • PILPython的一個圖像處理庫,支持多種文件格式;
  • PIL提供強大的圖像處理和圖形處理能力;
  • 可完成對圖像的縮放、裁剪、疊加以及圖像添加線條、圖像和文字等操作。
  • 安裝的話,使用以下命令:
pip install Pillow

3 本文涉及的PIL的幾個類

模塊或類說明
image模塊用于對圖像就行處理
ImageDraw2D圖像對象
ImageFont存儲字體
ImageEnhance圖像增強

4 實現(xiàn)原理

本文主要目的是批量對某個文件夾下的圖片進(jìn)行添加水印,原理如下:

  • 對水印內(nèi)容設(shè)置;
  • 使用Image對象的open()方法打開原始圖片;
  • 使用Image對象的new()方法新建存儲水印圖片對象;
  • 使用Image.Draw.Draw對象的text()繪制水印文字;
  • 使用ImageEnhance中的Brightness中的enhance()設(shè)置水印透明度。

5 實現(xiàn)過程

5.1 原始圖片

  • 我們設(shè)置一個原始圖片的存儲目錄,比如:
F:\python_study\image\image01

這個文件夾中存放了多個png格式的圖片:

這里的一個原始圖片為:

5.2 導(dǎo)入相關(guān)模塊

  • 導(dǎo)入需要的幾個PIL中的模塊或類:
from PIL import Image, ImageDraw, ImageFont, ImageEnhance
import os

5.3 初始化數(shù)據(jù)

  • 我們這里預(yù)想的是,通過用戶自己手動輸入相關(guān)信息,比如圖片的存儲路徑,需要設(shè)置的水印文字,通過輸入的方式設(shè)置水印的位置,設(shè)置水印的透明度等;
class TestText():
    def __init__(self):
        super(TestText, self).__init__()
        self.pic_path = input('圖片路徑:')
        self.pic_text = input('水印文字:')
        self.p_flag = int(input('水印位置(1:左上角,2:左下角,3:右上角,4:右下角,5:居中):'))
        self.a = float(input('水印透明度(0—1之間的1位小數(shù)):'))

5.4 水印字體設(shè)置

  • 這里我們先看下ImageFont.truetype源碼:
def truetype(font=None, size=10, index=0, encoding="", layout_engine=None):
    """
    Load a TrueType or OpenType font from a file or file-like object,
    and create a font object.
    This function loads a font object from the given file or file-like
    object, and creates a font object for a font of the given size.

    Pillow uses FreeType to open font files. On Windows, be aware that FreeType
    will keep the file open as long as the FreeTypeFont object exists. Windows
    limits the number of files that can be open in C at once to 512, so if many
    fonts are opened simultaneously and that limit is approached, an
    ``OSError`` may be thrown, reporting that FreeType "cannot open resource".
    A workaround would be to copy the file(s) into memory, and open that instead.

    This function requires the _imagingft service.

    :param font: A filename or file-like object containing a TrueType font.
                 If the file is not found in this filename, the loader may also
                 search in other directories, such as the :file:`fonts/`
                 directory on Windows or :file:`/Library/Fonts/`,
                 :file:`/System/Library/Fonts/` and :file:`~/Library/Fonts/` on
                 macOS.

    :param size: The requested size, in pixels.
    :param index: Which font face to load (default is first available face).
    :param encoding: Which font encoding to use (default is Unicode). Possible
                     encodings include (see the FreeType documentation for more
                     information):

  • 它的大意就是從系統(tǒng)中的字體庫中讀取字體;
  • 那我們的系統(tǒng)字體在哪里呢?
  • 系統(tǒng)字體在
C:\Windows\Fonts

如下:

  • 我們隨便選一個自己喜歡的字體,復(fù)制下名字即可:
self.font = ImageFont.truetype("cambriab.ttf", size=35)

5.5 打開原始圖片并新建存儲對象

  • 打開原始圖片,并轉(zhuǎn)換為RGB
image = Image.open(img).convert('RGBA') 
  • 創(chuàng)建繪制對象:
new_img = Image.new('RGBA', image.size, (255, 255, 255, 0))
image_draw = ImageDraw.Draw(new_img)

5.6 計算圖片和水印的大小

  • 圖片大?。?/li>
w, h = image.size  
  • 文字大小:
w1 = self.font.getsize(self.pic_text)[0]  # 獲取字體寬度
h1 = self.font.getsize(self.pic_text)[1]  # 獲取字體高度

5.7 選擇性設(shè)置水印文字

  • 通過if語句來實現(xiàn):
 if self.p_flag == 1:  # 左上角
      location = (0, 0)
 elif self.p_flag == 2:  # 左下角
      location = (0, h - h1)
 elif self.p_flag == 3:  # 右上角
      location = (w - w1, 0)
 elif self.p_flag == 4:  # 右下角
      location = (w - w1, h - h1)
 elif self.p_flag == 5:  # 居中
      location = (h/2, h/2)

5.8 繪制文字并設(shè)置透明度

  • 繪制文字:
image_draw.text(location, self.pic_text, font=self.font, fill="blue")
  • 設(shè)置透明度
 transparent = new_img.split()[3]
 transparent = ImageEnhance.Brightness(transparent).enhance(self.a)
 new_img.putalpha(transparent)

 Image.alpha_composite(image, new_img).save(img)

5.9 遍歷獲取圖片文件并調(diào)用繪制方法

 text_pic = TestText()
    try:
        file_list = os.listdir(text_pic.pic_path)
        for i in range(0, len(file_list)):
            filepath = os.path.join(text_pic.pic_path, file_list[i])  
            if os.path.isfile(filepath): 
                filetype = os.path.splitext(filepath)[1] 
                if filetype == '.png': 
                    text_pic.test_text(filepath) 
                else:
                    print("圖片格式有誤,無法添加水印,請使用png格式圖片")
        print('批量添加水印完成')
    except:
        print('輸入的文件路徑有誤,請檢查~~')

6 完整源碼

# -*- coding:utf-8 -*-
# 作者:蟲無涯
# 日期:2023/11/21 
# 文件名稱:test_text_pic.py
# 作用:批量給圖片添加文字水印和圖片水印
# 聯(lián)系:VX(NoamaNelson)
# 博客:https://blog.csdn.net/NoamaNelson


from PIL import Image, ImageDraw, ImageFont, ImageEnhance
import os


class TestText():
    def __init__(self):
        super(TestText, self).__init__()
        self.pic_path = input('圖片路徑:')
        self.pic_text = input('水印文字:')
        self.p_flag = int(input('水印位置(1:左上角,2:左下角,3:右上角,4:右下角,5:居中):'))
        self.a = float(input('水印透明度(0—1之間的1位小數(shù)):'))

        # 設(shè)置字體
        self.font = ImageFont.truetype("cambriab.ttf", size=35)

    # 文字水印
    def test_text(self, img):
        global location
        image = Image.open(img).convert('RGBA') 
        new_img = Image.new('RGBA', image.size, (255, 255, 255, 0)) 
        image_draw = ImageDraw.Draw(new_img) 
        w, h = image.size  # 圖片大小
        w1 = self.font.getsize(self.pic_text)[0]  # 字體寬度
        h1 = self.font.getsize(self.pic_text)[1]  # 字體高度

        # 設(shè)置水印文字位置
        if self.p_flag == 1:  # 左上角
            location = (0, 0)
        elif self.p_flag == 2:  # 左下角
            location = (0, h - h1)
        elif self.p_flag == 3:  # 右上角
            location = (w - w1, 0)
        elif self.p_flag == 4:  # 右下角
            location = (w - w1, h - h1)
        elif self.p_flag == 5:  # 居中
            location = (h/2, h/2)
        # 繪制文字
        image_draw.text(location, self.pic_text, font=self.font, fill="blue")

        # 設(shè)置透明度
        transparent = new_img.split()[3]
        transparent = ImageEnhance.Brightness(transparent).enhance(self.a)
        new_img.putalpha(transparent)

        Image.alpha_composite(image, new_img).save(img)


if __name__ == "__main__":
    text_pic = TestText()
    try:
        file_list = os.listdir(text_pic.pic_path) 
        for i in range(0, len(file_list)): 
            filepath = os.path.join(text_pic.pic_path, file_list[i])
            if os.path.isfile(filepath): 
                filetype = os.path.splitext(filepath)[1] 
                if filetype == '.png': 
                    text_pic.test_text(filepath) 
        print('批量添加水印完成')
    except:
        print('輸入的文件路徑有誤,請檢查~~')

7 顯示效果

  • 運行過程:

D:\Python37\python.exe F:/python_study/python_project/test_text_pic.py
圖片路徑:F:\python_study\image\image01
水印文字:@蟲無涯
水印位置(1:左上角,2:左下角,3:右上角,4:右下角,5:居中):1
水印透明度(0—1之間的1位小數(shù)):0.5
F:/python_study/python_project/test_text_pic.py:32: DeprecationWarning: getsize is deprecated and will be removed in Pillow 10 (2023-07-01). Use getbbox or getlength instead.
  w1 = self.font.getsize(self.pic_text)[0]  # 獲取字體寬度
F:/python_study/python_project/test_text_pic.py:33: DeprecationWarning: getsize is deprecated and will be removed in Pillow 10 (2023-07-01). Use getbbox or getlength instead.
  h1 = self.font.getsize(self.pic_text)[1]  # 獲取字體高度
批量添加水印完成

不同位置的水印效果:

居中效果:

以上就是使用python PIL庫批量對圖片添加水印的過程詳解的詳細(xì)內(nèi)容,更多關(guān)于python PIL庫圖片添加水印的資料請關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • python實現(xiàn)Floyd算法

    python實現(xiàn)Floyd算法

    這篇文章主要為大家詳細(xì)介紹了python實現(xiàn)Floyd算法,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-01-01
  • Python腳本實現(xiàn)下載合并SAE日志

    Python腳本實現(xiàn)下載合并SAE日志

    這篇文章主要介紹了Python腳本實現(xiàn)下載合并SAE日志,本文講解了代碼編寫過程,然后給出了完整代碼,需要的朋友可以參考下
    2015-02-02
  • 人工智能學(xué)習(xí)Pytorch張量數(shù)據(jù)類型示例詳解

    人工智能學(xué)習(xí)Pytorch張量數(shù)據(jù)類型示例詳解

    這篇文章主要為大家介紹了人工智能學(xué)習(xí)Pytorch張量數(shù)據(jù)類型的示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步
    2021-11-11
  • Restful_framework視圖組件代碼實例解析

    Restful_framework視圖組件代碼實例解析

    這篇文章主要介紹了Restful_framework視圖組件代碼實例解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2020-11-11
  • 使用Python實現(xiàn)惰性加載的三種方法詳解

    使用Python實現(xiàn)惰性加載的三種方法詳解

    在現(xiàn)代軟件開發(fā)中,惰性加載是一種優(yōu)化技術(shù),用于延遲計算或加載資源密集型屬性,直至首次訪問時才執(zhí)行,Python提供了豐富的元類與屬性操作機(jī)制,允許我們通過getattr、getattribute和setattr方法實現(xiàn)惰性屬性管理,本文將詳細(xì)探討這三種方法的實現(xiàn)細(xì)節(jié),需要的朋友可以參考下
    2025-10-10
  • pytorch 如何實現(xiàn)HWC轉(zhuǎn)CHW

    pytorch 如何實現(xiàn)HWC轉(zhuǎn)CHW

    這篇文章主要介紹了pytorch HWC轉(zhuǎn)CHW的實現(xiàn)方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-05-05
  • Python+OpenCV 圖像邊緣檢測四種實現(xiàn)方法

    Python+OpenCV 圖像邊緣檢測四種實現(xiàn)方法

    本文主要介紹了通過OpenCV中Sobel算子、Schaar算子、Laplacian算子以及Canny分別實現(xiàn)圖像邊緣檢測并總結(jié)了四者的優(yōu)缺點,感興趣的同學(xué)可以參考一下
    2021-11-11
  • pyinstaller參數(shù)介紹以及總結(jié)詳解

    pyinstaller參數(shù)介紹以及總結(jié)詳解

    這篇文章主要介紹了pyinstaller參數(shù)介紹以及總結(jié)詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-07-07
  • 基于Python+OpenCV實現(xiàn)自動掃雷功能

    基于Python+OpenCV實現(xiàn)自動掃雷功能

    相信許多人很早就知道有掃雷這么一款經(jīng)典的游(顯卡測試)戲(軟件),掃雷作為一款在Windows9x時代就已經(jīng)誕生的經(jīng)典游戲,從過去到現(xiàn)在依然都有著它獨特的魅力,所以本文小編給大家介紹了如何使用Python+OpenCV實現(xiàn)自動掃雷效果,感興趣的朋友可以參考下
    2023-12-12
  • Django 如何獲取前端發(fā)送的頭文件詳解(推薦)

    Django 如何獲取前端發(fā)送的頭文件詳解(推薦)

    這篇文章主要介紹了Django 如何獲取前端發(fā)送的頭文件,先需要導(dǎo)入一個類再通過request.environ來獲取,具體操作可查看下文,需要的朋友可以參考下
    2017-08-08

最新評論

松滋市| 龙州县| 平湖市| 静宁县| 会泽县| 大庆市| 扶沟县| 武城县| 尉氏县| 洱源县| 吴川市| 金乡县| 北海市| 宜城市| 宁安市| 台东县| 石棉县| 安徽省| 成安县| 永宁县| 于都县| 绍兴县| 聂荣县| 丰城市| 延川县| 沅陵县| 滦平县| 聊城市| 托克逊县| 黔西县| 天台县| 盐边县| 合水县| 杨浦区| 天气| 商都县| 开平市| 托里县| 额尔古纳市| 武清区| 呼和浩特市|