python使用PIL模块实现给图片打水印的方法


Posted in Python onMay 22, 2015

本文实例讲述了python使用PIL模块实现给图片打水印的方法。分享给大家供大家参考。具体实现方法如下:

import Image, ImageEnhance
def reduce_opacity(im, opacity):
  """Returns an image with reduced opacity."""
  assert opacity >= 0 and opacity <= 1
  if im.mode != 'RGBA':
    im = im.convert('RGBA')
  else:
    im = im.copy()
  alpha = im.split()[3]
  alpha = ImageEnhance.Brightness(alpha).enhance(opacity)
  im.putalpha(alpha)
  return im
def watermark(im, mark, position, opacity=1):
  """Adds a watermark to an image."""
  if opacity < 1:
    mark = reduce_opacity(mark, opacity)
  if im.mode != 'RGBA':
    im = im.convert('RGBA')
  # create a transparent layer the size of the image and draw the
  # watermark in that layer.
  layer = Image.new('RGBA', im.size, (0,0,0,0))
  if position == 'tile':
    for y in range(0, im.size[1], mark.size[1]):
      for x in range(0, im.size[0], mark.size[0]):
        layer.paste(mark, (x, y))
  elif position == 'scale':
    # scale, but preserve the aspect ratio
    ratio = min(
      float(im.size[0]) / mark.size[0], float(im.size[1]) / mark.size[1])
    w = int(mark.size[0] * ratio)
    h = int(mark.size[1] * ratio)
    mark = mark.resize((w, h))
    layer.paste(mark, ((im.size[0] - w) / 2, (im.size[1] - h) / 2))
  else:
    layer.paste(mark, position)
  # composite the watermark with the layer
  return Image.composite(layer, im, layer)
def test():
  im = Image.open('test.png')
  mark = Image.open('overlay.png')
  watermark(im, mark, 'tile', 0.5).show()
  watermark(im, mark, 'scale', 1.0).show()
  watermark(im, mark, (100, 100), 0.5).show()
if __name__ == '__main__':
  test()

希望本文所述对大家的Python程序设计有所帮助。

Python 相关文章推荐
python在命令行下使用google翻译(带语音)
Jan 16 Python
Python中输出ASCII大文字、艺术字、字符字小技巧
Apr 28 Python
Python的Scrapy爬虫框架简单学习笔记
Jan 20 Python
Windows和Linux下Python输出彩色文字的方法教程
May 02 Python
Python内存管理方式和垃圾回收算法解析
Nov 11 Python
Python闭包之返回函数的函数用法示例
Jan 27 Python
python中的字符串内部换行方法
Jul 19 Python
详解Django的CSRF认证实现
Oct 09 Python
python3中TQDM库安装及使用详解
Nov 18 Python
利用python查看数组中的所有元素是否相同
Jan 08 Python
教你怎么用Python selenium操作浏览器对象的基础API
Jun 23 Python
Django基础CBV装饰器和中间件
Mar 22 Python
python实现读取命令行参数的方法
May 22 #Python
Python中返回字典键的值的values()方法使用
May 22 #Python
python复制文件的方法实例详解
May 22 #Python
在Python中操作字典之update()方法的使用
May 22 #Python
python判断图片宽度和高度后删除图片的方法
May 22 #Python
在Python中操作字典之setdefault()方法的使用
May 21 #Python
在Python中用keys()方法返回字典键的教程
May 21 #Python
You might like
php中用foreach来操作数组的代码
2011/07/17 PHP
解析如何去掉CodeIgniter URL中的index.php
2013/06/25 PHP
PHP 文件编程综合案例-文件上传的实现
2013/07/03 PHP
从PHP $_SERVER相关参数判断是否支持Rewrite模块
2013/09/26 PHP
thinkphp使用literal防止模板标签被解析的方法
2014/11/22 PHP
PHP CURL 内存泄露问题解决方法
2015/02/12 PHP
详解在PHP的Yii框架中使用行为Behaviors的方法
2016/03/18 PHP
PHP静态方法和静态属性及常量属性的区别与介绍
2019/03/22 PHP
JavaScript Event学习第五章 高级事件注册模型
2010/02/07 Javascript
js使浏览器窗口最大化实现代码(适用于IE)
2013/08/07 Javascript
getAsDataURL在Firefox7.0下无法预览本地图片的解决方法
2013/11/15 Javascript
JavaScript中的全局对象介绍
2015/01/01 Javascript
JavaScript保存并运算页面中数字类型变量的写法
2015/07/06 Javascript
Javascript实现商品秒杀倒计时(时间与服务器时间同步)
2015/09/16 Javascript
JS实现获取图片大小和预览的方法完整实例【兼容IE和其它浏览器】
2017/04/24 Javascript
常见的浏览器Hack技巧整理
2017/06/29 Javascript
使用重写url机制实现验证码换一张功能
2017/08/01 Javascript
详解从Vue.js源码看异步更新DOM策略及nextTick
2017/10/11 Javascript
微信小程序实现的贪吃蛇游戏【附源码下载】
2018/01/03 Javascript
webgl实现物体描边效果的方法介绍
2019/11/27 Javascript
原生JS实现贪吃蛇小游戏
2020/03/09 Javascript
微信小程序之滑动页面隐藏和显示组件功能的实现代码
2020/06/19 Javascript
vue实现点击按钮“查看详情”弹窗展示详情列表操作
2020/09/09 Javascript
[06:38]DOTA2怒掀电竞风暴 2013Chinajoy
2013/07/27 DOTA
python数据结构之图的实现方法
2015/07/08 Python
Kali Linux安装ipython2 和 ipython3的方法
2019/07/11 Python
利用OpenCV中对图像数据进行64F和8U转换的方式
2020/06/03 Python
数据库什么时候应该被重组
2012/11/02 面试题
季度思想汇报
2014/01/01 职场文书
童装店创业计划书
2014/01/09 职场文书
小学校园活动策划
2014/01/30 职场文书
买房协议书
2014/04/11 职场文书
家长通知书家长评语
2014/04/17 职场文书
2016年寒假生活小结
2015/10/10 职场文书
微信小程序和php的登录实现
2021/04/01 PHP
方法汇总:Python 安装第三方库常用
2022/04/26 Python