python批处理将图片进行放大

212 阅读1分钟

python批处理将图片进行放大

有时候对于网络识别,将原始图片放进网络中并不能达到自己想要的效果,但是有时候如果将图片放大之后,识别率却能够达到意想不到的结果现在提供一种将文件中的图片进行批处理的代码:

 import os
 ​
 from PIL import Image
 import sys
  
  
 #获取path目录下的所有文件
 def get_imlist(path):
     return[os.path.join(path,f)
            for f in os.listdir(path)]
  
  
 def change_size(path):
     directorys=get_imlist(path)
     for directory in directorys:
     #不是图片文件就跳过
         print(directory)
         if not(directory.endswith('.jpg') or directory.endswith('.png') or directory.endswith('.bmp')):  ##b
             pass
         else:
             img=Image.open(directory)
             s="/"
             #获取文件名(含后缀)
             oimage_name=directory[directory.rfind(s)+1:]
             (oimage_width,oimage_height)=img.size
             new_width=oimage_width * 3
             new_height=oimage_height * 3
             out=img.resize((new_width,new_height),Image.ANTIALIAS)
             out.save("%s" %oimage_name)  #直接替换
  
  
 if __name__ == '__main__':
     change_size("F:\桌面\test")

\