Python: Pulling .png from a website, outputting to another

霸气de小男生 提交于 2019-12-12 05:44:42

问题


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.

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


回答1:


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

"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.

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

You can then see your page by pointing the browser at http://localhost:8000.

Here is an example implementation

    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://stackoverflow.com/questions/45202265/python-pulling-png-from-a-website-outputting-to-another

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!