#!/usr/bin/env python
# -*- coding: utf-8 -*-
from PIL import Image
f = r'1.jpg'
def resize(fn, width=None, height=None):
print fn
im = Image.open(fn)
ori_width = im.width
ori_height = im.height
if not width and not height:
print 'parameters incorrect'
return
elif not width:
width = 1.0*ori_width/ori_height*height
elif not height:
height = 1.0*ori_height/ori_width*width
resize_im = im.resize((width, height), Image.BILINEAR) ##重新调整大小
resize_im.save('thumb_'+fn)
resize(f, 128, 128)
resize(f, None, 128)
resize(f, 128, None)
def rotate(f, num)
im = Image.open(f)
rot_img = im.rotate(num) ##逆时针旋转一个角度
rot_img.save('rotate_'+f)
rotate(f, 45)
def transpose(f):
im = Image.open(f)
im.transpose(Image.FLIP_LEFT_RIGHT) ##左右镜像
im.transpose(Image.FLIP_TOP_BOTTOM) ##上下镜像
im.transpose(Image.ROTATE_90)<span style="white-space:pre"> </span> ##逆时针旋转90度
im.transpose(Image.ROTATE_180)
im.transpose(Image.ROTATE_270)
tm = im.transpose(Image.TRANSPOSE) ##左右镜像后逆时针90度
tm.save('transpose_'+f)
transpose(f)