杂项之图像处理pillow

本节内容

  1. 参考文献
  2. 生成验证码源码
  3. 一些小例子

1. 参考文献

http://pillow-cn.readthedocs.io/zh_CN/latest/ pillow中文文档
http://pillow.readthedocs.io/en/3.4.x/ pillow官方文档
http://blog.csdn.net/orangleliu/article/details/43529319 一些小例子
http://python.jobbole.com/83685/ pillow使用方法集合

2. 生成验证码源码

#!/usr/bin/env python
# encoding:utf-8
# __author__: check_code
# date: 2016/12/22 9:43
# blog: http://huxianglin.cnblogs.com/ http://xianglinhu.blog.51cto.com/ import random
from PIL import Image, ImageDraw, ImageFont, ImageFilter _letter_cases = "abcdefghjkmnpqrstuvwxy" # 小写字母,去除可能干扰的i,l,o,z
_upper_cases = _letter_cases.upper() # 大写字母
_numbers = ''.join(map(str, range(3, 10))) # 数字,将数字转换成字符串,并把0,1,2去除掉,防止干扰
init_chars = ''.join((_letter_cases, _upper_cases, _numbers)) # 将上面生成的内容拼接到一起 def create_validate_code(size=(120, 30),
chars=init_chars,
img_type="GIF",
mode="RGB",
bg_color=(255, 255, 255),
fg_color=(0, 0, 255),
font_size=18,
font_type="simkai.ttf",
length=4,
draw_lines=True,
n_line=(1, 2),
draw_points=True,
point_chance = 2):
'''
@todo: 生成验证码图片
@param size: 图片的大小,格式(宽,高),默认为(120, 30)
@param chars: 允许的字符集合,格式字符串
@param img_type: 图片保存的格式,默认为GIF,可选的为GIF,JPEG,TIFF,PNG
@param mode: 图片模式,默认为RGB
@param bg_color: 背景颜色,默认为白色
@param fg_color: 前景色,验证码字符颜色,默认为蓝色#0000FF
@param font_size: 验证码字体大小
@param font_type: 验证码字体,默认为 ae_AlArabiya.ttf
@param length: 验证码字符个数
@param draw_lines: 是否划干扰线
@param n_lines: 干扰线的条数范围,格式元组,默认为(1, 2),只有draw_lines为True时有效
@param draw_points: 是否画干扰点
@param point_chance: 干扰点出现的概率,大小范围[0, 100]
@return: [0]: PIL Image实例
@return: [1]: 验证码图片中的字符串
''' width, height = size # 宽, 高
img = Image.new(mode, size, bg_color) # 创建图形
draw = ImageDraw.Draw(img) # 创建画笔,参数中传递了img对象 def get_chars():
'''生成给定长度的字符串,返回列表格式'''
return random.sample(chars, length) def create_lines():
'''绘制干扰线'''
line_num = random.randint(*n_line) # 干扰线条数 for i in range(line_num):
# 起始点
begin = (random.randint(0, size[0]), random.randint(0, size[1]))
#结束点
end = (random.randint(0, size[0]), random.randint(0, size[1]))
draw.line([begin, end], fill=(0, 0, 0)) # 在起始点和结束点之间画一条线,颜色是黑色 def create_points():
'''绘制干扰点'''
chance = min(100, max(0, int(point_chance))) # 大小限制在[0, 100] # 设置干扰点在所有点中所占比例 for w in range(width):
for h in range(height):
tmp = random.randint(0, 100)
if tmp > 100 - chance:
draw.point((w, h), fill=(0, 0, 0)) # 满足条件的点就给打成黑色 def create_strs():
'''绘制验证码字符'''
c_chars = get_chars() # 获取生成的验证码字符串
strs = ' %s ' % ' '.join(c_chars) # 每个字符前后以空格隔开 font = ImageFont.truetype(font_type, font_size) # 生成字体对象,包含字体类型和字体大小
font_width, font_height = font.getsize(strs) # 计算生成的文字的宽度和高度 draw.text(((width - font_width) / 3, (height - font_height) / 3), # 设置验证码的起始点
strs, font=font, fill=fg_color) return ''.join(c_chars) # 返回验证码字符串 if draw_lines: # 如果为true则设置干扰线
create_lines()
if draw_points: # 如果为true则设置干扰点
create_points()
strs = create_strs() # 设置字符串 # 图形扭曲参数
params = [1 - float(random.randint(1, 2)) / 100,
0,
0,
0,
1 - float(random.randint(1, 10)) / 100,
float(random.randint(1, 2)) / 500,
0.001,
float(random.randint(1, 2)) / 500
]
img = img.transform(size, Image.PERSPECTIVE, params) # 创建扭曲
img = img.filter(ImageFilter.EDGE_ENHANCE_MORE) # 滤镜,边界加强(阈值更大)
return img, strs #-----------------------------views.py------------------------------------------------
from io import BytesIO
def test(req):
mstream = BytesIO() # 在python3里面引入的是BytesIO,python2中引入StringIO
validate_code = check_code.create_validate_code() # 拿到两个返回值,第一个是图片对象,第二个是生成的随机字符串
img = validate_code[0]
img.save(mstream, "GIF") # 将拿到的图片对象以 GIF 格式保存到内存中
return HttpResponse(mstream.getvalue()) # 将内存中保存的图片返回给客户端
'''
img = img.transform(size, Image.PERSPECTIVE, params) # 创建扭曲 Image.transform(size, method, data=None, resample=0, fill=1) 参数
Transforms this image. This method creates a new image with the given size, and the same mode as the original, and copies data to the new image using the given transform. Parameters:
size – The output size. 输出图片大小
method – The transformation method. This is one of
PIL.Image.EXTENT (cut out a rectangular subregion), 范围
PIL.Image.AFFINE (affine transform), 轮廓
PIL.Image.PERSPECTIVE (perspective transform), 透视
PIL.Image.QUAD (map a quadrilateral to a rectangle), 四方
PIL.Image.MESH (map a number of source quadrilaterals in one operation). 网格
data – Extra data to the transformation method.
resample – Optional resampling filter. It can be one of PIL.Image.NEAREST (use nearest neighbour), PIL.Image.BILINEAR (linear interpolation in a 2x2 environment), or PIL.Image.BICUBIC (cubic spline interpolation in a 4x4 environment). If omitted, or if the image has mode “1” or “P”, it is set to PIL.Image.NEAREST.
Returns: 返回一个新的image对象
An Image object.
''' '''
img = img.filter(ImageFilter.EDGE_ENHANCE_MORE) # 滤镜,边界加强(阈值更大) 图像滤波在ImageFilter 模块中,在该模块中,预先定义了很多增强滤波器,可以通过filter( )函数使用,预定义滤波器包括:
BLUR、均值滤波
CONTOUR、找轮廓
DETAIL、详细
EDGE_ENHANCE、边缘加强
EDGE_ENHANCE_MORE、
EMBOSS、突出
FIND_EDGES、边缘检测
SMOOTH、平滑
SMOOTH_MORE、
SHARPEN、锐化滤镜
使用该模块时,需先导入
'''

