当前位置:嗨网首页>书籍在线阅读

25-在硬盘上识别照片文件夹

  
选择背景色: 黄橙 洋红 淡粉 水蓝 草绿 白色 选择字体: 宋体 黑体 微软雅黑 楷体 选择字体大小: 恢复默认

19.7.2 在硬盘上识别照片文件夹

我有一个坏习惯,从数码相机将文件传输到硬盘的临时文件夹后,会忘记这些文件夹。下面来编程扫描整个硬盘,找到这些遗忘的“照片文件夹”。

编写一个程序,遍历硬盘上的每个文件夹,找到可能的照片文件夹。当然,首先你必须定义什么是“照片文件夹”——假定就是超过半数文件是照片的任何文件夹。你如何定义什么文件是照片?

首先,照片文件必须具有文件扩展名.png或.jpg。此外,照片是很大的图像。照片文件的宽度和高度都必须大于500像素。这是比较含蓄的假定,因为大多数数码相机照片的宽度和高度都是几千像素。

作为提示,下面是这个程序的粗略框架:

#! python3
# Import modules and write comments to describe this program.
for foldername, subfolders, filenames in os.walk('C:\\'):
     numPhotoFiles = 0
     numNonPhotoFiles  =  0
     for filename in filenames:
          # Check if file extension isn't .png or .jpg.
     if TODO:
         numNonPhotoFiles  +=  1
         continue    # skip to next filename
     # Open image file using Pillow.
     # Check if width & height are larger than 500.
     if TODO:
         # Image is large enough to be considered a photo.
         numPhotoFiles += 1
     else:
         # Image is too small to be a photo.
         numNonPhotoFiles += 1
# If more than half of files were photos,
# print the absolute path of the folder.
if TODO:
    print(TODO)

程序运行时,它应该在屏幕上输出所有照片文件夹的绝对路径。