背景

最近在看到了Python 练习册,每天一个小程序 这个项目,非常有趣,也比较实用.

晚上看了这第000题,关于Python图片处理:

将你的 QQ 头像(或者微博头像)右上角加上红色的数字,类似于微信未读信息数量那种提示效果。 类似于图中效果

之前没怎么使用过PIL库,在生成验证码及识别验证码时也需要了解这方面知识,就动手试了实践下.


PIL

The Python Imaging Library adds image processing capabilities to your Python interpreter. 这个库提供了广泛的文件格式支持、高效的内部表示以及相当强大的图像处理功能。

文档在这:http://omz-software.com/pythonista/docs/ios/PIL.html

思路

题目的意思实际就是为图片加水印,具体可分以下2步:

  1. 将文本"转"成图片
  2. 将生成的水印图片跟原图相"叠加"

原理差不多就是这样子,具体处理还得使用PIL.


最后贴上代码:

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Date : 2014-11-29 19:09:59
# @Author : Linsir (vi5i0n@hotmail.com)
# @Link : http://Linsir.sinaapp.com import Image, ImageEnhance, ImageDraw, ImageFont def text2img(text, font_color="Blue", font_size=25):
"""生成内容为 TEXT 的水印""" font = ImageFont.truetype('simsun.ttc', font_size)
#多行文字处理
text = text.split('\n')
mark_width = 0
for i in range(len(text)):
(width, height) = font.getsize(text[i])
if mark_width < width:
mark_width = width
mark_height = height * len(text) #生成水印图片
mark = Image.new('RGBA', (mark_width,mark_height))
draw = ImageDraw.ImageDraw(mark, "RGBA")
draw.setfont(font)
for i in range(len(text)):
(width, height) = font.getsize(text[i])
draw.text((0, i*height), text[i], fill=font_color)
return mark def set_opacity(im, 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):
"""添加水印""" try:
if opacity < 1:
mark = set_opacity(mark, opacity)
if im.mode != 'RGBA':
im = im.convert('RGBA')
if im.size[0] < mark.size[0] or im.size[1] < mark.size[1]:
print "The mark image size is larger size than original image file."
return False #设置水印位置
if position == 'left_top':
x = 0
y = 0
elif position == 'left_bottom':
x = 0
y = im.size[1] - mark.size[1]
elif position == 'right_top':
x = im.size[0] - mark.size[0]
y = 0
elif position == 'right_bottom':
x = im.size[0] - mark.size[0]
y = im.size[1] - mark.size[1]
else:
x = (im.size[0] - mark.size[0]) / 2
y = (im.size[1] - mark.size[1]) / 2 layer =Image.new('RGBA', im.size,)
layer.paste(mark,(x,y))returnImage.composite(layer, im, layer)exceptExceptionas e:print">>>>>>>>>>> WaterMark EXCEPTION: "+ str(e)returnFalsedef main():
text = u'Linsir.水印.\nvi5i0n@hotmail.com'# text = open('README.md').read().decode('utf-8')# print text
im =Image.open('origal.png')
mark = text2img(text)
image = watermark(im, mark,'center',0.9)if image:
image.save('watermark.png')
image.show()else:print"Sorry, Failed."if __name__ =='__main__':
main()

其实会了这些代码,也就可以做些像: python生成图片验证码, 10 行代码判定色情图片,PYTHON生成图片,生成长微博之类的事了.

enjoy it .

