網站首頁 編程語言 正文
本文實例為大家分享了Python使用Pillow添加水印的具體代碼,供大家參考,具體內容如下
python數據分析得到的圖片,并對照片添加水印pillow
PIL包含在pillow中,所以如果要從PIL導入相關庫需要安裝pillow
# install?
pip install pillow
安裝完之后就可以下面的頭文件來操作了
from PIL import Image, ImageDraw
采用函數式編程:
#!/usr/bin/python
from PIL import Image, ImageDraw
from matplotlib import pyplot as plt, font_manager
# 保存通過數據分析得到的圖片
def save_img():
?? ?# mac系統(tǒng)下查詢包含中文字體的方式是命令fc-list :lang=zh
?? ?my_font = font_manager.FontProperties(?? ??? ?fname="/System/Library/Assets/com_apple_MobileAsset_Font5/b2d7b382c0fbaa5777103242eb048983c40fb807.asset/AssetData/Kaiti.ttc")
?? ?x = [i for i in range(11, 31)]
?? ?y_1 = [1, 0, 1, 1, 2, 4, 3, 2, 3, 4, 4, 5, 6, 5, 4, 3, 3, 1, 1, 1]
?? ?y_2 = [0, 0, 0, 1, 2, 4, 3, 2, 3, 4, 2, 2, 1, 1, 1, 1, 1, 1, 1, 1]
?? ?# 做出兩個圖,分別包括的是自己和同桌的每個年齡點交的女朋友數量
?? ?plt.plot(x, y_1, label="自己", color="cyan", linestyle="--")
?? ?plt.plot(x, y_2, label="同桌", color="r")
?? ?
?? ?_xtick_labels = ["{}歲".format(i) for i in x]
?? ?plt.title("每年交的女朋友的數量走勢圖", fontproperties=my_font)
?? ?# 給x軸添加刻度
?? ?plt.xticks(x, _xtick_labels, fontproperties=my_font, rotation=45)
?? ?# 給y軸添加刻度
?? ?plt.yticks(range(min(y_1), max(y_1) + 1))
?? ?# 給xy軸添加說明
?? ?plt.xlabel("年齡", fontproperties=my_font)
?? ?plt.ylabel("數量", fontproperties=my_font)
?? ?# 做出網格更加直觀看出坐標點值
?? ?plt.grid(alpha=0.4, linestyle=":")
?? ?# 添加圖例
?? ?plt.legend(prop=my_font)
?? ?plt.savefig("./girl.png")
?? ?plt.show()?
# 對保存的圖片進行添加
def add_watermark(a):
?? ?im = Image.open(a)
?? ?draw = ImageDraw.Draw(im)
?? ?
?? ?# 這里的hello world是添加的水印
?? ?# 數組(50,50)表示的是水印要添加的位置?
?? ?draw.text((50, 50), 'hello world'.encode('utf-8'), fill=(1, 0, 0)) ?
?? ?im.show()
if __name__ == '__main__':
?? ?save_img()
?? ?add_watermark("girl.png")
代碼輸出的結果:
注意hello world 就是添加的水印
原文鏈接:https://blog.csdn.net/lc574260570/article/details/104208033
相關推薦
- 2022-07-02 python中如何設置list步長_python
- 2022-10-19 python用opencv將標注提取畫框到對應的圖像中_python
- 2022-06-28 python神經網絡使用Keras進行模型的保存與讀取_python
- 2022-04-23 Windows中將Nginx添加為服務的問題_nginx
- 2024-01-07 SpringData Jpa 之 修改、刪除數據
- 2022-02-10 antd table 增加底部合計行統(tǒng)計
- 2023-07-14 react 利用插件react-draggable實現拖拽功能
- 2023-07-07 Linux服務器zip安裝,及壓縮解壓
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細win安裝深度學習環(huán)境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發(fā)現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態(tài)字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支