一個用于批量給圖片增加水印的Python庫
給圖片、視頻增加水印以確認版權或者增加效果,是在媒體內(nèi)容信息經(jīng)常需要用到的技術。本文推薦一個開源免費Python腳本,可以在指定目錄及其子目錄中批量給圖像添加水印,當然,你也可以集成到你的Web應用中。
源碼地址:https://github.com/theitrain/watermark
該Python項目依賴于Pillow庫,需要先安裝它:
pip install pillow
如何使用:
執(zhí)行該項目的腳本可以攜帶幾個參數(shù):
- 要添加水印的圖像所在的文件夾
- 要添加的水印/徽標的路徑
- 要放置水印/徽標的位置(可選)
- 要保存水印圖像的目錄(可選;如果未提供,水印圖像將覆蓋原始圖像)
- 水印/徽標周圍的填充(以像素為單位)(可選;默認值為0)
放置水印的有效位置包括:
- topleft 左上角
- topright 右上角
- bottomleft 左下角
- bottomright右下角
- center 中間(如果未指定位置,則center為默認值)
除了以上位置,其他位置都將報錯。
例子:
1.使用watermark.py,請指定位置、目標目錄,并將水印/徽標縮放為圖像寬度的30%:
python watermark.py './images' 'logo.png' --pos bottomright --new_dir './watermarked_images' --scale 30
2.使用watermark.py,請指定位置和目標目錄:
python watermark.py './images' 'logo.png' --pos bottomright --new_dir './watermarked_images'
3.在水印周圍添加填充,請執(zhí)行以下操作:
python watermark.py './images' 'logo.png' --pos topleft --padding 20