注意:画笔能提供的功能封装路径:C:\Python35\Lib\site-packages\PIL\ImageDraw.py中的类ImageDraw中

一些小例子

# -*- encoding=utf-8 -*-
'''
pil处理图片,验证,处理
大小,格式 过滤
压缩,截图,转换 图片库最好用Pillow
还有一个测试图片test.jpg, 一个log图片,一个字体文件
''' #图片的基本参数获取
try:
from PIL import Image, ImageDraw, ImageFont, ImageEnhance
except ImportError:
import Image, ImageDraw, ImageFont, ImageEnhance def compress_image(img, w=128, h=128):
'''''
缩略图
'''
img.thumbnail((w,h))
im.save('test1.png', 'PNG')
print u'成功保存为png格式, 压缩为128*128格式图片' def cut_image(img):
'''''
截图, 旋转,再粘贴
'''
#eft, upper, right, lower
#x y z w x,y 是起点, z,w是偏移值
width, height = img.size
box = (width-200, height-100, width, height)
region = img.crop(box)
#旋转角度
region = region.transpose(Image.ROTATE_180)
img.paste(region, box)
img.save('test2.jpg', 'JPEG')
print u'重新拼图成功' def logo_watermark(img, logo_path):
'''''
添加一个图片水印,原理就是合并图层,用png比较好
'''
baseim = img
logoim = Image.open(logo_path)
bw, bh = baseim.size
lw, lh = logoim.size
baseim.paste(logoim, (bw-lw, bh-lh))
baseim.save('test3.jpg', 'JPEG')
print u'logo水印组合成功' def text_watermark(img, text, out_file="test4.jpg", angle=23, opacity=0.50):
'''''
添加一个文字水印,做成透明水印的模样,应该是png图层合并
http://www.pythoncentral.io/watermark-images-python-2x/
这里会产生著名的 ImportError("The _imagingft C module is not installed") 错误
Pillow通过安装来解决 pip install Pillow
'''
watermark = Image.new('RGBA', img.size, (255,255,255)) #我这里有一层白色的膜,去掉(255,255,255) 这个参数就好了 FONT = "msyh.ttf"
size = 2 n_font = ImageFont.truetype(FONT, size) #得到字体
n_width, n_height = n_font.getsize(text)
text_box = min(watermark.size[0], watermark.size[1])
while (n_width+n_height < text_box):
size += 2
n_font = ImageFont.truetype(FONT, size=size)
n_width, n_height = n_font.getsize(text) #文字逐渐放大,但是要小于图片的宽高最小值 text_width = (watermark.size[0] - n_width) / 2
text_height = (watermark.size[1] - n_height) / 2
#watermark = watermark.resize((text_width,text_height), Image.ANTIALIAS)
draw = ImageDraw.Draw(watermark, 'RGBA') #在水印层加画笔
draw.text((text_width,text_height),
text, font=n_font, fill="#21ACDA")
watermark = watermark.rotate(angle, Image.BICUBIC)
alpha = watermark.split()[3]
alpha = ImageEnhance.Brightness(alpha).enhance(opacity)
watermark.putalpha(alpha)
Image.composite(watermark, img, watermark).save(out_file, 'JPEG')
print u"文字水印成功" #等比例压缩图片
def resizeImg(img, dst_w=0, dst_h=0, qua=85):
'''''
只给了宽或者高,或者两个都给了,然后取比例合适的
如果图片比给要压缩的尺寸都要小,就不压缩了
'''
ori_w, ori_h = im.size
widthRatio = heightRatio = None
ratio = 1 if (ori_w and ori_w > dst_w) or (ori_h and ori_h > dst_h):
if dst_w and ori_w > dst_w:
widthRatio = float(dst_w) / ori_w #正确获取小数的方式
if dst_h and ori_h > dst_h:
heightRatio = float(dst_h) / ori_h if widthRatio and heightRatio:
if widthRatio < heightRatio:
ratio = widthRatio
else:
ratio = heightRatio if widthRatio and not heightRatio:
ratio = widthRatio if heightRatio and not widthRatio:
ratio = heightRatio newWidth = int(ori_w * ratio)
newHeight = int(ori_h * ratio)
else:
newWidth = ori_w
newHeight = ori_h im.resize((newWidth,newHeight),Image.ANTIALIAS).save("test5.jpg", "JPEG", quality=qua)
print u'等比压缩完成' '''''
Image.ANTIALIAS还有如下值:
NEAREST: use nearest neighbour
BILINEAR: linear interpolation in a 2x2 environment
BICUBIC:cubic spline interpolation in a 4x4 environment
ANTIALIAS:best down-sizing filter
''' #裁剪压缩图片
def clipResizeImg(im, dst_w, dst_h, qua=95):
'''''
先按照一个比例对图片剪裁,然后在压缩到指定尺寸
一个图片 16:5 ,压缩为 2:1 并且宽为200,就要先把图片裁剪成 10:5,然后在等比压缩
'''
ori_w,ori_h = im.size dst_scale = float(dst_w) / dst_h #目标高宽比
ori_scale = float(ori_w) / ori_h #原高宽比 if ori_scale <= dst_scale:
#过高
width = ori_w
height = int(width/dst_scale) x = 0
y = (ori_h - height) / 2 else:
#过宽
height = ori_h
width = int(height*dst_scale) x = (ori_w - width) / 2
y = 0 #裁剪
box = (x,y,width+x,height+y)
#这里的参数可以这么认为:从某图的(x,y)坐标开始截,截到(width+x,height+y)坐标
#所包围的图像,crop方法与php中的imagecopy方法大为不一样
newIm = im.crop(box)
im = None #压缩
ratio = float(dst_w) / width
newWidth = int(width * ratio)
newHeight = int(height * ratio)
newIm.resize((newWidth,newHeight),Image.ANTIALIAS).save("test6.jpg", "JPEG",quality=95)
print "old size %s %s"%(ori_w, ori_h)
print "new size %s %s"%(newWidth, newHeight)
print u"剪裁后等比压缩完成" if __name__ == "__main__":
'''''
主要是实现功能, 代码没怎么整理
'''
im = Image.open('test.jpg') #image 对象
compress_image(im) im = Image.open('test.jpg') #image 对象
cut_image(im) im = Image.open('test.jpg') #image 对象
logo_watermark(im, 'logo.png') im = Image.open('test.jpg') #image 对象
text_watermark(im, 'Orangleliu') im = Image.open('test.jpg') #image 对象
resizeImg(im, dst_w=100, qua=85) im = Image.open('test.jpg') #image 对象 # 这个先切割再生成缩略图的功能之后会在生成头像时用到
clipResizeImg(im, 100, 200)

