# -*- coding: utf-8 -*-
"""
Created on Fri Nov 17 06:51:30 2017
你有一个目录，装了很多照片，把它们的尺寸变成都不大于 iPhone5 分辨率的大小
1136*640 
@author: suidx
"""
#coding=utf-8
import os  #打开文件时需要
from PIL import Image
import re

Start_path='F:/image/'
Start_path='C:/Users/suidx/Desktop/python/image/'
iphone5_width=1136
iphone5_depth=640
list=os.listdir(Start_path)
#print list
count=0
for pic in list:
    path=Start_path+pic
    print path
    im=Image.open(path)
    w,h=im.size
    #print w,h
    #iphone 5的分辨率为1136*640，如果图片分辨率超过这个值，进行图片的等比例压缩

    if w>iphone5_width:
        print pic
        print "图片名称为"+pic+"图片被修改"
        h_new=iphone5_width*h/w
        w_new=iphone5_width
        count=count+1
        out = im.resize((w_new,h_new),Image.ANTIALIAS)
        new_pic=re.sub(pic[:-4],pic[:-4]+'_new',pic)
        #print new_pic
        new_path=Start_path+new_pic
        out.save(new_path)

    if h>iphone5_depth:
        print pic
        print "图片名称为"+pic+"图片被修改"
        w=iphone5_depth*w/h
        h=iphone5_depth
        count=count+1
        out = im.resize((w_new,h_new),Image.ANTIALIAS)
        new_pic=re.sub(pic[:-4],pic[:-4]+'_new',pic)
        #print new_pic
        new_path=Start_path+new_pic
        out.save(new_path)

print 'END'
count=str(count)
print "共有"+count+"张图片尺寸被修改"
