alex_bn_lee

导航

【327】Python 中 PIL 实现图像缩放

参考:Python 中使用PIL中的resize 进行缩放

参考:Python用Pillow(PIL)进行简单的图像操作(模糊、边缘增强、锐利、平滑等)

参考:廖雪峰 - Pillow


  实现代码如下:

from PIL import ImageGrab
img = Image.open('D:/tmp/4.jpg')
# 获取图像的大小
print(img.size)
# 获取图像 width
print(img.size[0])
# 获取图像 height
print(img.size[1])

img = img.resize((width, height),Image.ANTIALIAS) 

  实现批量修改图片的尺寸,可以自定义输入和输出文件以及缩放比例。

代码如下:

# coding=utf-8
# 批量修改图片尺寸
# imageResize(r"D:\tmp", r"D:\tmp\3", 0.7)

from PIL import ImageGrab
import os

def imageResize(input_path, output_path, scale):
	# 获取输入文件夹中的所有文件/夹,并改变工作空间
	files = os.listdir(input_path)
	os.chdir(input_path)
	# 判断输出文件夹是否存在,不存在则创建
	if(not os.path.exists(output_path)):
		os.makedirs(output_path)
	for file in files:
		# 判断是否为文件,文件夹不操作
		if(os.path.isfile(file)):
			img = Image.open(file)
			width = int(img.size[0]*scale)
			height = int(img.size[1]*scale)
			img = img.resize((width, height), Image.ANTIALIAS)
			img.save(os.path.join(output_path, "New_"+file)

 

posted on 2018-06-23 21:15  McDelfino  阅读(26884)  评论(0编辑  收藏  举报