杂项之图像处理pillow的更多相关文章

  1. 第二篇:杂项之图像处理pillow

    杂项之图像处理pillow   杂项之图像处理pillow 本节内容 参考文献 生成验证码源码 一些小例子 1. 参考文献 http://pillow-cn.readthedocs.io/zh_CN/ ...

  2. 图像处理pillow模块

    pillow模块: -->基本的图像处理模块 Pip install pillow from PIL import Image #1.读取图片 im = Image.open('/test.jp ...

  3. 图像处理PILLOW的使用

    1.安装 pip install Pillow 2.使用 1)图片缩放 from PIL import Imageim = Image.open('dog.jpg') w,h = im.size #获 ...

  4. 潭州课堂25班:Ph201805201 爬虫基础 第九课 图像处理- PIL (课堂笔记)

    Python图像处理-Pillow 简介 Python传统的图像处理库PIL(Python Imaging Library ),可以说基本上是Python处理图像的标准库,功能强大,使用简单. 但是由 ...

  5. python爬虫基础15-python图像处理,PIL库

    Python图像处理-Pillow 简介 Python传统的图像处理库PIL(Python Imaging Library ),可以说基本上是Python处理图像的标准库,功能强大,使用简单. 但是由 ...

  6. 6 python高级数据处理和可视化

    6.2. pyplot作图 1.折线图和散点图 t = np.arange(0,4,0.1) plt.plot(t,t,'o',t,t+2,t,t**2,'o') plt.show() 2.柱线图 p ...

  7. python武器库

    1,开发库You-GetYouTube/Youku/Niconico视频下载器Zipline一个Pythonic的交易算法库docopt为Python程序创造一个优雅的命令行界面PDFMinerPyt ...

  8. Python图像处理库:Pillow 初级教程

    Python图像处理库:Pillow 初级教程 2014-09-14 翻译 http://pillow.readthedocs.org/en/latest/handbook/tutorial.html ...

  9. 图像识别的前期工作——使用pillow进行图像处理

    pillow是个很好用的python图像处理库,可以到官方网站下载最新的文件.如果官网的任何PIL版本都不能与自己的python版本对应,或安装成功后发现运行出错,可以尝试从一个非官方的whl网站下载 ...

