使用 Python 创建照片库

发布于 2024-11-03 22:54:19 字数 2394 浏览 3 评论 0原文

我是一名新手程序员,我认为使用 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

I'm a novice programmer and thought it would be a fun learning experience to create a photo gallery using python. I've gotten pretty far in the project, but recently got stuck.

I have a folder filled with photos. I was able to generate an index page with thumbnails. When I click on a thumbnail, a larger version appears. However, when someone clicks the larger version, I'd like it to go to the next photo. Right now, the user has to click back to the index page to get to the next photo. Here's the index page with working thumbnails.

http://dl.dropbox.com/u/26085098/CCC%20Culinary%20Food%20and%20Wine%20Event%202011/index.html

The python script I used to create the gallery is shown below.

I would love if someone could point me in the right direction. Also, any suggestions on making my code more elegant would be greatly appreciated.

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()

I tried making the next url show up by incrementing "next", but it gives me an error.

next[x] = next[x+1].replace("jpg", "html")
IndexError: list index out of range

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

司马昭之心 2024-11-10 22:54:19
x=len(next)

for name in next:
    while x>0:
        x=x-1
        next[x] = next[x].replace("jpg", "html")

由于您并未真正在 for 迭代之间重置 x,因此这可能不会达到您的预期。

下一个循环同样如此:

for fname in page:
    while x>0:
        x=x-1

如果您的图像编号为 0:n-1,则创建链接的算法非常简单:

  • 对于图像 M 链接到 K 其中 K

    • M + 1 只要 M <= n-1
    • M == n-1时,是0(或者它本身,由你决定)
x=len(next)

for name in next:
    while x>0:
        x=x-1
        next[x] = next[x].replace("jpg", "html")

Since you aren't really resetting x between iterations of for, this probably doesn't do what you intended.

Same for the next loop starting with:

for fname in page:
    while x>0:
        x=x-1

If your images are numbered 0:n-1, the algorithm to create the links is quite simple:

  • For image M link to K where K:

    • Is M + 1 as long as M <= n-1
    • Is 0 (or itself, as you decide) when M == n-1
静谧幽蓝 2024-11-10 22:54:19

为了更加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>'

to be more pythonic, you can replace your list manipulations by list comprehension :

index=["".join(['<a href="./', item.replace("jpg", "html"), '">', '<img src="./Thumbs/', item, '" />', '</a>']) for item in os.listdir('./Images')]

instead of

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>'
千里故人稀 2024-11-10 22:54:19

让它工作了。如果有人感兴趣的话,这是 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()

Got it to work. Here's the python script if anyone's interested. I just had to append another item to the "next" list.

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()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文