python处理图片之PIL模块简单使用方法


Posted in Python onMay 11, 2015

本文实例讲述了python处理图片之PIL模块简单使用方法。分享给大家供大家参考。具体实现方法如下:

#!/usr/bin/env python  
#encoding: utf-8 
import Image  
class myimg: 
  def __init__(self, open_file, save_file): 
    self.img = Image.open(open_file) 
    self.save_file = save_file 
  def Change_Size(self, percent=100, height=None, width=None): 
    ''''' 
    percent:以百分比来决定大小 
    height, width:以指定的高、宽来决定大小 
    ''' 
    if not (height and width): 
      width,height = self.img.size   
    new_img = self.img.resize((width*percent/100,height*percent/100),Image.BILINEAR) 
    new_img.save(self.save_file) 
  def Rotation(self, angle): 
    ''''' 
    angle: 旋转的度数 
    ''' 
    rot_img = self.img.rotate(angle) #旋转 
    rot_img.save(self.save_file) 
  def Save_as(self, filename): 
    ''''' 
    filename: 另存为图片格式,直接根据后缀名来 
    ''' 
    self.img.save(filename)  
  def Draw_Something(self): 
    ''''' 
        利用ImageDraw来画图形 
    ''' 
    import ImageDraw 
    draw = ImageDraw.Draw(self.img) 
    width,height = self.img.size 
    draw.line(((0,0),(width-1,height-1)),fill=255) #画直线 
    draw.line(((0,height-1),(width-1,0)),fill=255) 
    draw.arc((0,0,width-1,height-1),0,360,fill=255) #画椭圆 
    self.img.save(self.save_file) 
  def Enhance_Something(self): 
    ''''' 
        利用 ImageEnhance来增强图片效果 
    ''' 
    import ImageEnhance 
    brightness = ImageEnhance.Brightness(self.img) 
    bright_img = brightness.enhance(2.0) ##亮度增强 
    bright_img.save(self.save_file) 
    sharpness = ImageEnhance.Sharpness(self.img) 
    sharp_img = sharpness.enhance(7.0) #锐度增强 
    sharp_img.save(self.save_file) 
    contrast = ImageEnhance.Contrast(self.img) #对比度增强 
    contrast_img = contrast.enhance(2.0)  
    contrast_img.save(self.save_file) 
if __name__ == "__main__": 
  file_name = r"D:\test.png" 
  save_file = r"D:\save.png" 
  saveas_file = r"D:\save_as.bmp" 
  oimg = myimg(file_name, save_file) 
  oimg.Change_Size(30) 
  oimg.Rotation(45) 
  oimg.Save_as(saveas_file) 
  oimg.Draw_Something() 
  oimg.Enhance_Something()

原图:

python处理图片之PIL模块简单使用方法

处理过的画图:(锐化过的)

python处理图片之PIL模块简单使用方法

PS:此外还有另一个比较常用的模块,image模块。

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

Python 相关文章推荐
简化Python的Django框架代码的一些示例
Apr 20 Python
浅谈Python 字符串格式化输出(format/printf)
Jul 21 Python
Python基于matplotlib绘制栈式直方图的方法示例
Aug 09 Python
Python中序列的修改、散列与切片详解
Aug 27 Python
python交互式图形编程实例(三)
Nov 17 Python
python实现文件助手中查看微信撤回消息
Apr 29 Python
python 图像处理画一个正弦函数代码实例
Sep 10 Python
python 实现方阵的对角线遍历示例
Nov 29 Python
使用Python制作新型冠状病毒实时疫情图
Jan 28 Python
Python实现代码块儿折叠
Apr 15 Python
浅谈Tensorflow加载Vgg预训练模型的几个注意事项
May 26 Python
Jupyter Notebook安装及使用方法解析
Nov 12 Python
python自定义解析简单xml格式文件的方法
May 11 #Python
python引用DLL文件的方法
May 11 #Python
深入解析Python中的WSGI接口
May 11 #Python
详细解析Python中__init__()方法的高级应用
May 11 #Python
从Python的源码来解析Python下的freeblock
May 11 #Python
详解Python的Django框架中的templates设置
May 11 #Python
Python素数检测的方法
May 11 #Python
You might like
php session劫持和防范的方法
2013/11/12 PHP
PHP中__FILE__、dirname与basename用法实例分析
2014/12/01 PHP
PHP简单生成缩略图相册的方法
2015/07/29 PHP
yii2-GridView在开发中常用的功能及技巧总结
2017/01/07 PHP
PHP调用Mailgun发送邮件的方法
2017/05/04 PHP
ThinkPHP5&5.1实现验证码的生成、使用及点击刷新功能示例
2020/02/07 PHP
JavaScript如何动态创建table表格
2020/08/02 Javascript
JavaScript脚本库编写的方法
2015/12/09 Javascript
Highcharts入门之基本属性
2016/08/02 Javascript
解决vue 路由变化页面数据不刷新的问题
2018/03/13 Javascript
JavaScript DOM元素常见操作详解【添加、删除、修改等】
2018/05/09 Javascript
javascript读取本地文件和目录方法详解
2020/08/06 Javascript
浅谈vue-props的default写不写有什么区别
2020/08/09 Javascript
python开发之str.format()用法实例分析
2016/02/22 Python
Python 变量类型详解
2018/10/10 Python
Python实现求两个数组交集的方法示例
2019/02/23 Python
通过pycharm使用git的步骤(图文详解)
2019/06/13 Python
在python 中split()使用多符号分割的例子
2019/07/15 Python
Django 对IP访问频率进行限制的例子
2019/08/30 Python
Python-openCV读RGB通道图实例
2020/01/17 Python
python实现提取COCO,VOC数据集中特定的类
2020/03/10 Python
python程序输出无内容的解决方式
2020/04/09 Python
Python数组拼接np.concatenate实现过程
2020/04/18 Python
让Django的BooleanField支持字符串形式的输入方式
2020/05/20 Python
Pycharm Plugins加载失败问题解决方案
2020/11/28 Python
美国网上眼镜供应商:LEOTONY(眼镜、RX太阳镜和太阳镜)
2017/10/31 全球购物
吉列剃须刀英国官网:Gillette英国
2019/03/28 全球购物
英国排名第一的宠物店:PetPlanet
2020/02/02 全球购物
教师求职推荐信范文
2013/11/20 职场文书
《乌塔》教学反思
2014/02/17 职场文书
旅游文化节策划方案
2014/06/06 职场文书
毕业生实习证明
2014/09/19 职场文书
2015年个人自我剖析材料
2014/12/29 职场文书
2015年政府采购工作总结
2015/05/21 职场文书
电工生产实习心得体会
2016/01/22 职场文书
PyQt5爬取12306车票信息程序的实现
2021/05/14 Python