python修改图片分辨率

2019-08-06  本文已影响0人  Vincy_ivy
#coding=utf-8
import os  #打开文件时需要
from PIL import Image
import re

Start_path='D:/Images/女5/'
iphone5_width=350
iphone5_depth=350
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 or h!=iphone5_depth:
        print(pic)
        print("图片名称为"+pic+"图片被修改")
        h_new=350
        w_new=350
        count=count+1
        out = im.resize((w_new,h_new),Image.ANTIALIAS)
        new_pic=re.sub(pic[:-4],pic[:-4],pic)
        #print new_pic
        new_path=Start_path+new_pic
        out.save(new_path)

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

#参考网址https://blog.csdn.net/danation/article/details/76522151
上一篇 下一篇

猜你喜欢

热点阅读