Python练习册--PIL处理图片之加水印的更多相关文章

  1. PHPThumb处理图片,生成缩略图,图片尺寸调整,图片截取,图片加水印,图片旋转

    [强烈推荐]下载地址(github.com/masterexploder/PHPThumb). 注意这个类库有一个重名的叫phpThumb,只是大小写的差别,所以查找文档的时候千万注意. 在网站建设过 ...

  2. Python实用案例,Python脚本,Python实现批量加水印

    往期回顾 Python实现自动监测Github项目并打开网页 Python实现文件自动归类 Python实现帮你选择双色球号码 Python实现每日更换"必应图片"为"桌 ...

  3. Python实现给图片加水印功能

    前言 最近忙得连轴转,很久没更新博客了,代码倒是没啥写,积累了好些东西,接下来一有时间就来更新吧~ 本文记录使用Python实现给图片添加水印的功能实现过程 先看效果 把公众号的封面作为素材 原图是这 ...

  4. 火车头dede采集接口,图片加水印,远程图片本地化,远程无后缀的无图片本地化

    <?php /* [LocoySpider] (C)2005-2010 Lewell Inc. 火车采集器 DedeCMS 5.7 UTF8 文章发布接口 Update content: 图片加 ...

  5. Python用Pillow(PIL)进行简单的图像操作

    Python用Pillow(PIL)进行简单的图像操作 颜色与RGBA值 计算机通常将图像表示为RGB值,或者再加上alpha值(通透度,透明度),称为RGBA值.在Pillow中,RGBA的值表示为 ...

  6. Python中的PIL

    转自:http://blog.csdn.net/yockie/article/details/8498301 介绍 把Python的基础知识学习后,尝试一下如何安装.加载.使用非标准库,选择了图像处理 ...

  7. ASP组件AspJpeg(加水印)生成缩略图等使用方法

    ASP组件AspJpeg(加水印)生成缩略图等使用方法 作者: 字体:[增加 减小] 类型:转载 时间:2012-12-17我要评论 ASPJPEG是一款功能相当强大的图象处理组件,用它可以轻松地做出 ...

  8. 20190728-Python爬取视频&切割视频&视频加水印

    1.视频爬取 1.下载视频的源码如下: import os import requests from bs4 import BeautifulSoup import threading from bj ...

  9. thinkphp 3.2.3整合ueditor 1.4,给上传的图片加水印

    今天分享一下thinkphp 3.2.3整合ueditor 1.4,给上传的图片加水印.博主是新手,在这里卡住了很久(>_<) thinkphp 3.2.3整合ueditor 1.4 下载 ...

随机推荐

  1. Label & TextBlock

    I always thought it was odd that WPF has both TextBlock and Label.  They both are responsible for di ...

  2. Linux下安装Android Studio (Centos 7)

    首先去下载一个android studio的包. http://www.android-studio.org/ (友情一个) http://www.cnblogs.com/gssl/p/4963668 ...

  3. 转载:rebar和erlang

    使用rebar生成erlang release 并进行热代码升级 http://blog.sina.com.cn/s/blog_6530ad590100wmkn.html 使用rebar工具开发erl ...

  4. BOM(制造数据管理)

    --工艺路线 DECLARE -- API input variables l_operation_tbl bom_rtg_pub.operation_tbl_type := bom_rtg_pub. ...

  5. FTP远程命令集

    使用ftp命令进行远程文件传输 ftp命令是标准的文件传输协议的用户接口.ftp是在TCP/IP网络上的计算机之间传输文件的简单有效的方法.它允许用户传输ASCII文件和二进制文件. 在ftp会话过程 ...

  6. [转]Swift Cheat Sheet

    原文:http://kpbp.github.io/swiftcheatsheet/ A quick cheat sheet and reference guide for Apple's Swift ...

  7. 解决从内部存储设备安装apk提示Permission Denied

    做应用商店,下载apk,考虑一种情况,如果没有sd卡的情况下就将apk下载到 Internal Cache目录下. 下载都正常,但是在安装的时候提示Permission Denied /data/da ...

  8. Entity Framework 6 Code First +MVC5+MySql/Oracle使用过程中的几个问题

    1. namespace Snapsia.Web.Models { using System; using System.Data.Entity; using System.ComponentMode ...

  9. Codeforces Gym 100531D Digits 暴力

    Problem D. Digits 题目连接: http://codeforces.com/gym/100531/attachments Description Little Petya likes ...

  10. linux下的文件操作——批量重命名

    概述:在日常工作中,我们经常需要对一批文件进行重命名操作,例如将所有的jpg文件改成bnp,将名字中的1改成one,等等.文本主要为你讲解如何实现这些操作 1.删除所有的 .bak 后缀: renam ...