16 lines of code to convert webp to jpg in python and add full-screen watermark to pictures

./ppppp/*.jpg is the path for watermarking, just put the picture in this path

webp to jpg is in the path of ./*.webp

import glob,os
from PIL import Image,ImageFont,ImageDraw,ImageEnhance,ImageOps
list(map(lambda f:(Image.open(f).save(f[0:-5]+'.jpg'),os.remove(f)),glob.glob('./*.webp')))
for n in glob.glob('./ppppp/*.jpg'):
 im=ImageOps.exif_transpose(Image.open(n))
 mark=Image.new('RGBA',(len('我是水印')*30,round(30*1.2))) #生成文字,透明度
 ImageDraw.Draw(mark).text((0,0),'我是水印','#946DC6',ImageFont.truetype('C:/Windows/Fonts/msyhbd',30))
 mark.putalpha(ImageEnhance.Brightness(mark.split()[3]).enhance(0.3))
 c=int((im.size[0]**2+im.size[1]**2)**0.5) #以斜边长度为宽高创建大图(旋转后大图才足以覆盖原图)
 y,idx,mark2=0,0,Image.new('RGBA',(c,c))
 while y<c:
  x,idx=-int((mark.size[0]+75)*0.5*idx),(idx+1)%2
  while x<c:mark2.paste(mark,(x,y));x=x+mark.size[0]+75
  y=y+mark.size[1]+75
 im.paste(mark2.rotate(30),(int((im.size[0]-c)/2),int((im.size[1]-c)/2)),mark2.rotate(30))
 im.save('.\\'+os.path.basename(n)),os.remove(n)

Guess you like

Origin blog.csdn.net/LingLing1301/article/details/128736779