不知道大家有沒有這樣的體驗(yàn),windows電腦上查看一張gif圖,默認(rèn)就把IE給打開了,還彈出個(gè)什么詢問項(xiàng),好麻煩的感覺。所以為了解決自己的這個(gè)問題,寫了個(gè)簡單的文件夾內(nèi)圖片瀏覽工具。
效果圖
以E盤某一文件夾為例
效果圖
實(shí)現(xiàn)思路
業(yè)務(wù)代碼
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
|
# coding:utf-8 import sys reload (sys) sys.setdefaultencoding( 'utf8' ) # __author__ = '郭 璞' # __date__ = '2016/8/5' # __Desc__ = 自動(dòng)生成網(wǎng)頁相冊 import os # 呵呵了,原來有標(biāo)準(zhǔn)庫中的walk方法。那么這個(gè)方法就獲得一個(gè)文件夾下的圖片文件吧 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é)尾的文件,并補(bǔ)全路徑保存到列表中輸出 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" href="http://csshake.surge.sh/csshake-slow.min.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é)
•首先說一下缺點(diǎn):
?缺點(diǎn)很明顯,對于中文支持的不夠好,因?yàn)椴榭磮D片大圖的時(shí)候是以超鏈接的形式出現(xiàn)的,所以會(huì)發(fā)生亂碼的情況。
?然后是優(yōu)點(diǎn):
優(yōu)點(diǎn)不是很明顯,因?yàn)槿绻粋€(gè)文件夾下面有很多的子文件夾,或者圖片很多的時(shí)候,就會(huì)很慢了。
•然后說一下可以改進(jìn)的地方
?引入JQuery,添加雙擊事件相應(yīng),實(shí)現(xiàn)雙擊刪除不想要的圖片
?使用多線程的方式運(yùn)行代碼,加快網(wǎng)頁的生成速度
最后,我想說的是,雖然這是個(gè)娛樂性質(zhì)的小東西,但是多發(fā)揮一下想象力,不斷地完善,對我們開發(fā)而言,一定會(huì)有幫助的。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持服務(wù)器之家。
原文鏈接:http://blog.csdn.net/marksinoberg/article/details/52131417