Converting a HTML file to a PNG file through Python script
Hi,
I have managed to create a HTML file inside python code, now can someone help me to convert this to a PNG file through a Python script ?? EDIT: Details added__________________________________ I have a python script which generates map-legends in the form of an html file. The legend generated have to be pasted on a map which is in a png format. A png format file can be pasted on another png format file easily. But because the legends generated are in a html format I cannot paste it on the map file !! EDIT: Details added__________________________________ I did Googling first but it resulted in various soft wares for above purpose which I don't want !! I am not asking for direct solutions, but please give directions if you can !! |
Your question could be interpreted two ways: you want to render your HTML page in the same way as a browser would, or you want the HTML code to be displayed. I will assume the former.
First, consider what goes on to render an HTML file into a human-viewable screen image. A program has to read and interpret the HTML, download any inline links to images and the like, and then render the results in a window. Many details of all of this are left out, and some possible trip-ups like CSS and Javascript complicate matters. These are all activities performed by a web browser, so it sounds like you will have to write a web browser, or use an existing one by modifying the code to do what you want. Or, maybe not. You could use an existing browser to render your page(s), and then simply grab a screen-shot of the result. If you are trying to do everything from some kind of script or program, then this will be a substandard solution, as it probably requires human interaction. Another possible solution would be to use a conventional browser to do the rendering, and then have a script that can locate the X window on which the page is rendered by parsing the output of xlsclients -al + xwininfo -id xxxx, and take a snapshot of the window using xwd, and finally convert the image to PNG format using the ImageMagick convert tool. A bit of a kludge but it can work (been there, done that). An easier way might be to get the conventional browser to render to a virtual X server, like Xvfb, and then simply use convert to translate the xwd file upon which the browser is rendered. These latter methods will involve a lot of scripting and orchestrating of existing components, but it can be done. A Google search for 'xvfb screenshot' yields pointers to numerous articles outlining this method. Good luck. --- rod. |
Nbomr - you're being too charitable.
My response is "WTF?" anishakaul - What exactly do you want to do? *NOT* "how" (don't think about "implementation details" like Python, HTML or PNG). But "what" do you want to do? What exactly are you starting out with? And what - *conceptually* - do you want to accomplish? Thanx in advance .. PSM |
Quote:
Details: I have a python script which generates map-legends in the form of an html file. The legend generated have to be pasted on a map which is in a png format. A png format file can be pasted on another png format file easily. But because the legends generated are in a html format I cannot paste it on the map file !! Kindly guide.. |
@theNbomr
Thanks for replying, I shall study your advise in detail.. |
Quote:
--- rod. |
You need to render html first. I suggest you to use python os.* to start the browser with your page and then xgrab screen into the image.
|
With GD you can programatically draw arbitrary graphic elements, including text, on an image, and then save the image as a PNG, or other image file format.
--- rod. |
It seems like a better idea to make the code generate a PNG in the first place, not generate HTML and then somehow render it to PNG.
I would recommend the "cairo" vector graphics lib. It is capable of drawing to PNG files, X Windows, and much more. http://www.cairographics.org/ http://www.tortall.net/mu/wiki/CairoTutorial |
Quote:
I shall consider GD as one more alternative for achieving the task. Quote:
I failed to mention in my first post that I am using Mapnik to render maps and I need to display legends on those maps. Mapnik doesn't support legends currently, that's why I posted this question.. Yesterday I mailed to mailing lists of Mapnik and the main person there also said the same thing w.r.t Cairo and I found this link too http://trac.mapnik.org/wiki/MapnikRenderers Quote:
|
I don't like the idea of using a browser, because the screenshot will contain the user interface and sfuff, it will depend on the user having a certain browser, and all the bloat cause by loading the browser's UI for nothing. It just seems very kludgy compared to simply creating an image in the first place.
|
with xgrab you can define what to grab, iirc
|
Quote:
--- rod. |
All times are GMT -5. The time now is 02:30 PM. |