我是一个新手程序员,并认为使用python创建照片库将是一种有趣的学习体验。我在项目中已经做得很好,但最近却陷入困境。使用Python创建照片库
我有一个文件夹充满照片。我能够用缩略图生成索引页面。当我点击一个缩略图时,会出现一个更大的版本。但是,当有人点击较大的版本时,我希望它能够转到下一张照片。现在,用户必须点击回到索引页才能看到下一张照片。这里是带有工作缩略图的索引页面。
http://dl.dropbox.com/u/26085098/CCC%20Culinary%20Food%20and%20Wine%20Event%202011/index.html
我用来创建画廊的Python脚本如下所示。
我会爱如果有人能指出我在正确的方向。此外,任何建议让我的代码更优雅,将不胜感激。
import os
index=os.listdir('./Images')
x=len(index)
for fname in index:
while x>0:
x=x-1
index[x] = '<a href="./' + index[x].replace("jpg", "html") + '">' + '<img src="./Thumbs/' + index[x] + '" />' + '</a>'
listString='\n'.join(index)
title=os.getcwd()
title=title.split("/")
title=title.pop()
file = open("index.html", 'w')
file.write('<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"' + '\n')
file.write(' "http://www.w3.org/TR/html4/loose.dtd">' + '\n')
file.write('<html>' + '\n')
file.write('<title>' + title + '</title>' + '\n')
file.write('<head>' + '\n')
file.write('<style>' + '\n')
file.write('body {padding:10px;background-color:black;margin-left:15%;margin-right:15%;font-family:"Lucida Grande",Verdana,Arial,Sans-Serif;color: white;}' + '\n')
file.write('img {border-style:solid;border-width:5px;border-color:white;}' + '\n')
file.write('</style>' + '\n')
file.write('</head>' + '\n')
file.write('<body>' + '\n')
file.write('<h1>' + title + '</h1>' + '\n')
file.write(listString + '\n')
file.write('</body>' + '\n')
file.write('</html>')
file.close()
next=os.listdir('./Images')
x=len(next)
for name in next:
while x>0:
x=x-1
next[x] = next[x].replace("jpg", "html")
image=os.listdir('./Images')
page=os.listdir('./Images')
x=len(page)
for fname in page:
while x>0:
x=x-1
page[x] = page[x].replace("jpg", "html")
file = open(page[x], 'w')
file.write('<a href="./' + next[x] + '">' + '<img height="95%" src="./Images/' + image[x] + '" />' + '</a>')
file.close()
我试着通过递增“下一个”使下一个URL显示,但它给了我一个错误。
next[x] = next[x+1].replace("jpg", "html")
IndexError: list index out of range
谢谢Eli。我能够修复它。 – 2011-04-27 05:21:29