欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

python實現(xiàn)水印圖片功能

 更新時間:2022年05月06日 16:36:30   作者:月逝天  
這篇文章主要為大家詳細(xì)介紹了python實現(xiàn)水印圖片功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下

在做一些工作的時候,有時候會涉及到給圖片加上水印,這個如果手動添加的話,效率太低了,通常選擇代碼完成。下面這個是給圖像添加文字水?。▓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ù)

    今天小編就為大家分享一篇pytorch 實現(xiàn)查看網(wǎng)絡(luò)中的參數(shù),具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-01-01
  • python如何刪除文件中重復(fù)的字段

    python如何刪除文件中重復(fù)的字段

    這篇文章主要為大家詳細(xì)介紹了python如何刪除文件中重復(fù)的字段,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2019-07-07
  • Python API自動化框架總結(jié)

    Python API自動化框架總結(jié)

    在本篇文章里小編給大家整理的是關(guān)于Python API自動化框架總結(jié)內(nèi)容,需要的朋友們學(xué)習(xí)下。
    2019-11-11
  • Python實現(xiàn)Word的讀寫改操作

    Python實現(xiàn)Word的讀寫改操作

    本文主要介紹了運用docx模塊實現(xiàn)讀取Word,調(diào)整Word樣式以及Word 寫入操作的示例代碼,希望對大家有所幫助。一起跟隨小編過來看看吧
    2021-11-11
  • matplotlib中plt.hist()參數(shù)解釋及應(yīng)用實例

    matplotlib中plt.hist()參數(shù)解釋及應(yīng)用實例

    本文主要介紹了matplotlib中plt.hist()參數(shù)解釋及應(yīng)用實例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2022-08-08
  • 用Python的Turtle制作自己的星空

    用Python的Turtle制作自己的星空

    這篇文章主要介紹了用Python的Turtle制作自己的星空,本文用了turtle繪圖包,是一款非常強大的內(nèi)置包,需要的朋友可以參考下
    2023-04-04
  • python如何實現(xiàn)數(shù)組元素兩兩相加

    python如何實現(xiàn)數(shù)組元素兩兩相加

    這篇文章主要介紹了python如何實現(xiàn)數(shù)組元素兩兩相加,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-05-05
  • python 下劃線的多種應(yīng)用場景總結(jié)

    python 下劃線的多種應(yīng)用場景總結(jié)

    Python有很多地方使用下劃線,在不同場合下,有不同含義。本文總結(jié)Python語言編程中常用下劃線的地方,力圖一次搞懂下劃線的常見用法,感興趣的朋友快來一起看看吧
    2021-05-05
  • python set集合使用方法解析

    python set集合使用方法解析

    這篇文章主要介紹了python set集合使用方法解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2019-11-11
  • 基于深度學(xué)習(xí)和OpenCV實現(xiàn)目標(biāo)檢測

    基于深度學(xué)習(xí)和OpenCV實現(xiàn)目標(biāo)檢測

    這篇文章主要介紹了通過使用OpenCV進(jìn)行基于深度學(xué)習(xí)的對象檢測以及使用OpenCV檢測視頻,文中的示例代碼講解詳細(xì),需要的可以參考一下
    2021-12-12

最新評論