# -*- coding: UTF-8 -*- from PIL import Image import os import sys reload(sys) sys.setdefaultencoding('utf-8') def resize2(im): xsize, ysize = im.size xrate,yrate=xsize/720.0,ysize/1280.0 print xrate,yrate if xrate<1 and yrate<1: return im elif xrate>1 and yrate>1: if xrate<=yrate: xsize,ysize=xsize/xrate,ysize/xrate im = im.resize((int(xsize),int(ysize))) box = (0, 0, 720, 1280) im = im.crop(box) return im else: xsize,ysize=xsize/yrate,ysize/yrate im = im.resize((int(xsize),int(ysize))) box = (0, 0, 720, 1280) im = im.crop(box) return im if __name__=='__main__': oldpath=r'E:logoldjpg' newpath=r'E:log ewpng' files= os.listdir(oldpath) for file in files: im=Image.open(os.path.join(oldpath,file)) print im.format, im.size, im.mode im=resize2(im) im.save(os.path.join(newpath,file))
# 旋转
# out = im.rotate(45)
# 水平和垂直反转
# out = im.transpose(Image.FLIP_LEFT_RIGHT)
# out = im.transpose(Image.FLIP_TOP_BOTTOM)
# out = im.transpose(Image.ROTATE_90)
# out = im.transpose(Image.ROTATE_180)
# out = im.transpose(Image.ROTATE_270)
# 缩略图
# im.thumbnail((x//2, y//2))
# 拆分文件和路径
# os.path.split()
# 拆分文件名和后缀
# os.path.splittext()
# 把图片粘贴到原图的某个区域