随机推荐

  1. Angular学习-指令入门

    1.指令的定义 从用户的角度来看,指令就是在应用的模板中使用的自定义HTML标签.指令可以很简单,也可以很复杂.AngularJS的HTML编译器会解析指令,增强模板的功能.也是组件化未来的发展趋势, ...

  2. Oracle简单常用的数据泵导出导入(expdp/impdp)命令举例(下)

    <Oracle简单常用的数据泵导出导入(expdp/impdp)命令举例(上)> <Oracle简单常用的数据泵导出导入(expdp/impdp)命令举例(下)> 目的:指导项 ...

  3. TFS工作项数据统计及相关数据库结构分析

    今天为客户的质量管理部门人员提供TFS咨询过程中,客户的质量管理专家基于TFS提出了一个比较棘手的数据统计需求.需求是这样,客户的数十个软件项目通过质量管理部按照年度版本计划进行软件产品系统的发布,因 ...

  4. 【原创】如何确定Kafka的分区数、key和consumer线程数

    在Kafak中国社区的qq群中,这个问题被提及的比例是相当高的,这也是Kafka用户最常碰到的问题之一.本文结合Kafka源码试图对该问题相关的因素进行探讨.希望对大家有所帮助.   怎么确定分区数? ...

  5. HTML5学习

    HTML5动画效果 http://www.html5tricks.com/30-more-html5-apps.html http://www.html5tricks.com/category/htm ...

  6. C# 本质论 第三章 操作符和控制流

    操作符通常分为3大类:一元操作符(正.负).二元操作符(加.减.乘.除.取余)和三元操作符( condition?consequence:alternative(consequence和alterna ...

  7. 怎样在Redis通过StackExchange.Redis 存储集合类型List

    StackExchange 是由StackOverFlow出品, 是对Redis的.NET封装,被越来越多的.NET开发者使用在项目中. 绝大部分原先使用ServiceStack的开发者逐渐都转了过来 ...

  8. jquery禁用文本框

    禁用文本框 //文本框禁用 $("input[type='text']").each(function () { $("#" + this.id).attr(& ...

  9. 【转】MVC、MVP与MVT

    MVC是Model-View-Control的缩写,Model指的是数据层,View指的是UI层,Control指的是控制层,这三层之间彼此联系.View层的用户行为,触发Control层,Contr ...

  10. j2ee log4j集中式日志解决方案logpool-v0.2

    下一个小版本会进行清理. war包下载地址 http://pan.baidu.com/s/1nvGmORn