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

python 调整图片大小源代码

2016-03-09 13:55 471 查看
在平时的使用过程中,经常会使用到有关于python调整图片的需求,所以现将源代码po出,将来有需要会直接找到

import Image
import os
path = os.getcwd()
rate = 3.06
for file in os.listdir(path):
if file.split('.')[-1] == 'JPG':
file_path = os.path.join(path,file)
print file_path
im = Image.open(file_path)
(x,y) = im.size
x_small = int(round(x/rate))
y_small = int(round(y/rate))
print x_small
out = im.resize((x_small,y_small),Image.ANTIALIAS)
out.save(file_path)


import os
import Image
path = os.getcwd()
index = 1
for file in os.listdir(path):
file_path = os.path.join(path,file)
print '*****************   entering the proceduring of images'
if file.split('.')[-1] == 'JPG' or file.split('.')[-1] == 'jpg':
im = Image.open(file_path)
(x,y) = im.size
if x > y:
rate = float(x)/800
x_small = 800
y_small = int(round(y/rate))
print '------------------ done   ' + file
else:
rate = float(y)/800
y_small = 800
x_small = int(round(x/rate))
print '------------------ done   ' + file

out = im.resize((x_small,y_small) , Image.ANTIALIAS)
out.save(file_path)
print str(index)
index+=1



                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: