Python实现自动为照片添加日期并分类的方法


Posted in Python onSeptember 30, 2017

本文实例讲述了Python实现自动为照片添加日期并分类的方法。分享给大家供大家参考,具体如下:

小时候没怎么照相,所以跟别人说小时候特别帅他们都不信。小外甥女出生了,我给买了个照相机,让她多照相。可惜他舅目前还是个?潘浚?蛄烁?00的?潘空障嗷??谷幻挥凶远?尤掌诘墓δ堋J粤思父鲂∪砑??疾缓糜茫?蟮耐枷袢砑?塾植换嵊谩I砦?桓黾扑慊?蒲в爰际踝ㄒ档难???荒茏粤⒏??恕?/p>

听说Python有个图形库,不错,在照片上打日期很容易,于是我就下了这个库。对Python不熟,一面看着手册一面写的。完成了下面的小程序,很简单。还不实用,我再修改一下,加上图形界面,并且将Python代码转换成exe,因为我要把程序给我姐用,所以要做到最傻瓜式。

(1)在相片右下角打印日期,格式类似于 2012-12-05 10:23:46

(2)以上面的日期为例,将原文件重命名为20121205102346.jpg,生成的文件命名为20121205102346DATE.jpg,并且放入文件夹20121205中,这样就可以把相片自动分类了。两个相片拍摄时间到秒数就应该不同了,除非是连拍。

代码(事先安装PIL库,http://www.pythonware.com/products/pil/)

import os,sys,shutil
from PIL import Image
from PIL import ImageDraw
from PIL.ExifTags import TAGS
from PIL import ImageFont
#open image file
if len(sys.argv) < 2:
    print "Usage: ",sys.argv[0]," ImageFile"
    sys.exit(1)
im = Image.open(sys.argv[1])
print 'Image size is:',im.size
#get the info dict
info = im._getexif()
#info store the information of the image
#it stores the info like this: [233:'name',2099:'2012:01:01 10:44:55',...]
#the key need to be decoded,
#This piece of code will extract the time when the photo is taken
for tag,value in info.items():
    decoded = TAGS.get(tag,tag)
    if decoded == 'DateTime':
        date = value
        break
#The date time is in this format '2012:01:01 10:44:22', replace the first two ":" with "-", need a writable list
date_list = []
for x in range(0,len(date)):
    date_list.append(date[x])
date_list[4] = '-'
date_list[7] = '-'
date = ''.join(date_list) #draw.text expect a string, convert it back to string
#the font size will be 1/15 of the images size
font = ImageFont.truetype("FZYTK.TTF",im.size[1] / 15)
draw = ImageDraw.Draw(im)
stringsize=draw.textsize(date,font=font)
print 'Text size is:',stringsize
#put the text to the right corner
draw.text((im.size[0]-stringsize[0],im.size[1]-stringsize[1]),date,fill=255,font=font)
#rename the source photo and the dated photo, eliminate the ':' and '-' and ' '
new_date_list = []
for x in range(0,len(date_list)):
    if date_list[x] != ':' and date_list[x] != '-' and date_list[x] != ' ':
        new_date_list.append(date_list[x])
date = ''.join(new_date_list[0:8])
time = ''.join(new_date_list[8:])
#print date
#print time
dir_name = ''.join(date)
src_filename = ''.join(new_date_list)
dst_filename = src_filename + 'DATE'
#print dir_name
#print src_filename
#print dst_filename
if not os.path.isdir(dir_name):
    os.makedirs(dir_name)
path = dir_name + '/' + dst_filename +'.JPG'
#print path
im.save(path)
shutil.copy(sys.argv[1],dir_name+'/'+src_filename+'.JPG')

效果图如下:

Python实现自动为照片添加日期并分类的方法

希望本文所述对大家Python程序设计有所帮助。

Python 相关文章推荐
python统计文本文件内单词数量的方法
May 30 Python
python 上下文管理器使用方法小结
Oct 10 Python
深入理解Python中的super()方法
Nov 20 Python
完美解决在oj中Python的循环输入问题
Jun 25 Python
python利用requests库模拟post请求时json的使用教程
Dec 07 Python
Python图像处理之gif动态图的解析与合成操作详解
Dec 30 Python
python-pyinstaller、打包后获取路径的实例
Jun 10 Python
使用python list 查找所有匹配元素的位置实例
Jun 11 Python
Mysql数据库反向生成Django里面的models指令方式
May 18 Python
在pycharm中文件取消用 pytest模式打开的操作
Sep 01 Python
python函数指定默认值的实例讲解
Mar 29 Python
详解非极大值抑制算法之Python实现
Jun 28 Python
Python实现获取照片拍摄日期并重命名的方法
Sep 30 #Python
Python3利用SMTP协议发送E-mail电子邮件的方法
Sep 30 #Python
Python字符编码与函数的基本使用方法
Sep 30 #Python
详谈Python高阶函数与函数装饰器(推荐)
Sep 30 #Python
深入浅出学习python装饰器
Sep 29 #Python
Python连接phoenix的方法示例
Sep 29 #Python
Python矩阵常见运算操作实例总结
Sep 29 #Python
You might like
php中iconv函数使用方法
2008/05/24 PHP
使用VisualStudio开发php的图文设置方法
2010/08/21 PHP
PHP Error与Logging函数的深入理解
2013/06/03 PHP
使用PHP编写发红包程序
2015/07/22 PHP
CI框架整合smarty步骤详解
2016/05/19 PHP
PHP使用星号替代用户名手机和邮箱的实现代码
2018/02/07 PHP
快速排序 php与javascript的不同之处
2011/02/22 Javascript
浅析Prototype的模板类 Template
2011/12/07 Javascript
jquery怎样实现ajax联动框(二)
2013/03/08 Javascript
Js 获取、判断浏览器版本信息的简单方法
2016/08/08 Javascript
jQuery实现鼠标滑过预览图片大图效果的方法
2017/04/26 jQuery
Node.js学习之地址解析模块URL的使用详解
2017/09/28 Javascript
微信小程序-getUserInfo回调的实例详解
2017/10/27 Javascript
html5以及jQuery实现本地图片上传前的预览代码实例讲解
2021/03/01 jQuery
python日志记录模块实例及改进
2017/02/12 Python
python实现杨辉三角思路
2017/07/14 Python
使用Python实现博客上进行自动翻页
2017/08/23 Python
django使用xlwt导出excel文件实例代码
2018/02/06 Python
python实现电脑自动关机
2018/06/20 Python
Python3爬虫之urllib携带cookie爬取网页的方法
2018/12/28 Python
python3.7实现云之讯、聚合短信平台的短信发送功能
2019/09/26 Python
python分布式编程实现过程解析
2019/11/08 Python
html5版canvas自由拼图实例
2014/10/15 HTML / CSS
德国综合购物网站:OTTO
2018/11/13 全球购物
网络、C以及其他硬件方面的面试题
2016/08/23 面试题
家庭教育先进个人事迹材料
2014/01/24 职场文书
优秀老师事迹材料
2014/02/05 职场文书
招标保密承诺书
2015/01/20 职场文书
病人家属写给医院的感谢信
2015/01/23 职场文书
开学典礼观后感
2015/06/15 职场文书
企业内部管理控制:银行存款控制制度范本
2020/01/10 职场文书
解决Golang中ResponseWriter的一个坑
2021/04/27 Golang
springcloud之Feign超时问题的解决
2021/06/24 Java/Android
Redis分布式锁的7种实现
2022/04/01 Redis
Windows Server 2016 配置 IIS 的详细步骤
2022/04/28 Servers
Android Studio 计算器开发
2022/05/20 Java/Android