[Python] 获取指定文件夹下所有图片的名字、长和宽 →→→→→进入此内容的聊天室

来自 , 2021-03-21, 写在 Python, 查看 148 次.
URL http://www.code666.cn/view/f466e84e
  1. # home.zhenliang@gmail.com
  2. # http://t.qq.com/zhenliang
  3. # 获取指定文件夹下所有图片的名字、长和宽
  4.  
  5. import os, sys
  6. from stat import *
  7.  
  8. import Image
  9.  
  10. PicPathNameList = []
  11. PicWidthList = []
  12. PicHeightList = []
  13.  
  14. def WalkTree(top, callback):
  15.  
  16.     for f in os.listdir(top):
  17.        
  18.         pathname = os.path.join(top, f)
  19.         mode = os.stat(pathname)[ST_MODE]
  20.        
  21.         if S_ISDIR(mode):
  22.             WalkTree(pathname, callback)
  23.         elif S_ISREG(mode):
  24.             callback(pathname)
  25.         else:
  26.             print 'Skipping %s' % pathname
  27.  
  28. def GetPicInfo(file):
  29.  
  30.     global PicPathNameList
  31.     global PicWidthList
  32.     global PicHeightList
  33.  
  34.     try:    
  35.         image = Image.open(file)
  36.         PicPathNameList.append(file)      
  37.         PicWidthList.append(image.size[0])
  38.         PicHeightList.append(image.size[1])
  39.     except IOError:
  40.         pass
  41.  
  42. if __name__ == '__main__':
  43.     WalkTree(top, GetPicInfo)
  44.     print "PicPathNameList Begin"
  45.     print PicPathNameList
  46.     print PicWidthList
  47.     print PicHeightList
  48.     print "PicPathNameList End"
  49. #//python/1188

回复 "获取指定文件夹下所有图片的名字、长和宽"

这儿你可以回复上面这条便签

captcha