python實現(xiàn)水印圖片功能
在做一些工作的時候,有時候會涉及到給圖片加上水印,這個如果手動添加的話,效率太低了,通常選擇代碼完成。下面這個是給圖像添加文字水印(圖片水印還在研究中)
比如,在下面的圖片中添加 “美團外賣” 水印

from PIL import Image,ImageDraw,ImageFont
import numpy as np
import random
import cv2
import re
?
###################################################################################
class Make_Font(object): ?#### 設置文字水印
?? ?def __init__(self,image_path,out_path,font,font_size,diaphaneity):
?? ??? ?self.image_path = image_path ?### 讀入背景圖片
?? ??? ?self.out_path = out_path ?### 輸出水印圖片
?? ??? ?self.font = font ?### 設置水印字內容
?? ??? ?self.font_size = font_size ?## 設置字體大小
?? ??? ?self.diaphaneity = diaphaneity ?### 設置字體透明度
?
?? ??? ?suffix = self.out_path.split('.')[-1]
?? ??? ?match = re.match(r'png',suffix)
?? ??? ?if not match:
?? ??? ??? ?raise ValueError('The out put file name must be PNG file!')
?? ?def _text_xy(self,image_size):
?? ??? ?width, height = image_size
?? ??? ?x = random.randint(min(0, width), max(0, width)) ?#### 隨機取畫文字點
?? ??? ?y = random.randint(min(0, height), max(0, height))
?? ??? ?return x,y
?
?? ?def _draw_font_box(self,image_size,font_size):
?? ??? ?img_w,img_h = image_size
?? ??? ?font_w,font_h = font_size
?? ??? ?all_x = []
?? ??? ?x = 0
?? ??? ?all_x.append(x)
?? ??? ?while x < img_w:
?? ??? ??? ?x = font_w + 50 + x ?#### ?隔50 畫一次文字
?? ??? ??? ?all_x.append(x)
?
?? ??? ?all_y = []
?? ??? ?y = 0
?? ??? ?all_y.append(y)
?? ??? ?while y < img_h:
?? ??? ??? ?y = font_h + 50 + y ? #### ?隔50 畫一次文字
?? ??? ??? ?all_y.append(y)
?? ??? ?return all_x,all_y
?
?? ?def run_make_font(self):
?? ??? ?image = Image.open(self.image_path) ## (598,419)
?? ??? ?image_x,image_y = image.size[0:2] ## (598,419)
?? ??? ?text = self.font
?? ??? ?text_diap = self.diaphaneity ? #### ?設置字體透明度 ?越小越透明 (0,100)
?? ??? ?font = ImageFont.truetype('1.ttf',self.font_size) ## 設置字體和大小
?? ??? ?layer = image.convert('RGBA') ?## 轉換圖像格式:A為透明度 ? 尺寸(598, 419)
?
?? ??? ?max_size = max(image_x,image_y)
?? ??? ?text_overlayer = Image.new('RGBA',(2*max_size,2*max_size),(255,255,255,0)) ?## 生成同等大小的透明圖片
?
?? ??? ?image_draw = ImageDraw.Draw(text_overlayer) ## 畫圖
?? ??? ?text_size_x,text_size_y = image_draw.textsize(text,font = font) ?## 獲取文本大小
?? ??? ?#print(text_size_x,text_size_y) ? ### 字體大小 (250,50)
?? ??? ?x_count,y_count = self._draw_font_box(text_overlayer.size,(text_size_x,text_size_y))
?? ??? ?for i in x_count:
?? ??? ??? ?for j in y_count:
?? ??? ??? ??? ?#text_x,text_y = text_xy((image_x,image_y)) ## 設置文本位置
?? ??? ??? ??? ?image_draw.text((int(i),int(j)),text,font=font,fill=(255,255,255,text_diap)) ## 設置文本顏色和透明度
?? ??? ?text_overlayer = text_overlayer.rotate(45) ? # 設置逆時針旋轉45度
?? ??? ?####### ?設置切割點 ?##############
?? ??? ?box_x = (text_overlayer.size[0]-image_x)/2
?? ??? ?box_y = (text_overlayer.size[1]-image_y)/2
?? ??? ?box = [box_x,box_y,box_x+image_x,box_y+image_y]
?? ??? ?new_img = text_overlayer.crop(box)
?? ??? ?new_img = new_img.resize(layer.size)
?? ??? ?#text_overlayer.save('text_overlayer_after.png') ?## 生成的水印png圖片
?? ??? ?#new_img.save('new_img.png') ?## 生成的水印png圖片
?? ??? ?after = Image.alpha_composite(layer,new_img) ?## (im1,im2)將im2復合到im1上,返回一個Image對象
?? ??? ?after.save(self.out_path) ?### .png 可以直接保存RGBA格式
?
#########################################################################################
?
if __name__=="__main__":
?? ?############### 文字水印 ?########################
?? ?MK = Make_Font(image_path='./without_water/test5.jpg',out_path='test5_after.png',font = '美團外賣',font_size = 30,diaphaneity = 90)
?? ?MK.run_make_font()
?? ?##################################################這段代碼主要完成的是,將特定的文字水印添加圖像中,文字內容、文字尺寸、文字透明度都可以調節(jié)(image_path為原圖像,out_path為輸出的水印圖像)
效果圖如下:
注:上傳圖像的時候,圖像進行了壓縮。

本來準備再寫一個生成圖像Logo的水印的代碼,可惜一直沒達到自己預期。
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
最新PyCharm從安裝到PyCharm永久激活再到PyCharm官方中文漢化詳細教程
這篇文章涵蓋了最新版PyCharm安裝教程,最新版PyCharm永久激活碼教程,PyCharm官方中文(漢化)版安裝教程圖文并茂非常詳細,需要的朋友可以參考下2020-11-11
Python cookbook(數(shù)據(jù)結構與算法)將序列分解為單獨變量的方法
這篇文章主要介紹了Python cookbook(數(shù)據(jù)結構與算法)將序列分解為單獨變量的方法,結合實例形式分析了Python序列賦值實現(xiàn)的分解成單獨變量功能相關操作技巧,需要的朋友可以參考下2018-02-02
Python基礎之教你怎么在M1系統(tǒng)上使用pandas
這篇文章主要介紹了Python基礎之教你怎么在M1系統(tǒng)上使用pandas,文中有非常詳細的代碼示例,對正在學習python基礎的小伙伴們有很好地幫助,需要的朋友可以參考下2021-05-05
Python實現(xiàn)對比不同字體中的同一字符的顯示效果
這篇文章主要介紹了Python實現(xiàn)對比不同字體中的同一字符的顯示效果,也就是對比不同字體中某個字的顯示效果,這在做設計時非常有用,需要的朋友可以參考下2015-04-04
玩轉串口通信:利用pyserial庫,Python打開無限可能
想要學習如何使用pyserial庫實現(xiàn)串口通信嗎?這篇指南將帶你一步步了解Python中的串口通信,無論是控制硬件設備還是與外部設備進行數(shù)據(jù)交換,pyserial庫都能為你提供便捷的解決方案,快來跟著我們的指南,輕松掌握串口通信的技巧吧!2023-11-11
Django中select_related和prefetch_related的用法與區(qū)別詳解
在實際的開發(fā)中,模型之間經常存在復雜的關聯(lián)關系,下面這篇文章主要給大家介紹了關于Django中select_related和prefetch_related的用法與區(qū)別的相關資料,需要的朋友可以參考下2022-11-11

