您的位置:首页 > 编程语言 > Python开发

python Image模块学习

2016-05-16 22:31 513 查看
__author__ = 'fuquanjun'
# -*- coding: utf-8 -*-

from PIL import Image
import ImageFont
import ImageDraw
import ImageFilter
import sys
import os

class UImage(object):
def __init__(self, path):
self.im = Image.open(imagePath)
(self.width, self.height) = self.im.size

def getSize(self):
return self.width, self.height

def getHalfReduceImage(self,  path, encoding= 'jpeg'):
self.im.thumbnail((self.width//2, self.height//2))
self.im.save(path, encoding)

def getReduceImageBySize(self, w, h, path, encoding= 'jpeg'):
self.im.thumbnail((w, h))
self.im.save(path, encoding)

def getReduceImageByRatio(self, w, h, path, encoding='jpeg'):
self.im.thumbnail((self.width//w, self.height//h))
self.im.save(path,encoding)

def getFuzzyImage(self,path):
self.im = self.im.filter(ImageFilter.BLUR)
self.im.save(path)

def
if __name__ == "__main__":
#test get size

imagePath = os.path.join(os.path.dirname(os.path.realpath(sys.argv[0])), 'images')
imagePath = os.path.join(imagePath, "1.jpg")
uImage = UImage(imagePath)

uImage.getHalfReduceImage(os.path.join(os.path.dirname(os.path.realpath(sys.argv[0])), 'images')+'2.jpg')
uImage.getReduceImageBySize(100, 100, os.path.join(os.path.dirname(os.path.realpath(sys.argv[0])), 'images')+'3.jpg')
uImage.getReduceImageByRatio(0.1, 0.1, os.path.join(os.path.dirname(os.path.realpath(sys.argv[0])), 'images')+'4.jpg')
uImage.getFuzzyImage(os.path.join(os.path.dirname(os.path.realpath(sys.argv[0])), 'images')+'5.jpg')
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: