使用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()
我尝试通过增加“next”来让下一个网址显示出来,但这给我带来了错误。
next[x] = next[x+1].replace("jpg", "html")
IndexError: list index out of range
3 个回答
0
我搞定了。这里有个Python脚本,如果有人感兴趣的话。我只需要在“next”列表里再加一个项目。
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()
1
为了让你的代码更符合Python的风格,你可以用列表推导式来替代你对列表的操作:
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
x=len(next)
for name in next:
while x>0:
x=x-1
next[x] = next[x].replace("jpg", "html")
因为你在每次循环中并没有真正重置 x
的值,所以这可能不是你想要的效果。
接下来的循环也是这样:
for fname in page:
while x>0:
x=x-1
如果你的图片编号是 0:n-1
,那么创建链接的算法其实很简单:
对于图片
M
,链接到K
,其中K
:- 当
M
小于等于n-1
时,K
是M + 1
- 当
M
等于n-1
时,K
是0
(或者你自己决定的值)
- 当