zoukankan      html  css  js  c++  java
  • 用Python给你的博客加上水印

      之前写的博客里面用到的图片都没有加水印,今天才在别的网站上发现自己的博客居然一个字不动的出现在了别人的文章里,而且还不标注出处,简直醉了。

      其实博客这种东西讲真我是很愿意让别人看得,因为自己写的也比较水,但是好歹你也标注一下原作者啊!也没有什么好的方法可以防止别人转载不注明出处,无奈,只好给图片加上个水印了。

      翻了一下博客园的设置,没找到如何在上传图片的时候自动加上水印,谷歌了一下也没有结果,这个时候就又想起了万能的python了。

      思路比较简单,先新建一张跟原图一样大小的白色图片,在上面加上水印,然后将两张图片融合就好了。

      已经添加了对不同大小图片的位置和字体自适应,结果如图:

      

    代码如下:

     1 # -*- coding: utf-8 -*-
     2 import sys
     3 import os
     4 from PIL import Image, ImageDraw, ImageFont  
     5 import time  
     6 
     7 exts = ['.jpg','.jpeg','.png','.JPG','.JPEG','.PNG']
     8 
     9 blog_name = 'christ_song'
    10 blog_site = 'http://www.cnblogs.com/christsong/'
    11  
    12 def watermark(fname): 
    13     """Adds a watermark to an image."""  
    14     img = Image.open(fname).convert('RGBA') 
    15 
    16     #make a blank image for the text, initialized to transparent text color  
    17     img_ed = Image.new('RGBA', img.size, (255,255,255,0))  
    18 
    19     w,h = img.size
    20     font_time_size = w // 10
    21     font_time = ImageFont.truetype('STCAIYUN.TTF', font_time_size)#不同的电脑可能不存在这里的字体,导致最后没有输出结果;如果你的电脑上没有这几种字体,请自行修改
    22     font_name_size = w // 15
    23     font_name = ImageFont.truetype('STENCIL.TTF',  font_name_size)
    24     font_blog_size = w // 21
    25     font_blog = ImageFont.truetype('STLITI.TTF',   font_blog_size)     
    26     
    27     date = time.strftime("%Y/%m/%d")
    28 
    29     #draw the watermark on the empty pic at a specified position  
    30     img_draw = ImageDraw.Draw(img_ed)  
    31     img_draw.text((w - font_name_size*7, h - font_blog_size - font_time_size - font_name_size), blog_name, font = font_name, fill = (255,255,255,125))
    32     img_draw.text((w - font_time_size*5.3, h - font_blog_size - font_time_size), date, font = font_time, fill = (255,0,0,255))  
    33     img_draw.text((w - font_blog_size*13.1, h - font_blog_size), blog_site, font = font_blog, fill = (255,255,255,200))
    34   
    35     out = Image.alpha_composite(img, img_ed)  
    36     out.save(fname)  
    37     print("saved %s as jpg" % os.path.basename(fname))  
    38   
    39   
    40 if __name__ == '__main__': 
    41     for root,dirs,files in os.walk(os.getcwd()):
    42         for f in files:
    43             # Check the sub directorys
    44             fname = (root + '\'+ f).lower()
    45             if os.path.splitext(f)[1]:
    46                 ext = f[f.rindex('.'):]
    47             try:
    48                 if(exts.index(ext) >= 0):
    49                     watermark(fname)
    50             except:
    51                 pass 
    52     print("done!")
    53       
  • 相关阅读:
    口语详解|为什么“how to say”是错的?
    9 tips to improve spoken english
    splash 安装
    ubuntu 安装NVIDIA驱动过程
    【Python数据分析】时间模块datetime
    【Python数据分析】Pandas模块下的Series与DataFrame
    【Python】文件
    博客园Markdown样式美化
    【Python】异常处理
    【Python】eval函数
  • 原文地址:https://www.cnblogs.com/christsong/p/5716287.html
Copyright © 2011-2022 走看看