python實現(xiàn)水印圖片功能
在做一些工作的時候,有時候會涉及到給圖片加上水印,這個如果手動添加的話,效率太低了,通常選擇代碼完成。下面這個是給圖像添加文字水?。▓D片水印還在研究中)
比如,在下面的圖片中添加 “美團(tuán)外賣” 水印
from PIL import Image,ImageDraw,ImageFont import numpy as np import random import cv2 import re ? ################################################################################### class Make_Font(object): ?#### 設(shè)置文字水印 ?? ?def __init__(self,image_path,out_path,font,font_size,diaphaneity): ?? ??? ?self.image_path = image_path ?### 讀入背景圖片 ?? ??? ?self.out_path = out_path ?### 輸出水印圖片 ?? ??? ?self.font = font ?### 設(shè)置水印字內(nèi)容 ?? ??? ?self.font_size = font_size ?## 設(shè)置字體大小 ?? ??? ?self.diaphaneity = diaphaneity ?### 設(shè)置字體透明度 ? ?? ??? ?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 ? #### ?設(shè)置字體透明度 ?越小越透明 (0,100) ?? ??? ?font = ImageFont.truetype('1.ttf',self.font_size) ## 設(shè)置字體和大小 ?? ??? ?layer = image.convert('RGBA') ?## 轉(zhuǎn)換圖像格式: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)) ## 設(shè)置文本位置 ?? ??? ??? ??? ?image_draw.text((int(i),int(j)),text,font=font,fill=(255,255,255,text_diap)) ## 設(shè)置文本顏色和透明度 ?? ??? ?text_overlayer = text_overlayer.rotate(45) ? # 設(shè)置逆時針旋轉(zhuǎn)45度 ?? ??? ?####### ?設(shè)置切割點 ?############## ?? ??? ?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復(fù)合到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 = '美團(tuán)外賣',font_size = 30,diaphaneity = 90) ?? ?MK.run_make_font() ?? ?##################################################
這段代碼主要完成的是,將特定的文字水印添加圖像中,文字內(nèi)容、文字尺寸、文字透明度都可以調(diào)節(jié)(image_path為原圖像,out_path為輸出的水印圖像)
效果圖如下:
注:上傳圖像的時候,圖像進(jìn)行了壓縮。
本來準(zhǔn)備再寫一個生成圖像Logo的水印的代碼,可惜一直沒達(dá)到自己預(yù)期。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
pytorch 實現(xiàn)查看網(wǎng)絡(luò)中的參數(shù)
今天小編就為大家分享一篇pytorch 實現(xiàn)查看網(wǎng)絡(luò)中的參數(shù),具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-01-01matplotlib中plt.hist()參數(shù)解釋及應(yīng)用實例
本文主要介紹了matplotlib中plt.hist()參數(shù)解釋及應(yīng)用實例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-08-08python 下劃線的多種應(yīng)用場景總結(jié)
Python有很多地方使用下劃線,在不同場合下,有不同含義。本文總結(jié)Python語言編程中常用下劃線的地方,力圖一次搞懂下劃線的常見用法,感興趣的朋友快來一起看看吧2021-05-05基于深度學(xué)習(xí)和OpenCV實現(xiàn)目標(biāo)檢測
這篇文章主要介紹了通過使用OpenCV進(jìn)行基于深度學(xué)習(xí)的對象檢測以及使用OpenCV檢測視頻,文中的示例代碼講解詳細(xì),需要的可以參考一下2021-12-12