img.resize()函数的作用和用法-单张图像变换大小

https://blog.csdn.net/qq_32801595/article/details/80461084

这个是一段学过的简单程序,可以改变图像的大小,jpg,png都可以的:

#encoding=utf-8
#author: walker
#date: 2014-05-15
#function: 更改图片尺寸大小

from PIL import Image
'''
filein: 输入图片
fileout: 输出图片
width: 输出图片宽度
height:输出图片高度
type:输出图片类型(png, gif, jpeg...)
'''
def ResizeImage(filein, fileout, width, height, type):
  img = Image.open(filein)
  out = img.resize((width, height),Image.ANTIALIAS) #resize image with high-quality
  out.save(fileout, type)
if __name__ == "__main__":
  filein = r'0.jpg'
  fileout = r'testout.png'
  width = 6000
  height = 6000
  type = 'png'
  ResizeImage(filein, fileout, width, height, type)

这个函数img.resize((width, height),Image.ANTIALIAS)
第二个参数:
Image.NEAREST :低质量
Image.BILINEAR:双线性
Image.BICUBIC :三次样条插值
Image.ANTIALIAS:高质量

上面是单张图片尺寸的改变,针对大量数据集图片,如何批量操作,记录一下,为以后数据集预处理提供一点参考:

from PIL import Image
import os.path
import glob
def convertjpg(jpgfile,outdir,width=1280,height=720):
    img=Image.open(jpgfile)   
    new_img=img.resize((width,height),Image.BILINEAR)   
    new_img.save(os.path.join(outdir,os.path.basename(jpgfile)))
for jpgfile in glob.glob("E:/test/picture/12/*.jpg"):
    convertjpg(jpgfile,"E:/test/picture/111/")

涉及到的一个重要函数glob.glob()
例如:

glob.glob(“E:/test/picture/12/*.jpg”)

返回12文件夹下所有的jpg路径

glob.glob(“E:/test/picture/111//“)

返回的是111文件夹下下个文件的所有路径

猜你喜欢

转载自blog.csdn.net/xjp_xujiping/article/details/81607964