Python:从网站提取.png并输出到另一个网站 [英] Python: Pulling .png from a website, outputting to another

查看:85
本文介绍了Python:从网站提取.png并输出到另一个网站的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

希望这个问题不太模糊,或者要求太多.本质上,我正在分析大量光谱,并希望创建一个包含这些光谱的大型网页,而不是查看各个光谱.随附的示例显示了最终结果的外观.

Hoping this question is not too vague, or asking for too much. Essentially I am analyzing large amounts of spectra, and wanting to create one large webpage that contains these spectra rather than looking at individual spectra. Attached is an example of what the end result should look like.

那里的每个光谱都是从一个庞大的库中提取的.自从我编码以来已经有很长时间了,所以这仍然是一种学习经验.我设法创建了一个网页,并向前推了一个光谱.但是还没有把这两个放在一起.特别是没有几十万的规模.大概这是一个for循环的问题吗? 如果有人可以帮助您,那将是惊人的,请指明某个方向或模板.这应该很容易,但是我很努力. P.s.我现在的大部分工作是在蟒蛇蟒蛇皮中

Each individual spectra on there is pulled from a massive library. It has been a long time since I have coded, so this is still a learning experience. I have managed to create a webpage, and pull forward a single spectra. But have not put those two together. Especially not on the scale of hundred of thousands. Presumably this is a matter of a for loop? If someone could help that would be amazing, point in some direction, or a template. This should be very easy, yet I am struggling. P.s. Much of my work right now is in anaconda python

推荐答案

不足为奇的是,您对此项目一无所知.它需要结合使用Python,HTML以及可能的CSS.

Not too surprising you are little at sea with this project. It requires combining Python, HTML, and possibly CSS.

每个单独的光谱都是从庞大的库中提取的"-我将在此答案中假设您已将感兴趣的光谱提取到本地目录中,并希望在本地提供的网页中查看这些光谱.我还要假设所有png文件的大小都相同.

"Each individual spectra is pulled from a massive library" - I am going to assume in this answer that you have pulled the spectra of interest into a local directory and want to look at these in a web page served locally. I am also going to assume that all the png files are the same size.

如果这是正确的,那么您想要创建一个引用所有png文件的简单html文件,并将它们放置在一个简单表中.为此,代码需要与图像文件一起进入CD目录,打开一个名为"index.html"的输出文件(名称有意义),使用glob获取所有光谱图像的名称,然后遍历写出html的名字 用于页面.创建文件后,您可以使用命令行命令在本地提供文件

If this is correct, what you want is to create a file of simple html that references all the png files, laying them out in a simple table. To do this the code needs to cd into the directory with the image files, open up an output file named "index.html" (the name is significant), use glob to get all the names of the spectra images, and loop over the names writing out the html for the page. Once you have the file created you can serve it locally with the command line command

    python -m http.server 8000

然后您可以通过将浏览器指向http://localhost:8000来查看页面.

You can then see your page by pointing the browser at 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)

这篇关于Python:从网站提取.png并输出到另一个网站的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