python-img.resize()

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

第二个参数:
Image.NEAREST :低质量
Image.BILINEAR:双线性
Image.BICUBIC :三次样条插值
Image.ANTIALIAS:高质量

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)

 

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

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/")

2.重要函数glob.glob()

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

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

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

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

转载地址:https://blog.csdn.net/xjp_xujiping/article/details/81607964

你可能感兴趣的:(python)