用Python图像处理
前几天弄了下django的图片上传,上传之后还需要做些简单的处理,python中PIL模块就是专门用来做这个事情的。
于是照葫芦画瓢做了几个常用图片操作,在这里记录下,以便备用。
这里有个字体文件,大家可以在自己的系统中选取一个,我这打包放在网盘中 下载
一 图样
原始图片
操作一: 缩略图(通常不用这个方式,因为图片质量损坏太大)
操作二 : 旋转图片中的某一部分
操作三: 给图片添加一个图片水印, 2张图层合并
操作四: 给图片添加文字水印,这个用的比较多, 我这里弄了个白色通明低,可以弄成完全透明的
操作 五 等比压缩(比较适合做缩略图)
操作六 按照比例剪裁之后,等比压缩,有时候需要定比例的图片可以这么做
二 代码
- # -*- encoding=utf-8 -*-
- '''''
- author: orangleliu
- 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)
三 参考
用Python图像处理的更多相关文章
- Python图像处理库:Pillow 初级教程
Python图像处理库:Pillow 初级教程 2014-09-14 翻译 http://pillow.readthedocs.org/en/latest/handbook/tutorial.html ...
- Python图像处理之验证码识别
在上一篇博客Python图像处理之图片文字识别(OCR)中我们介绍了在Python中如何利用Tesseract软件来识别图片中的英文与中文,本文将具体介绍如何在Python中利用Tesseract ...
- 【python图像处理】图像的缩放、旋转与翻转
[python图像处理]图像的缩放.旋转与翻转 图像的几何变换,如缩放.旋转和翻转等,在图像处理中扮演着重要的角色,python中的Image类分别提供了这些操作的接口函数,下面进行逐一介绍. 1.图 ...
- Python图像处理库(1)
转自:http://www.ituring.com.cn/tupubarticle/2024 第 1 章 基本的图像操作和处理 本章讲解操作和处理图像的基础知识,将通过大量示例介绍处理图像所需的 Py ...
- Python图像处理库:PIL中Image,ImageDraw等基本模块介绍
Python图像处理库:PIL中Image,ImageDraw等基本模块介绍 标签: 图像处理PILPYTHON 2016-08-19 10:58 461人阅读 评论(0) 收藏 举报 分类: 其他 ...
- Python图像处理库PIL中图像格式转换(一)
在数字图像处理中,针对不同的图像格式有其特定的处理算法. 所以,在做图像处理之前,我们须要考虑清楚自己要基于哪种格式的图像进行算法设计及事实上现.本文基于这个需求.使用python中的图像处理库PIL ...
- python 图像处理中二值化方法归纳总结
python图像处理二值化方法 1. opencv 简单阈值 cv2.threshold 2. opencv 自适应阈值 cv2.adaptiveThreshold 3. Otsu's 二值化 例子: ...
- python图像处理:一福变五福
快过年了,各种互联网产品都出来撒红包.某宝一年一度的“集五福活动”更是成为每年的必备活动之一. 虽然到最后每人大概也就分个两块钱,但作为一个全民话题,大多数人还是愿意凑凑热闹. 毕竟对于如今生活在大城 ...
- Python 图像处理 OpenCV (2):像素处理与 Numpy 操作以及 Matplotlib 显示图像
前文传送门: 「Python 图像处理 OpenCV (1):入门」 普通操作 1. 读取像素 读取像素可以通过行坐标和列坐标来进行访问,灰度图像直接返回灰度值,彩色图像则返回B.G.R三个分量. 需 ...
- Python 图像处理 OpenCV (3):图像属性、图像感兴趣 ROI 区域及通道处理
前文传送门: 「Python 图像处理 OpenCV (1):入门」 「Python 图像处理 OpenCV (2):像素处理与 Numpy 操作以及 Matplotlib 显示图像」 图像属性 图像 ...
随机推荐
- SERVICE_NAME和SERVICE_NAMES和GLOBAL_DBNAME的各自己定义
tnsnames.ora文件中边SERVICE_NAME的參数值--对于动态注冊和静态注冊.该參数有不同的取值 对于动态注冊: The following pfile/spfile parameter ...
- C算法与数据结构-线性表的应用,多项式求和---ShinePans
/*---上机作业作业,二项式加法---*/ /*---By 潘尚 ---*/ /*---日期: 2014-5-8 . ---*/ /*---题目:---*/ //如果有两个稀疏多项式A和B,设计算法 ...
- quick-cocos2d-x游戏开发【9】——单点触摸
quick的触摸机制,我想廖大已经在这篇文章里说的非常清楚了.我们这些小辈们就是在他的基础上完备一下,说说使用方法就能够了.嘿嘿. 在2.2.3之前的版本号(不包含2.2.3).触摸机制和廖大在那篇文 ...
- Android开发时经经常使用的LogUtil
在开发过程中经经常使用到Log.我们常写的一种方式就是自己定义一个LogUtil工具类 private static boolean LOGV = true; private static boole ...
- Uva1335 二分+贪心
/* 奇数怎么搞呢 二分到答案怎么judge呢 贪心怎么贪呢 假设贪心方案是 前两个挨着取 后面的能靠前就靠前 这样子似乎保证了ans最min 但是不管贪的对不对 操作起来时间GG 而且 如果真的这样 ...
- oracle表类似:BIN$dJ5h8mA4Lr/gQAB/AQB0oA==$0 TABLE
今天看到数据库中有很多类似: TNAME TABTYPE CLUSTERID ------------------------------ ---- ...
- 杂项:DS(目录服务)
ylbtech-杂项:DS(目录服务) 1.返回顶部 1. DS(目录服务). 目录服务管理概述: 目录服务是扩展计算机系统中最重要的组件之一.虽然用户和管理通常不知道他们感兴趣对象的确切名称,但他们 ...
- 不用任何插件,实现一个tab栏切换
//使用jquery中获取当前索引的方法.显示隐藏 <script> $(".tab_list li").on('click', function () { $(thi ...
- Excel 查找某列中的数据在另一列是否存在并输出其他列的数据
最近在操作Excel文件数据导入数据库时,经常需要检查Excel中哪些数据数据库中已经存在,哪些不存在,然后再将不存在数据库中的Excel数据导入:在此过程中,经常需要操作Excel中的数据,所以.也 ...
- MySQL架构与SQL执行流程
MySQL架构设计 下面是一张MySQL的架构图: 上方各个组件的含义如下: Connectors 指的是不同语言中与SQL的交互 Management Serveices & Utiliti ...