python 調整圖片大小源代碼

在平時的使用過程中,經常會使用到有關於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





發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章