不要太驚訝,你在這個項目的海上很少。它需要結合Python,HTML和可能的CSS。
「每個單獨的光譜都是從一個大型圖書館中提取的」 - 我將假設在這個答案中,您已經將感興趣的光譜拉入本地目錄,並希望在本地提供的網頁中查看這些光譜。我也將假設所有的png文件都是相同的大小。
如果這是正確的,你想要的是創建一個簡單的HTML文件,引用所有的PNG文件,把它們放在一個簡單的表格。要做到這一點,代碼需要cd到圖像文件的目錄中,打開一個名爲「index.html」的輸出文件(名稱很重要),使用glob獲取光譜圖像的所有名稱,然後遍歷爲頁面寫出html 的名稱。一旦你的文件中創建您可以使用命令行命令
python -m http.server 8000
在本地提供它然後,您可以在http://localhost:8000
指向瀏覽器中看到您的頁面。
這裏是一示例實現
from glob import glob
import os
def spectra_imgs(spectra_dir, sp_format):
'''Return an iterable with names of all the files in the spectra_dir
that match the name format sp_format.'''
for gl_name in glob(os.path.join(spectra_dir, sp_format)):
yield os.path.basename(gl_name)
def add_img(sp_img, side, outfile):
''' Add a table item to existing table.
sp_img is the filename of the image
side is "left" or "right"
outfile is an open file handle
Returns None
'''
if side == 'left':
outfile.write('<tr><td class="left_img"><img src="{0}" alt={0}></td>\n"'.format(sp_img))
elif side == 'right':
outfile.write('<td class="right_img"><img src="{0}" alt={0}></td></tr>\n"'.format(sp_img))
else:
raise ValueError("Side must be either left or right, not {0}".format(side))
def sides():
''' Return an iterator that supplies an infinite sequence of "left" and "right".'''
while True:
yield "left"
yield "right"
def write_prefix(outfile):
outfile.write(
'<!DOCTYPE html>\n'
'<html class="spectra_page" lang="en">\n'
'<head>\n'
'<meta charset="UTF-8"/>\n'
'<title>Spectra Comparison Page</title>\n'
'<style>\n'
'table { width: 100%; }\n'
'#left_img { align-items: center }\n'
'#right_img { align-items: center }\n'
'img { height:500px; width:500px }\n' # change to whatever size you want the images displayed at
'</style>\n'
'</head>\n'
'<body>\n')
def write_suffix(outfile):
outfile.write(
'</table>\n'
'</body>\n'
'</html>\n'
)
def write_spectra_page(spectra_dir):
''' Write an index.html file with all the spectra images shown. '''
with open(os.join(spectra_dir, "index.html")) as outfile:
write_prefix(outfile)
for side, sp_img in zip(sides, spectra_imgs(spectra_dir,'sp*.png'):
add_img(sp_img, side, outfile)
if side == "left":
# need to close the table row
outfile.write('</tr>\n')
write_suffix(outfile)
https://i.stack.imgur.com/dmOz5.png這裏是附件很抱歉。 – l1ve4science