動感網(wǎng)頁相冊 python編寫簡單文件夾內(nèi)圖片瀏覽工具
不知道大家有沒有這樣的體驗,windows電腦上查看一張gif圖,默認就把IE給打開了,還彈出個什么詢問項,好麻煩的感覺。所以為了解決自己的這個問題,寫了個簡單的文件夾內(nèi)圖片瀏覽工具。
效果圖
以E盤某一文件夾為例
效果圖
實現(xiàn)思路
業(yè)務代碼
# coding:utf-8 import sys reload(sys) sys.setdefaultencoding('utf8') # __author__ = '郭 璞' # __date__ = '2016/8/5' # __Desc__ = 自動生成網(wǎng)頁相冊 import os # 呵呵了,原來有標準庫中的walk方法。那么這個方法就獲得一個文件夾下的圖片文件吧 def getFiles(filepath): files = [] if os.path.isdir(filepath): for file in os.listdir(filepath): if os.path.isdir(file): getFiles(file) elif file.endswith('.jpg') or file.endswith('.png') or file.endswith('.gif'): files.append(filepath + str(file)) elif os.path.isfile(filepath): files.append(filepath) return files # 獲取給定目錄下所有以.jpg .png .gif結(jié)尾的文件,并補全路徑保存到列表中輸出 def recourse(filepath): files = [] for fpathe, dirs, fs in os.walk(filepath): for f in fs: if f.endswith('.jpg') or f.endswith('.png') or f.endswith('.gif'): files.append(os.path.join(fpathe, f)) return files # 生成網(wǎng)頁源碼文件,指定 def generate(files, shuffle=False): template_start = ''' <html><head><meta charset='utf-8'><title>網(wǎng)頁版相冊</title><link rel="stylesheet" type="text/css" href="csshake-slow.min.css"> <link rel="stylesheet" type="text/css" ></script></head><body> ''' template_body = '' # 如果指定亂序,就亂序列表中的數(shù)據(jù) if shuffle == True: from random import shuffle shuffle(files) for file in files: template_body += '<a href="' + file + '"><img class="shake-slow" src="' + file + '" style="width:64px;height:auto;"></a>' template_end = ''' </body></html> '''% html = template_start + template_body + template_end return html # 生成html文件,并輸出到指定的目錄 def write2File(filepath, data): file = open(filepath, 'wb') file.write(data) file.close() print 'Write to file Scuuess!' if __name__ == "__main__": # E:\\Picture\\LOFTER\\ filepath = 'E:\\Picture\\LOFTER\\' files = recourse(filepath=filepath) for item in files: print item html = generate(files, True) output_path = r'C:\Users\Administrator\Desktop\test.html' write2File(filepath=output_path, data=html) print 'HTML相冊文件已生成在桌面,請查看'
總結(jié)
•首先說一下缺點:
◦缺點很明顯,對于中文支持的不夠好,因為查看圖片大圖的時候是以超鏈接的形式出現(xiàn)的,所以會發(fā)生亂碼的情況。
◦然后是優(yōu)點:
優(yōu)點不是很明顯,因為如果一個文件夾下面有很多的子文件夾,或者圖片很多的時候,就會很慢了。
•然后說一下可以改進的地方
◦引入JQuery,添加雙擊事件相應,實現(xiàn)雙擊刪除不想要的圖片
◦使用多線程的方式運行代碼,加快網(wǎng)頁的生成速度
最后,我想說的是,雖然這是個娛樂性質(zhì)的小東西,但是多發(fā)揮一下想象力,不斷地完善,對我們開發(fā)而言,一定會有幫助的。
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
python:解析requests返回的response(json格式)說明
這篇文章主要介紹了python:解析requests返回的response(json格式)說明,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-04-04