2020-001 用python进行图片整理
(11条消息) 2020-001 用python进行图片整理_SSSimonYang的博客-CSDN博客_用python整理照片
用python进行图片整理
今天没有hr理我,单子都做完了,闲来无事把之前想做的图片整理做一下。
图片整理的基础是区分拍摄图片和表情包、截图之类的其他图片。在此,我们使用exif
信息对拍摄图片和其他图片进行区分。
对exif
信息进行一下简单介绍。
Exif
可交换图像文件格式(英语:Exchangeable image file format,官方简称Exif),是专门为数码相机的照片设定的,可以记录数码照片的属性信息和拍摄数据。
exif
的常见形式为,手机上你拍照时照片同时记录下的gps位置信息和拍照时间,数码相机存储的拍照时间。在手机上查看图片时有一个详情选项,如果记录了gps位置信息和拍照时间就会显示。同时,手机相册也会根据此信息进行照片展示和分类。
下图为一个例子:
值得注意的是,聊天软件(QQ、Wechat等)在发送图片时可能会发送图片的完整信息,即,对方在收到图片后能够通过技术手段获得你的定位。在测试时,我们发现苹果的发送后图片,其定位和拍摄时间等信息均消失,而在小米手机上,MIUI12注意到了此问题,在发送图片时可以进行信息是否抹除选项的选择,而MIUI之前的系统均存在此问题。
因此,建议大家,尤其是女生,在发送给陌生人图片时谨慎一些,注意隐私保护。此处给MIUI12打call。
当我们知道了exif
信息中存在图片拍摄时间后,稍加搜索,便可以知道python的exifread
库可以进行图片拍摄时间的提取。因此,整个项目的完成就是可预期的。
思路
在对图片进行整理时,考虑将存在拍摄时间的放在一起,将不存在拍摄时间的放在一起,然后对存在时间信息的图片根据年月进行分区。同时,在整个待处理文件夹中也可能存在一些乱入的word、视频文件。也因为大文件可能对我们更重要,小文件则相对来说更多,但是可能都是一些无用的聊天表情包之类的。因此也根据文件类型以及文件大小进行区分。
最终,第一层的文件夹结构如下:
1 2 3 4 5 6 7 8 9 10 |
-after_arrangement -img -big_withouttime -big_withtime -small -other -video -big -small |
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
生成大类目录:
1 2 3 4 5 6 7 8 |
file_category <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token string">'img'</span><span class="token punctuation">,</span> <span class="token string">'video'</span><span class="token punctuation">,</span> <span class="token string">'other'</span><span class="token punctuation">,</span> <span class="token string">'video/big'</span><span class="token punctuation">,</span> <span class="token string">'video/small'</span><span class="token punctuation">,</span> <span class="token string">'img/big_withtime'</span><span class="token punctuation">,</span> <span class="token string">'img/big_withouttime'</span><span class="token punctuation">,</span><span class="token string">'img/small'</span><span class="token punctuation">]</span> <span class="token keyword">def</span> <span class="token function">makedirs</span><span class="token punctuation">(</span>file_category<span class="token punctuation">)</span><span class="token punctuation">:</span> <span class="token keyword">for</span> category <span class="token keyword">in</span> file_category<span class="token punctuation">:</span> directory <span class="token operator">=</span> os<span class="token punctuation">.</span>path<span class="token punctuation">.</span>join<span class="token punctuation">(</span><span class="token operator">*</span>category<span class="token punctuation">.</span>split<span class="token punctuation">(</span><span class="token string">'/'</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token keyword">if</span> <span class="token operator">not</span> os<span class="token punctuation">.</span>path<span class="token punctuation">.</span>exists<span class="token punctuation">(</span>directory<span class="token punctuation">)</span><span class="token punctuation">:</span> os<span class="token punctuation">.</span>mkdir<span class="token punctuation">(</span>directory<span class="token punctuation">)</span> |
- 1
- 2
- 3
- 4
- 5
- 6
- 7
其中,category
使用/
拆分之后解包使用join
连接。
生成目录
我们想将存有拍摄时间的图片分到对应文件夹里,比如2020年06月放到2020/06
文件夹下,但是同时又不想让空的文件夹存在。如果我们在对每个文件获得时间后再查看是否存在对应的年月文件夹,在不存在时创建,其运行次数将等于照片数目,这增加了运行时间。因此,我们考虑预先生成所有年月文件夹。在此处,我们主要是在big_withtime
目录下生成这些文件夹。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
time_dir <span class="token operator">=</span> <span class="token string">'img/big_withtime'</span> <span class="token keyword">def</span> <span class="token function">timedir_prepare</span><span class="token punctuation">(</span>timedir<span class="token punctuation">)</span><span class="token punctuation">:</span> timedir <span class="token operator">=</span> os<span class="token punctuation">.</span>path<span class="token punctuation">.</span>join<span class="token punctuation">(</span><span class="token operator">*</span>timedir<span class="token punctuation">.</span>split<span class="token punctuation">(</span><span class="token string">'/'</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token keyword">for</span> i <span class="token keyword">in</span> <span class="token builtin">range</span><span class="token punctuation">(</span><span class="token number">1998</span><span class="token punctuation">,</span> time<span class="token punctuation">.</span>localtime<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span>tm_year <span class="token operator">+</span> <span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">:</span> directory <span class="token operator">=</span> os<span class="token punctuation">.</span>path<span class="token punctuation">.</span>join<span class="token punctuation">(</span>timedir<span class="token punctuation">,</span> f<span class="token string">'{i}'</span><span class="token punctuation">)</span> <span class="token keyword">if</span> <span class="token operator">not</span> os<span class="token punctuation">.</span>path<span class="token punctuation">.</span>exists<span class="token punctuation">(</span>directory<span class="token punctuation">)</span><span class="token punctuation">:</span> os<span class="token punctuation">.</span>mkdir<span class="token punctuation">(</span>directory<span class="token punctuation">)</span> <span class="token keyword">for</span> j <span class="token keyword">in</span> <span class="token builtin">range</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">13</span><span class="token punctuation">)</span><span class="token punctuation">:</span> directory <span class="token operator">=</span> os<span class="token punctuation">.</span>path<span class="token punctuation">.</span>join<span class="token punctuation">(</span>timedir<span class="token punctuation">,</span> f<span class="token string">'{i}'</span><span class="token punctuation">,</span> f<span class="token string">'{j:02}'</span><span class="token punctuation">)</span> <span class="token keyword">if</span> <span class="token operator">not</span> os<span class="token punctuation">.</span>path<span class="token punctuation">.</span>exists<span class="token punctuation">(</span>directory<span class="token punctuation">)</span><span class="token punctuation">:</span> os<span class="token punctuation">.</span>mkdir<span class="token punctuation">(</span>directory<span class="token punctuation">)</span> |
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
timedir_prepare
函数主要是生成从1998(我的出生年,不可能有更早的照片了哈哈)到今年的每年12个月对应的文件夹。
这样生成之后,图片直接储存,就不用担心对应的文件夹是否存在的问题了。
删除目录
我们生成了充分多的目录,很显然,不是所有的目录都会有文件。为了便于之后的查看,我们希望空目录是不存在的。因此,写一个timedir_end
函数,对上述所有文件夹进行从下往上的遍历,当不存在文件时,删除该文件夹。
1 2 3 4 5 6 7 8 9 10 11 12 |
<span class="token keyword">def</span> <span class="token function">timedir_end</span><span class="token punctuation">(</span>timedir<span class="token punctuation">)</span><span class="token punctuation">:</span> timedir <span class="token operator">=</span> os<span class="token punctuation">.</span>path<span class="token punctuation">.</span>join<span class="token punctuation">(</span><span class="token operator">*</span>timedir<span class="token punctuation">.</span>split<span class="token punctuation">(</span><span class="token string">'/'</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token keyword">for</span> i <span class="token keyword">in</span> <span class="token builtin">range</span><span class="token punctuation">(</span><span class="token number">1998</span><span class="token punctuation">,</span> time<span class="token punctuation">.</span>localtime<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span>tm_year <span class="token operator">+</span> <span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">:</span> <span class="token keyword">for</span> j <span class="token keyword">in</span> <span class="token builtin">range</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">13</span><span class="token punctuation">)</span><span class="token punctuation">:</span> directory <span class="token operator">=</span> os<span class="token punctuation">.</span>path<span class="token punctuation">.</span>join<span class="token punctuation">(</span>timedir<span class="token punctuation">,</span> f<span class="token string">'{i}'</span><span class="token punctuation">,</span> f<span class="token string">'{j:02}'</span><span class="token punctuation">)</span> <span class="token keyword">if</span> <span class="token operator">not</span> os<span class="token punctuation">.</span>listdir<span class="token punctuation">(</span>directory<span class="token punctuation">)</span><span class="token punctuation">:</span> os<span class="token punctuation">.</span>rmdir<span class="token punctuation">(</span>directory<span class="token punctuation">)</span> <span class="token keyword">for</span> i <span class="token keyword">in</span> <span class="token builtin">range</span><span class="token punctuation">(</span><span class="token number">1998</span><span class="token punctuation">,</span> time<span class="token punctuation">.</span>localtime<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span>tm_year <span class="token operator">+</span> <span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">:</span> directory <span class="token operator">=</span> os<span class="token punctuation">.</span>path<span class="token punctuation">.</span>join<span class="token punctuation">(</span>timedir<span class="token punctuation">,</span> f<span class="token string">'{i}'</span><span class="token punctuation">)</span> <span class="token keyword">if</span> <span class="token operator">not</span> os<span class="token punctuation">.</span>listdir<span class="token punctuation">(</span>directory<span class="token punctuation">)</span><span class="token punctuation">:</span> os<span class="token punctuation">.</span>rmdir<span class="token punctuation">(</span>directory<span class="token punctuation">)</span> |
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
注意此处的先后顺序,先删除月,后年才能因为没有内部文件夹而被删除。
获取图片时间
首先,通过命令安装exifread
库:
1 2 |
pip <span class="token function">install</span> exifread |
- 1
搜索找个示例用一下
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
<span class="token keyword">def</span> <span class="token function">process_img</span><span class="token punctuation">(</span>path<span class="token punctuation">)</span><span class="token punctuation">:</span> <span class="token triple-quoted-string string">''' 这个函数用来处理图片 并返回图片的 经纬度、拍摄时间信息 :return: 返回图片信息 是一个字典 '''</span> f <span class="token operator">=</span> <span class="token builtin">open</span><span class="token punctuation">(</span>path<span class="token punctuation">,</span> <span class="token string">'rb'</span><span class="token punctuation">)</span> tags <span class="token operator">=</span> exifread<span class="token punctuation">.</span>process_file<span class="token punctuation">(</span>f<span class="token punctuation">)</span> info <span class="token operator">=</span> <span class="token punctuation">{</span> <span class="token string">'Image DateTime(拍摄时间)'</span><span class="token punctuation">:</span> tags<span class="token punctuation">.</span>get<span class="token punctuation">(</span><span class="token string">'Image DateTime'</span><span class="token punctuation">,</span> <span class="token string">'0'</span><span class="token punctuation">)</span><span class="token punctuation">.</span>values<span class="token punctuation">,</span> <span class="token string">'GPS GPSLatitudeRef(纬度标志)'</span><span class="token punctuation">:</span> tags<span class="token punctuation">.</span>get<span class="token punctuation">(</span><span class="token string">'GPS GPSLatitudeRef'</span><span class="token punctuation">,</span> <span class="token string">'0'</span><span class="token punctuation">)</span><span class="token punctuation">.</span>values<span class="token punctuation">,</span> <span class="token string">'GPS GPSLatitude(纬度)'</span><span class="token punctuation">:</span> tags<span class="token punctuation">.</span>get<span class="token punctuation">(</span><span class="token string">'GPS GPSLatitude'</span><span class="token punctuation">,</span> <span class="token string">'0'</span><span class="token punctuation">)</span><span class="token punctuation">.</span>values<span class="token punctuation">,</span> <span class="token string">'GPS GPSLongitudeRef(经度标志)'</span><span class="token punctuation">:</span> tags<span class="token punctuation">.</span>get<span class="token punctuation">(</span><span class="token string">'GPS GPSLongitudeRef'</span><span class="token punctuation">,</span> <span class="token string">'0'</span><span class="token punctuation">)</span><span class="token punctuation">.</span>values<span class="token punctuation">,</span> <span class="token string">'GPS GPSLongitude(经度)'</span><span class="token punctuation">:</span> tags<span class="token punctuation">.</span>get<span class="token punctuation">(</span><span class="token string">'GPS GPSLongitude'</span><span class="token punctuation">,</span> <span class="token string">'0'</span><span class="token punctuation">)</span><span class="token punctuation">.</span>values <span class="token punctuation">}</span> <span class="token keyword">return</span> info |
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
代码来源于CSDN博主「大隐.」的原创文章,遵循CC 4.0 BY-SA版权协议,CSDN原文链接
显然这个信息过多,我们只需要时间就够了。
1 2 3 4 5 6 |
<span class="token keyword">def</span> <span class="token function">get_photo_time</span><span class="token punctuation">(</span><span class="token builtin">file</span><span class="token punctuation">)</span><span class="token punctuation">:</span> f <span class="token operator">=</span> <span class="token builtin">open</span><span class="token punctuation">(</span><span class="token builtin">file</span><span class="token punctuation">,</span> <span class="token string">'rb'</span><span class="token punctuation">)</span> tags <span class="token operator">=</span> exifread<span class="token punctuation">.</span>process_file<span class="token punctuation">(</span>f<span class="token punctuation">)</span> photo_time <span class="token operator">=</span> tags<span class="token punctuation">.</span>get<span class="token punctuation">(</span><span class="token string">'Image DateTime'</span><span class="token punctuation">)</span><span class="token punctuation">.</span>values <span class="token keyword">return</span> photo_time |
- 1
- 2
- 3
- 4
- 5
但是可以预知的是,不是所有的图片都存在exif
信息,也有可能exif
信息里没有时间,对这种情况我们希望返回None
。同时,我们最后所用到的只有年和月,因此需要对类似2019:03:28 21:08:36
形式的信息进行提取后加工。
更改后如下:
1 2 3 4 5 6 7 8 |
<span class="token keyword">def</span> <span class="token function">get_photo_time</span><span class="token punctuation">(</span><span class="token builtin">file</span><span class="token punctuation">)</span><span class="token punctuation">:</span> f <span class="token operator">=</span> <span class="token builtin">open</span><span class="token punctuation">(</span><span class="token builtin">file</span><span class="token punctuation">,</span> <span class="token string">'rb'</span><span class="token punctuation">)</span> tags <span class="token operator">=</span> exifread<span class="token punctuation">.</span>process_file<span class="token punctuation">(</span>f<span class="token punctuation">)</span> <span class="token keyword">if</span> tags <span class="token operator">and</span> <span class="token string">'Image DateTime'</span> <span class="token keyword">in</span> tags<span class="token punctuation">:</span> photo_time <span class="token operator">=</span> tags<span class="token punctuation">.</span>get<span class="token punctuation">(</span><span class="token string">'Image DateTime'</span><span class="token punctuation">)</span><span class="token punctuation">.</span>values <span class="token keyword">return</span> time<span class="token punctuation">.</span>strftime<span class="token punctuation">(</span><span class="token string">'%Y/%m'</span><span class="token punctuation">,</span> time<span class="token punctuation">.</span>strptime<span class="token punctuation">(</span>photo_time<span class="token punctuation">,</span> <span class="token string">'%Y:%m:%d %H:%M:%S'</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token keyword">return</span> <span class="token boolean">None</span> |
- 1
- 2
- 3
- 4
- 5
- 6
- 7
该函数的返回值则类似2019/03
形式,是我们所期望的。
文件移动
在找到文件并且知道他该去哪的时候,就需要进行文件的复制或移动。稍加搜索便可以知道shutil
库可以解决该问题。
主要使用的函数为复制shutil.copyfile
,移动shutil.move
,参数为原路径和新路径。
在移动时根据参数进行文件路径的拼接生成,因此函数如下。
1 2 3 4 5 6 7 8 9 10 11 12 |
<span class="token keyword">def</span> <span class="token function">move</span><span class="token punctuation">(</span>to_where<span class="token punctuation">,</span> file_path<span class="token punctuation">,</span> ext<span class="token punctuation">,</span> remove<span class="token operator">=</span><span class="token boolean">False</span><span class="token punctuation">,</span> photo_time<span class="token operator">=</span><span class="token boolean">None</span><span class="token punctuation">)</span><span class="token punctuation">:</span> <span class="token keyword">if</span> to_where<span class="token punctuation">.</span>endswith<span class="token punctuation">(</span><span class="token string">'withtime'</span><span class="token punctuation">)</span> <span class="token operator">and</span> photo_time<span class="token punctuation">:</span> to_file_path <span class="token operator">=</span> os<span class="token punctuation">.</span>path<span class="token punctuation">.</span>join<span class="token punctuation">(</span><span class="token operator">*</span>to_where<span class="token punctuation">.</span>split<span class="token punctuation">(</span><span class="token string">'/'</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token operator">*</span>photo_time<span class="token punctuation">.</span>split<span class="token punctuation">(</span><span class="token string">'/'</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token keyword">else</span><span class="token punctuation">:</span> to_file_path <span class="token operator">=</span> os<span class="token punctuation">.</span>path<span class="token punctuation">.</span>join<span class="token punctuation">(</span><span class="token operator">*</span>to_where<span class="token punctuation">.</span>split<span class="token punctuation">(</span><span class="token string">'/'</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token keyword">if</span> remove<span class="token punctuation">:</span> shutil<span class="token punctuation">.</span>move<span class="token punctuation">(</span>file_path<span class="token punctuation">,</span> os<span class="token punctuation">.</span>path<span class="token punctuation">.</span>join<span class="token punctuation">(</span>f<span class="token string">'{to_file_path}'</span><span class="token punctuation">,</span> f<span class="token string">'{len(os.listdir(to_file_path)) + 1:03}'</span><span class="token punctuation">)</span> <span class="token operator">+</span> <span class="token string">'.'</span> <span class="token operator">+</span> ext<span class="token punctuation">)</span> <span class="token keyword">else</span><span class="token punctuation">:</span> shutil<span class="token punctuation">.</span>copyfile<span class="token punctuation">(</span>file_path<span class="token punctuation">,</span> os<span class="token punctuation">.</span>path<span class="token punctuation">.</span>join<span class="token punctuation">(</span>f<span class="token string">'{to_file_path}'</span><span class="token punctuation">,</span> f<span class="token string">'{len(os.listdir(to_file_path)) + 1:03}'</span><span class="token punctuation">)</span> <span class="token operator">+</span> <span class="token string">'.'</span> <span class="token operator">+</span> ext<span class="token punctuation">)</span> |
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
其中,to_where
是类似img/big_withtime
的形式,file_path
是原文件的地址,ext
是文件后缀名,remove
可以控制是否保留原文件,photo_time
信息由get_photo_time
函数获得。文件的命名采取顺序命名,三位靠右填充0,当num=1
时{num:03}
呈现出001
的形式。
文件夹遍历
稍加搜索,遍历的实现库函数是os.walk
,该函数默认从上到下(topdown=False
时从下到上)遍历每一个文件夹,返回root dirs files
- root str,当前正在遍历的文件夹的地址
- dirs list,该文件夹中所有的目录
- files list, 该文件夹中所有的文件
先写出如下函数:
1 2 3 4 5 6 7 8 9 10 |
<span class="token keyword">def</span> <span class="token function">main</span><span class="token punctuation">(</span>process_directory<span class="token punctuation">,</span> to_directory<span class="token punctuation">,</span> remove<span class="token operator">=</span><span class="token boolean">False</span><span class="token punctuation">)</span><span class="token punctuation">:</span> curdir <span class="token operator">=</span> os<span class="token punctuation">.</span>path<span class="token punctuation">.</span>abspath<span class="token punctuation">(</span>os<span class="token punctuation">.</span>curdir<span class="token punctuation">)</span> os<span class="token punctuation">.</span>chdir<span class="token punctuation">(</span>to_directory<span class="token punctuation">)</span> makedirs<span class="token punctuation">(</span>file_category<span class="token punctuation">)</span> timedir_prepare<span class="token punctuation">(</span>time_dir<span class="token punctuation">)</span> <span class="token keyword">for</span> root<span class="token punctuation">,</span> dirs<span class="token punctuation">,</span> files <span class="token keyword">in</span> os<span class="token punctuation">.</span>walk<span class="token punctuation">(</span>process_directory<span class="token punctuation">)</span><span class="token punctuation">:</span> process_files<span class="token punctuation">(</span>root<span class="token punctuation">,</span> files<span class="token punctuation">,</span> remove<span class="token punctuation">)</span> timedir_end<span class="token punctuation">(</span>time_dir<span class="token punctuation">)</span> os<span class="token punctuation">.</span>chdir<span class="token punctuation">(</span>curdir<span class="token punctuation">)</span> |
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
因为之后的处理都是类似img/big_withtime
的目录名,因此首先保留当前运行目录,切换至目标写入目录,最后切换回来。中间先生成第一级目录,然后生成年月目录。
对于处理该目录下所有文件的函数process_files
还没写,主要的工作是完成类型鉴定,以及指定move
需要的to_where
。我们主要依靠文件的后缀名进行文件分类,但是考虑到后缀名可能是大写的以及jpg png bmp
均为img
,因此,此处需要对后缀名删除点号后变小写再进行映射。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
format_map <span class="token operator">=</span> <span class="token punctuation">{</span><span class="token string">'jpg'</span><span class="token punctuation">:</span> <span class="token string">'img'</span><span class="token punctuation">,</span> <span class="token string">'jpeg'</span><span class="token punctuation">:</span> <span class="token string">'img'</span><span class="token punctuation">,</span> <span class="token string">'png'</span><span class="token punctuation">:</span> <span class="token string">'img'</span><span class="token punctuation">,</span> <span class="token string">'bmp'</span><span class="token punctuation">:</span> <span class="token string">'img'</span><span class="token punctuation">,</span> <span class="token string">'gif'</span><span class="token punctuation">:</span> <span class="token string">'img'</span><span class="token punctuation">,</span> <span class="token string">'mp4'</span><span class="token punctuation">:</span> <span class="token string">'video'</span><span class="token punctuation">}</span> <span class="token keyword">def</span> <span class="token function">process_files</span><span class="token punctuation">(</span>root<span class="token punctuation">,</span> files<span class="token punctuation">,</span> remove<span class="token punctuation">)</span><span class="token punctuation">:</span> <span class="token keyword">for</span> <span class="token builtin">file</span> <span class="token keyword">in</span> files<span class="token punctuation">:</span> file_path <span class="token operator">=</span> os<span class="token punctuation">.</span>path<span class="token punctuation">.</span>join<span class="token punctuation">(</span>root<span class="token punctuation">,</span> <span class="token builtin">file</span><span class="token punctuation">)</span> ext <span class="token operator">=</span> os<span class="token punctuation">.</span>path<span class="token punctuation">.</span>splitext<span class="token punctuation">(</span>file_path<span class="token punctuation">)</span><span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">:</span><span class="token punctuation">]</span><span class="token punctuation">.</span>lower<span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token keyword">if</span> ext <span class="token operator">not</span> <span class="token keyword">in</span> format_map<span class="token punctuation">.</span>keys<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">:</span> file_type <span class="token operator">=</span> <span class="token builtin">input</span><span class="token punctuation">(</span>f<span class="token string">"{ext}属于何种类型:"</span><span class="token punctuation">)</span> <span class="token keyword">while</span> file_type <span class="token operator">not</span> <span class="token keyword">in</span> <span class="token punctuation">[</span><span class="token string">'img'</span><span class="token punctuation">,</span> <span class="token string">'video'</span><span class="token punctuation">,</span> <span class="token string">'other'</span><span class="token punctuation">]</span><span class="token punctuation">:</span> <span class="token keyword">print</span><span class="token punctuation">(</span>f<span class="token string">"请输入img或video或other"</span><span class="token punctuation">)</span> file_type <span class="token operator">=</span> <span class="token builtin">input</span><span class="token punctuation">(</span>f<span class="token string">"{ext}属于何种类型:"</span><span class="token punctuation">)</span> format_map<span class="token punctuation">[</span>ext<span class="token punctuation">]</span> <span class="token operator">=</span> file_type photo_time <span class="token operator">=</span> <span class="token boolean">None</span> to_where <span class="token operator">=</span> <span class="token string">''</span> <span class="token keyword">if</span> format_map<span class="token punctuation">[</span>ext<span class="token punctuation">]</span> <span class="token operator">==</span> <span class="token string">'img'</span><span class="token punctuation">:</span> <span class="token keyword">if</span> os<span class="token punctuation">.</span>path<span class="token punctuation">.</span>getsize<span class="token punctuation">(</span>file_path<span class="token punctuation">)</span> <span class="token operator">>=</span> <span class="token number">1024</span> <span class="token operator">**</span> <span class="token number">2</span><span class="token punctuation">:</span> photo_time <span class="token operator">=</span> get_photo_time<span class="token punctuation">(</span>file_path<span class="token punctuation">)</span> <span class="token keyword">if</span> photo_time<span class="token punctuation">:</span> to_where <span class="token operator">=</span> <span class="token string">'img/big_withtime'</span> <span class="token keyword">else</span><span class="token punctuation">:</span> to_where <span class="token operator">=</span> <span class="token string">'img/big_withouttime'</span> <span class="token keyword">else</span><span class="token punctuation">:</span> to_where <span class="token operator">=</span> <span class="token string">'img/small'</span> <span class="token keyword">if</span> format_map<span class="token punctuation">[</span>ext<span class="token punctuation">]</span> <span class="token operator">==</span> <span class="token string">'video'</span><span class="token punctuation">:</span> <span class="token keyword">if</span> os<span class="token punctuation">.</span>path<span class="token punctuation">.</span>getsize<span class="token punctuation">(</span>file_path<span class="token punctuation">)</span> <span class="token operator">>=</span> <span class="token number">1024</span> <span class="token operator">**</span> <span class="token number">2</span><span class="token punctuation">:</span> to_where <span class="token operator">=</span> <span class="token string">'video/big'</span> <span class="token keyword">else</span><span class="token punctuation">:</span> to_where <span class="token operator">=</span> <span class="token string">'video/small'</span> <span class="token keyword">if</span> format_map<span class="token punctuation">[</span>ext<span class="token punctuation">]</span> <span class="token operator">==</span> <span class="token string">'other'</span><span class="token punctuation">:</span> to_where <span class="token operator">=</span> <span class="token string">'other'</span> move<span class="token punctuation">(</span>to_where<span class="token punctuation">,</span> file_path<span class="token punctuation">,</span> ext<span class="token punctuation">,</span> remove<span class="token punctuation">,</span> photo_time<span class="token punctuation">)</span> |
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
while
部分主要解决出现预知不到的类型的问题,出现时可以手动指示该文件是img video other
中的哪一种,使软件能够应对突如其来的其他类型文件。然后主要根据获得的文件后缀名ext
以及文件是否大于1024^2(1M)来进行文件夹的选择,最后使用move进行具体文件操作。
完善
项目需要的大部分操作都已完成,但是,思考一下,如果每次遇见新文件都要你手动输入类型,那太烦了,如果软件具有记忆功能就好了。我们选择使用json
作为format_map
信息文件存储的形式,在你手动输入陌生后缀名对应的类型后,json
文件便会增加一条记录。当再次运行该软件时,就不用再次输入了。同时,为了便于使用,我们采用argparse
库进行命令行参数解析。
1 2 3 4 5 6 7 8 9 10 11 12 |
<span class="token keyword">if</span> __name__ <span class="token operator">==</span> <span class="token string">'__main__'</span><span class="token punctuation">:</span> parser <span class="token operator">=</span> argparse<span class="token punctuation">.</span>ArgumentParser<span class="token punctuation">(</span><span class="token punctuation">)</span> parser<span class="token punctuation">.</span>add_argument<span class="token punctuation">(</span><span class="token string">"-d"</span><span class="token punctuation">,</span> <span class="token string">"--dir"</span><span class="token punctuation">,</span> <span class="token builtin">help</span><span class="token operator">=</span><span class="token string">"specify the image directory you want to process"</span><span class="token punctuation">)</span> parser<span class="token punctuation">.</span>add_argument<span class="token punctuation">(</span><span class="token string">"-o"</span><span class="token punctuation">,</span> <span class="token string">"--out"</span><span class="token punctuation">,</span> <span class="token builtin">help</span><span class="token operator">=</span><span class="token string">"specify the directory to store file, empty is recommended"</span><span class="token punctuation">)</span> parser<span class="token punctuation">.</span>add_argument<span class="token punctuation">(</span><span class="token string">"-remove"</span><span class="token punctuation">,</span> <span class="token string">"--remove"</span><span class="token punctuation">,</span> <span class="token builtin">help</span><span class="token operator">=</span><span class="token string">"remove the raw file or false"</span><span class="token punctuation">,</span> action<span class="token operator">=</span><span class="token string">"store_true"</span><span class="token punctuation">)</span> args <span class="token operator">=</span> parser<span class="token punctuation">.</span>parse_args<span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token keyword">with</span> <span class="token builtin">open</span><span class="token punctuation">(</span><span class="token string">'format.json'</span><span class="token punctuation">,</span> <span class="token string">'r'</span><span class="token punctuation">)</span> <span class="token keyword">as</span> f<span class="token punctuation">:</span> format_map <span class="token operator">=</span> json<span class="token punctuation">.</span>load<span class="token punctuation">(</span>f<span class="token punctuation">)</span> main<span class="token punctuation">(</span>args<span class="token punctuation">.</span><span class="token builtin">dir</span><span class="token punctuation">,</span> args<span class="token punctuation">.</span>out<span class="token punctuation">,</span> args<span class="token punctuation">.</span>remove<span class="token punctuation">)</span> <span class="token keyword">with</span> <span class="token builtin">open</span><span class="token punctuation">(</span><span class="token string">'format.json'</span><span class="token punctuation">,</span> <span class="token string">'w'</span><span class="token punctuation">)</span> <span class="token keyword">as</span> f<span class="token punctuation">:</span> json<span class="token punctuation">.</span>dump<span class="token punctuation">(</span>format_map<span class="token punctuation">,</span> f<span class="token punctuation">,</span> indent<span class="token operator">=</span><span class="token number">2</span><span class="token punctuation">)</span> |
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
命令行运行,当最后添加-remove
时,表示需要删除原文件。
1 2 3 |
python image_arrangement<span class="token punctuation">.</span>py <span class="token operator">-</span>d C:\Users\sssimonyang\Pictures\before_arrangement <span class="token operator">-</span>o C :\Users\sssimonyang\Pictures\after_arrangement |
- 1
- 2
bug fix
在运行时出现了一些错误,主要问题在exif
信息处理。
这个日期后面还带个下午就很有意思,但是也没有办法。
同时也有存在Image DateTime
键但是对应值为空值的情况,也对这种情况进行处理。修改后的函数如下:
1 2 3 4 5 6 7 8 9 |
<span class="token keyword">def</span> <span class="token function">get_photo_time</span><span class="token punctuation">(</span><span class="token builtin">file</span><span class="token punctuation">)</span><span class="token punctuation">:</span> f <span class="token operator">=</span> <span class="token builtin">open</span><span class="token punctuation">(</span><span class="token builtin">file</span><span class="token punctuation">,</span> <span class="token string">'rb'</span><span class="token punctuation">)</span> tags <span class="token operator">=</span> exifread<span class="token punctuation">.</span>process_file<span class="token punctuation">(</span>f<span class="token punctuation">)</span> <span class="token keyword">if</span> tags <span class="token operator">and</span> <span class="token string">'Image DateTime'</span> <span class="token keyword">in</span> tags<span class="token punctuation">:</span> photo_time <span class="token operator">=</span> tags<span class="token punctuation">.</span>get<span class="token punctuation">(</span><span class="token string">'Image DateTime'</span><span class="token punctuation">)</span><span class="token punctuation">.</span>values <span class="token keyword">if</span> photo_time<span class="token punctuation">:</span> <span class="token keyword">return</span> time<span class="token punctuation">.</span>strftime<span class="token punctuation">(</span><span class="token string">'%Y/%m'</span><span class="token punctuation">,</span> time<span class="token punctuation">.</span>strptime<span class="token punctuation">(</span>photo_time<span class="token punctuation">[</span><span class="token punctuation">:</span><span class="token number">19</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token string">'%Y:%m:%d %H:%M:%S'</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token keyword">return</span> <span class="token boolean">None</span> |
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
最后
至此,大功告成,代码文件在13:45创建,18点前已经基本完成,花费4h。代码存储在github:https://github.com/sssimonyang/tools
其实,考虑到小图片中也有一些人像照片,比如说个人的历史照片、表情包之类的,其实都很有价值。未来,我们想使用opencv
的图像识别功能对小图片进行处理,分成有人像和无人像两大类,然后对于这些exif
信息中没有时间或者非拍摄类型的图片,其实还可以利用文件生成时间进行分类。这些可以作为进一步的工作。如果你有好的建议和想法,也欢迎提出。
我
我是 SSSimon Yang,关注我,用code解读世界
转载请注明:徐自远的乱七八糟小站 » 用python进行图片整理