Python實(shí)現(xiàn)自動(dòng)為照片添加日期并分類的方法
本文實(shí)例講述了Python實(shí)現(xiàn)自動(dòng)為照片添加日期并分類的方法。分享給大家供大家參考,具體如下:
小時(shí)候沒怎么照相,所以跟別人說小時(shí)候特別帥他們都不信。小外甥女出生了,我給買了個(gè)照相機(jī),讓她多照相??上四壳斑€是個(gè)屌絲,買了個(gè)700的屌絲照相機(jī),竟然沒有自動(dòng)加日期的功能。試了幾個(gè)小軟件,都不好用,大的圖像軟件咱又不會(huì)用。身為一個(gè)計(jì)算機(jī)科學(xué)與技術(shù)專業(yè)的學(xué)生,只能自立更生了。
聽說Python有個(gè)圖形庫,不錯(cuò),在照片上打日期很容易,于是我就下了這個(gè)庫。對(duì)Python不熟,一面看著手冊(cè)一面寫的。完成了下面的小程序,很簡(jiǎn)單。還不實(shí)用,我再修改一下,加上圖形界面,并且將Python代碼轉(zhuǎn)換成exe,因?yàn)槲乙殉绦蚪o我姐用,所以要做到最傻瓜式。
(1)在相片右下角打印日期,格式類似于 2012-12-05 10:23:46
(2)以上面的日期為例,將原文件重命名為20121205102346.jpg,生成的文件命名為20121205102346DATE.jpg,并且放入文件夾20121205中,這樣就可以把相片自動(dòng)分類了。兩個(gè)相片拍攝時(shí)間到秒數(shù)就應(yīng)該不同了,除非是連拍。
代碼(事先安裝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')
效果圖如下:

更多關(guān)于Python相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Python圖片操作技巧總結(jié)》、《Python數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Python Socket編程技巧總結(jié)》、《Python函數(shù)使用技巧總結(jié)》、《Python字符串操作技巧匯總》、《Python入門與進(jìn)階經(jīng)典教程》及《Python文件與目錄操作技巧匯總》
希望本文所述對(duì)大家Python程序設(shè)計(jì)有所幫助。
- 3行Python代碼實(shí)現(xiàn)圖像照片摳圖和換底色的方法
- 詳解Python給照片換底色(藍(lán)底換紅底)
- Python實(shí)現(xiàn)將照片變成卡通圖片的方法【基于opencv】
- 利用python爬取斗魚app中照片方法實(shí)例
- 用python找出那些被“標(biāo)記”的照片
- python實(shí)現(xiàn)圖片彩色轉(zhuǎn)化為素描
- windows下Python實(shí)現(xiàn)將pdf文件轉(zhuǎn)化為png格式圖片的方法
- Python+OpenCV+圖片旋轉(zhuǎn)并用原底色填充新四角的例子
- python實(shí)現(xiàn)視頻讀取和轉(zhuǎn)化圖片
- Python實(shí)現(xiàn)將藍(lán)底照片轉(zhuǎn)化為白底照片功能完整實(shí)例
相關(guān)文章
Python2與Python3的區(qū)別實(shí)例總結(jié)
這篇文章主要介紹了Python2與Python3的區(qū)別,結(jié)合實(shí)例形式總結(jié)分析了Python2與Python3打印輸出、編碼、數(shù)值運(yùn)算、異常處理等使用區(qū)別,需要的朋友可以參考下2019-04-04
python+openCV對(duì)視頻進(jìn)行截取的實(shí)現(xiàn)
這篇文章主要介紹了python+openCV對(duì)視頻進(jìn)行截取的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-11-11
Python 用matplotlib畫以時(shí)間日期為x軸的圖像
這篇文章主要介紹了Python 用matplotlib畫以時(shí)間日期為x軸的圖像,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-08-08
python實(shí)現(xiàn)支持目錄FTP上傳下載文件的方法
這篇文章主要介紹了python實(shí)現(xiàn)支持目錄FTP上傳下載文件的方法,適用于windows及Linux平臺(tái)FTP傳輸文件及文件夾,需要的朋友可以參考下2015-06-06
Python字典中的值為列表或字典的構(gòu)造實(shí)例
今天小編就為大家分享一篇Python字典中的值為列表或字典的構(gòu)造實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2019-12-12
Python利用三層神經(jīng)網(wǎng)絡(luò)實(shí)現(xiàn)手寫數(shù)字分類詳解
這篇文章主要介紹了如何設(shè)計(jì)一個(gè)三層神經(jīng)網(wǎng)絡(luò)模型來實(shí)現(xiàn)手寫數(shù)字分類。本文給大家介紹的非常詳細(xì),感興趣的小伙伴快來跟小編一起學(xué)習(xí)一下2021-11-11

