1+ #!/usr/bin/env python3
2+ # -*- coding: utf-8 -*-
3+ """
4+ 相册照片统计脚本
5+ 统计相册中的照片数量,并按类型分类
6+ """
7+
8+ import photos
9+ import dialogs
10+ import console
11+ from datetime import datetime
12+
13+ def main ():
14+ """主函数:统计相册照片"""
15+ console .clear ()
16+ print ("📸 相册照片统计" )
17+ print ("=" * 40 )
18+
19+ # 检查相册权限
20+ if not photos .is_available ():
21+ print ("❌ 无法访问相册,请检查权限设置" )
22+ dialogs .alert ("权限错误" , "无法访问相册,请检查App权限设置" )
23+ return
24+
25+ print ("正在扫描相册..." )
26+
27+ try :
28+ # 获取所有照片
29+ all_photos = photos .get_assets (media_type = 'photo' , limit = 0 )
30+ total_count = len (all_photos )
31+
32+ # 获取所有视频
33+ all_videos = photos .get_assets (media_type = 'video' , limit = 0 )
34+ video_count = len (all_videos )
35+
36+ # 获取所有媒体(照片+视频)
37+ all_media = photos .get_assets (media_type = 'all' , limit = 0 )
38+ media_count = len (all_media )
39+
40+ print (f"✅ 扫描完成!" )
41+ print ()
42+
43+ # 显示统计结果
44+ print ("📊 统计结果:" )
45+ print (f" 照片数量:{ total_count } 张" )
46+ print (f" 视频数量:{ video_count } 个" )
47+ print (f" 媒体总数:{ media_count } 个" )
48+ print ()
49+
50+ # 如果有照片,显示一些详细信息
51+ if total_count > 0 :
52+ # 获取最近的照片
53+ recent_photos = photos .get_recent_images (count = min (5 , total_count ))
54+ print ("📅 最近的照片:" )
55+ for i , asset in enumerate (recent_photos , 1 ):
56+ try :
57+ # 尝试获取日期信息
58+ if hasattr (asset , 'creation_date' ):
59+ # 确保 creation_date 是 datetime 对象
60+ from datetime import datetime
61+ if isinstance (asset .creation_date , (int , float )):
62+ # 如果是时间戳,转换为 datetime
63+ date_obj = datetime .fromtimestamp (asset .creation_date )
64+ date_str = date_obj .strftime ("%Y-%m-%d %H:%M" )
65+ else :
66+ # 假设是 datetime 对象
67+ date_str = asset .creation_date .strftime ("%Y-%m-%d %H:%M" )
68+ else :
69+ date_str = "未知日期"
70+ except :
71+ date_str = "日期未知"
72+
73+ # 获取照片尺寸
74+ width = getattr (asset , 'pixel_width' , '未知' )
75+ height = getattr (asset , 'pixel_height' , '未知' )
76+ print (f" { i } . { date_str } - { width } x{ height } " )
77+
78+ # 获取相册信息
79+ print ()
80+ print ("📁 相册信息:" )
81+ albums = photos .get_albums ()
82+ smart_albums = photos .get_smart_albums ()
83+
84+ print (f" 普通相册:{ len (albums )} 个" )
85+ print (f" 智能相册:{ len (smart_albums )} 个" )
86+
87+ # 显示一些特殊相册
88+ special_albums = []
89+ try :
90+ screenshots = photos .get_screenshots_album ()
91+ if screenshots :
92+ special_albums .append (f"截屏 ({ screenshots .count } 张)" )
93+ except :
94+ pass
95+
96+ try :
97+ recently_added = photos .get_recently_added_album ()
98+ if recently_added :
99+ special_albums .append (f"最近添加 ({ recently_added .count } 张)" )
100+ except :
101+ pass
102+
103+ try :
104+ selfies = photos .get_selfies_album ()
105+ if selfies :
106+ special_albums .append (f"自拍 ({ selfies .count } 张)" )
107+ except :
108+ pass
109+
110+ if special_albums :
111+ print (f" 特殊相册:{ ', ' .join (special_albums )} " )
112+
113+ print ()
114+ print ("=" * 40 )
115+ print ("📱 操作提示:" )
116+ print (" 1. 要查看照片详情,可以使用 photos.pick_image()" )
117+ print (" 2. 要拍照,可以使用 photos.capture_image()" )
118+ print (" 3. 要保存图片到相册,可以使用 photos.save_image()" )
119+
120+ # 显示弹窗总结
121+ summary = f"""
122+ 照片统计完成!
123+
124+ 📸 照片:{ total_count } 张
125+ 🎬 视频:{ video_count } 个
126+ 📁 相册:{ len (albums )} 个普通相册
127+ { len (smart_albums )} 个智能相册
128+
129+ 点击确定继续...
130+ """
131+ dialogs .alert ("相册统计结果" , summary .strip ())
132+
133+ except Exception as e :
134+ print (f"❌ 发生错误:{ e } " )
135+ dialogs .alert ("错误" , f"统计相册时发生错误:{ e } " )
136+
137+ if __name__ == "__main__" :
138+ main ()
0 commit comments