Python實現(xiàn)自動為照片添加日期并分類的方法
本文實例講述了Python實現(xiàn)自動為照片添加日期并分類的方法。分享給大家供大家參考,具體如下:
小時候沒怎么照相,所以跟別人說小時候特別帥他們都不信。小外甥女出生了,我給買了個照相機,讓她多照相。可惜他舅目前還是個屌絲,買了個700的屌絲照相機,竟然沒有自動加日期的功能。試了幾個小軟件,都不好用,大的圖像軟件咱又不會用。身為一個計算機科學與技術(shù)專業(yè)的學生,只能自立更生了。
聽說Python有個圖形庫,不錯,在照片上打日期很容易,于是我就下了這個庫。對Python不熟,一面看著手冊一面寫的。完成了下面的小程序,很簡單。還不實用,我再修改一下,加上圖形界面,并且將Python代碼轉(zhuǎn)換成exe,因為我要把程序給我姐用,所以要做到最傻瓜式。
(1)在相片右下角打印日期,格式類似于 2012-12-05 10:23:46
(2)以上面的日期為例,將原文件重命名為20121205102346.jpg,生成的文件命名為20121205102346DATE.jpg,并且放入文件夾20121205中,這樣就可以把相片自動分類了。兩個相片拍攝時間到秒數(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īng)典教程》及《Python文件與目錄操作技巧匯總》
希望本文所述對大家Python程序設(shè)計有所幫助。
- 3行Python代碼實現(xiàn)圖像照片摳圖和換底色的方法
- 詳解Python給照片換底色(藍底換紅底)
- Python實現(xiàn)將照片變成卡通圖片的方法【基于opencv】
- 利用python爬取斗魚app中照片方法實例
- 用python找出那些被“標記”的照片
- python實現(xiàn)圖片彩色轉(zhuǎn)化為素描
- windows下Python實現(xiàn)將pdf文件轉(zhuǎn)化為png格式圖片的方法
- Python+OpenCV+圖片旋轉(zhuǎn)并用原底色填充新四角的例子
- python實現(xiàn)視頻讀取和轉(zhuǎn)化圖片
- Python實現(xiàn)將藍底照片轉(zhuǎn)化為白底照片功能完整實例
相關(guān)文章
Python2與Python3的區(qū)別實例總結(jié)
這篇文章主要介紹了Python2與Python3的區(qū)別,結(jié)合實例形式總結(jié)分析了Python2與Python3打印輸出、編碼、數(shù)值運算、異常處理等使用區(qū)別,需要的朋友可以參考下2019-04-04Python 用matplotlib畫以時間日期為x軸的圖像
這篇文章主要介紹了Python 用matplotlib畫以時間日期為x軸的圖像,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2019-08-08python實現(xiàn)支持目錄FTP上傳下載文件的方法
這篇文章主要介紹了python實現(xiàn)支持目錄FTP上傳下載文件的方法,適用于windows及Linux平臺FTP傳輸文件及文件夾,需要的朋友可以參考下2015-06-06Python利用三層神經(jīng)網(wǎng)絡(luò)實現(xiàn)手寫數(shù)字分類詳解
這篇文章主要介紹了如何設(shè)計一個三層神經(jīng)網(wǎng)絡(luò)模型來實現(xiàn)手寫數(shù)字分類。本文給大家介紹的非常詳細,感興趣的小伙伴快來跟小編一起學習一下2021-11-11