python处理图片之PIL模块简单使用方法
作者:像风一样的自由 时间:2023-06-25 21:42:05
本文实例讲述了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()
原图:
处理过的画图:(锐化过的)
PS:此外还有另一个比较常用的模块,image模块。
希望本文所述对大家的Python程序设计有所帮助。
标签:python,图片,PIL
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
MySQL错误提示:sql_mode=only_full_group_by完美解决方案
2024-01-17 02:50:18
![](https://img.aspxhome.com/file/2023/7/122887_0s.png)
浅谈一下mysql数据库底层原理
2024-01-27 21:19:39
MySQL中索引的优化的示例详解
2024-01-26 21:31:25
![](https://img.aspxhome.com/file/2023/4/108884_0s.png)
错误 2812: 未能找到存储过程 'master.dbo.xp_fileexist'
2010-07-22 19:50:00
hadoop迁移数据应用实例详解
2022-12-28 01:29:45
![](https://img.aspxhome.com/file/2023/3/112083_0s.jpg)
MYSQL子查询和嵌套查询优化实例解析
2024-01-23 13:24:34
python并发编程多进程 模拟抢票实现过程
2022-03-07 11:53:05
mysql 5.7.13 安装配置方法图文教程(win10 64位)
2024-01-28 09:07:21
python 以16进制打印输出的方法
2023-10-23 07:33:17
Highcharts+NodeJS搭建数据可视化平台示例
2024-05-02 17:38:38
一篇文章读懂Golang init函数执行顺序
2024-04-27 15:40:37
![](https://img.aspxhome.com/file/2023/7/133957_0s.jpg)
在Spring中用select last_insert_id()时遇到问题
2009-05-24 19:50:00
PyTorch中Torch.arange函数详解
2022-04-29 02:30:50
![](https://img.aspxhome.com/file/2023/4/134424_0s.png)
Python自动化部署工具Fabric的简单上手指南
2022-11-23 03:23:25
Python基础语法之容器详解
2022-01-07 23:20:19
![](https://img.aspxhome.com/file/2023/4/121704_0s.jpg)
Python中re模块常用方法总结分析
2022-10-27 14:40:20
解决Python中pandas读取*.csv文件出现编码问题
2023-10-15 11:48:42
![](https://img.aspxhome.com/file/2023/0/94180_0s.png)
解决pycharm上的jupyter notebook端口被占用问题
2022-10-28 15:43:13
![](https://img.aspxhome.com/file/2023/6/115426_0s.jpg)
可用于监控 mysql Master Slave 状态的python代码
2023-01-05 07:39:30
Python爬虫之Selenium实现关闭浏览器
2022-12-09 07:11:03