2017-07-19 49 views
0

希望这个问题不是太模糊,或要求太多。本质上,我正在分析大量的光谱,并希望创建一个包含这些光谱的大网页,而不是查看单个光谱。附件是最终结果应该是什么样子的例子。Python:从网站拉.png,输出到另一个

example

在那里每个单独的光谱是由一个巨大的图书馆拉动。自从我编写代码已经很长时间了,所以这仍然是一种学习体验。我设法创建了一个网页,并提出了一个单一的光谱。但是没有把这两个放在一起。尤其不在数十万的规模上。据推测这是一个for循环的问题? 如果有人能够提供帮助,那将是惊人的,指向某个方向或模板。这应该很容易,但我很挣扎。 P.s.我现在的大部分工作是在蟒蛇蟒

+0

https://i.stack.imgur.com/dmOz5.png这里是附件很抱歉。 – l1ve4science

回答

0

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

他们都从这个https://data.sdss.org/sas/dr13/eboss/spectro/redux/images/v5_9_0/v5_9_0/4381-55824/ – l1ve4science

+0

拉而且,所有图像是不同的大小 – l1ve4science

+0

'img 'CSS中的标签将使图像全部显示在相同的大小,但它可能会拉伸一些,以使它们适合。您可以修改''标签,以便在每次启动页面时浏览器都会将它们加载到互联网上,而不是从本地存储加载图像。这可能会很慢。如果您的计算机上没有足够的存储空间,我只会选择该方法。 – verisimilidude

相关问题