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

2024/10/11 20:31:06

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.

example

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

Answer

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 globimport osdef spectra_imgs(spectra_dir, sp_format):'''Return an iterable with names of all the files in the spectra_dirthat 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 imageside is "left" or "right"outfile is an open file handleReturns 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 rowoutfile.write('</tr>\n')write_suffix(outfile)
https://en.xdnf.cn/q/118277.html

Related Q&A

Using peakutils - Detecting dull peaks

Im currently using peakutils to find peaks in some data. My data contains some "dull peaks", that is my peaks plateau somewhat. I cant set my code to find these peaks even after playing aroun…

nonlinear scaling image in figure axis matplotlib

enter image description hereI hope I have not over-looked as previously asked question. I dont think so. I have an image of a spectrum. I have several laser lines for calibration. Since the laser li…

How to correctly call a git submodule symlinked?

On the Sublime Text Package Control issue:Why ignore VCS-based packages accordingly to this message? I find out what causes this error. I had the package All Autocomplete triggering it. Then I gone to…

Python and MySQL query with quotes

With a script in Python3, after extracting some strings from a file, they should be used as data to be inserted into a MySQL database as follows:query1 = """INSERT INTO {:s} VALUES ({:s}…

Using scipy kmeans for cluster analysis

I want to understand scipy.cluster.vq.kmeans. Having a number of points distributed in 2D space, the problem is to group them into clusters. This problem came to my attention reading this question and …

Scrapy and celery `update_state`

I have the following setup (Docker):Celery linked to Flask setup which runs the Scrapy spider Flask setup (obviously) Flask setup gets request for Scrapy -> fire up worker to do some workNow I wish …

SPIDEV on raspberry pi for TI DAC8568 not behaving as expected

I have a Texas Instruments DAC8568 in their BOOST breakout board package. The DAC8568 is an 8 channel, 16bit DAC with SPI interface. The BOOST package has headers to connect it to my raspberry pi, an…

Tensorflow: Simple Linear Regression using CSV data

I am an extreme beginner at tensorflow, and i was tasked to do a simple linear regression using my csv data which contains 2 columns, Height & State of Charge(SoC), where both values are float. In …

How to resolve positional index error in python while solving a condition in python?

I have the following data and I am trying the following code: Name Sensex_index Start_Date End_Date AAA 0.5 20/08/2016 25/09/2016 AAA 0.8 26/08/2016 …

Google Calendar API: Insert multiple events (in Python)

I am using the Google Calendar API, and have successfully managed to insert a single event into an authorized primary calendar, but I would like to hard code multiple events that, when executed, would …