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

本文实例讲述了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程序设计有所帮助。

Posted in 未分类