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 相关文章推荐
Flask入门教程实例:搭建一个静态博客
Mar 27 Python
python基础入门学习笔记(Python环境搭建)
Jan 13 Python
python中函数总结之装饰器闭包详解
Jun 12 Python
谈谈如何手动释放Python的内存
Dec 17 Python
Python实现Youku视频批量下载功能
Mar 14 Python
Python编程给numpy矩阵添加一列方法示例
Dec 04 Python
Python简单I/O操作示例
Mar 18 Python
Python单元和文档测试实例详解
Apr 11 Python
介绍一款python类型检查工具pyright(推荐)
Jul 03 Python
详解python中*号的用法
Oct 21 Python
关于TensorFlow新旧版本函数接口变化详解
Feb 10 Python
Python办公自动化之教你如何用Python将任意文件转为PDF格式
Jun 28 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
phpexcel导出excel的颜色和网页中的颜色显示不一致
2012/12/11 PHP
使用PHP把HTML生成PDF文件的几个开源项目介绍
2014/11/17 PHP
Yii Framework框架使用PHPExcel组件的方法示例
2019/07/24 PHP
对YUI扩展的Gird组件 Part-2
2007/03/10 Javascript
javascript 冒泡排序 正序和倒序实现代码
2010/12/14 Javascript
用javascript作一个通用向导说明
2011/08/30 Javascript
js加入收藏以及使用Jquery更改透明度
2014/01/26 Javascript
jquery如何判断表格同一列不同行input数据是否重复
2014/05/14 Javascript
js实现Select下拉框具有输入功能的方法
2015/02/06 Javascript
JQuery显示隐藏DIV的方法及代码实例
2015/04/16 Javascript
JS实现网页右侧带动画效果的伸缩窗口代码
2015/10/29 Javascript
归纳下js面向对象的几种常见写法总结
2016/08/24 Javascript
利用Javascript裁剪图片并存储的简单实现
2017/03/13 Javascript
JS实现的添加弹出层并完成锁屏操作示例
2017/04/07 Javascript
Bootstrap响应式导航由768px变成992px的实现代码
2017/06/15 Javascript
JS验证码实现代码
2017/09/14 Javascript
JavaScript的setter与getter方法
2017/11/29 Javascript
vue-cli 引入、配置axios的方法
2018/05/08 Javascript
微信小程序实现运动步数排行功能(可删除)
2018/07/05 Javascript
webpack-url-loader 解决项目中图片打包路径问题
2019/02/15 Javascript
JS实现图片轮播效果实例详解【可自动和手动】
2019/04/04 Javascript
ionic2.0双击返回键退出应用
2019/09/17 Javascript
python删除列表内容
2015/08/04 Python
Python之csv文件从MySQL数据库导入导出的方法
2018/06/21 Python
详解PANDAS 数据合并与重塑(join/merge篇)
2019/07/09 Python
django项目简单调取百度翻译接口的方法
2019/08/06 Python
nginx黑名单和django限速,最简单的防恶意请求方法分享
2019/08/09 Python
Python数据可视化:饼状图的实例讲解
2019/12/07 Python
python如何写个俄罗斯方块
2020/11/06 Python
Java中实现多态的机制是什么?
2014/12/07 面试题
应届生求职信写作技巧
2013/10/24 职场文书
硕士生工作推荐信
2014/03/07 职场文书
我为党旗添光彩演讲稿
2014/09/13 职场文书
出纳试用期自我评价
2015/03/10 职场文书
储备店长岗位职责
2015/04/14 职场文书
从QQtabBar看css命名规范BEM的详细介绍
2021/08/07 HTML / CSS