本文实例讲述了python处理图片之PIL模块简单使用方法。分享给大家供大家参考。具体实现方法如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
|
#!/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程序设计有所帮助。