我是一名新手程序员,并认为使用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()
我尝试通过递增“下一个”来显示下一个网址,但它给了我一个错误。
next[x] = next[x+1].replace("jpg", "html")
IndexError: list index out of range
答案 0 :(得分:1)
x=len(next)
for name in next:
while x>0:
x=x-1
next[x] = next[x].replace("jpg", "html")
由于您并未在x
的迭代之间重置for
,因此这可能与您的预期无关。
以下列循环开头的相同:
for fname in page:
while x>0:
x=x-1
如果您的图片编号为0:n-1
,则创建链接的算法非常简单:
图片M
链接到K
其中K
:
M + 1
只要M <= n-1
0
M == n-1
(或您自己决定)
答案 1 :(得分:1)
更加pythonic,您可以通过列表理解来替换列表操作:
index=["".join(['<a href="./', item.replace("jpg", "html"), '">', '<img src="./Thumbs/', item, '" />', '</a>']) for item in 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>'
答案 2 :(得分:0)
让它发挥作用。如果有人感兴趣的话,这是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("gallery.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 {font-size:small;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('h1 {text-align:center;}' + '\n')
file.write('a:link {color: grey; text-decoration: none;}' + '\n')
file.write('a:visited {color: grey; text-decoration: none;}' + '\n')
file.write('a:active {color: grey; text-decoration: none;}' + '\n')
file.write('a:hover {color: grey;text-decoration: underline;}' + '\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')
image=os.listdir('./Images')
page=os.listdir('./Images')
next.append('gallery.html')
x=len(next)
y=len(page)
z=len(image)
for fname in page:
while y>0:
y=y-1
x=x-1
z=z-1
page[y] = page[y].replace("jpg", "html")
file = open(page[y], 'w')
file.write('<html>' + '\n')
file.write('<title>' + title + '</title>' + '\n')
file.write('<head>' + '\n')
file.write('<script type="text/javascript">function delayer(){window.location = "./' + next[x].replace("jpg", "html") +'"}</script>' + '\n')
file.write('<style>' + '\n')
file.write('body {font-size:small;text-align:center;background-color:black;font-family:"Lucida Grande",Verdana,Arial,Sans-Serif;color: white;}' + '\n')
file.write('a:link {color: white; text-decoration: none;}' + '\n')
file.write('a:visited {color: white; text-decoration: none;}' + '\n')
file.write('a:active {color: white; text-decoration: none;}' + '\n')
file.write('a:hover {color: white;text-decoration: underline;}' + '\n')
file.write('</style>' + '\n')
file.write('</head>' + '\n')
file.write('<body onLoad="setTimeout(\'delayer()\', 3000)">' + '\n')
file.write('<p><a href="gallery.html">' + title + '</a></p>' + '\n')
file.write('<a href="./' + next[x].replace("jpg", "html") + '">' + '<img height="90%" src="./Images/' + image[z] + '" />' + '</a>')
file.write('</body>' + '\n')
file.write('</html>')
file.close()