不要太惊讶,你在这个项目的海上很少。它需要结合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