python實現(xiàn)水印圖片功能_第1頁
python實現(xiàn)水印圖片功能_第2頁
python實現(xiàn)水印圖片功能_第3頁
python實現(xiàn)水印圖片功能_第4頁
python實現(xiàn)水印圖片功能_第5頁
全文預覽已結(jié)束

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領(lǐng)

文檔簡介

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

比如,在下面的圖片中添加美團外賣水印

fromPILimportImage,ImageDraw,ImageFont

importnumpyasnp

importrandom

importcv2

importre

###################################################################################

classMake_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)

ifnotmatch:

raiseValueError('TheoutputfilenamemustbePNGfile!')

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))

returnx,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)

whileximg_w:

x=font_w+50+x

####

隔50畫一次文字

all_x.append(x)

all_y=[]

y=0

all_y.append(y)

whileyimg_h:

y=font_h+50+y

####

隔50畫一次文字

all_y.append(y)

returnall_x,all_y

defrun_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)

###字體大?。?50,50)

x_count,y_count=self._draw_font_box(text_overlayer.size,(text_size_x,text_size_y))

foriinx_count:

forjiny_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復合到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()

##################################################

這段代碼主要完成的是

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責。
  • 6. 下載文件中如有侵權(quán)或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

最新文檔

評論

0/150

提交評論