LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Converting a HTML file to a PNG file through Python script (https://www.linuxquestions.org/questions/programming-9/converting-a-html-file-to-a-png-file-through-python-script-785530/)

Aquarius_Girl 01-29-2010 12:52 AM

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 !!

theNbomr 01-29-2010 08:15 AM

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.

paulsm4 01-29-2010 10:01 AM

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

Aquarius_Girl 02-01-2010 03:15 AM

Quote:

Originally Posted by paulsm4
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

First of all, what is PSM ?

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

Aquarius_Girl 02-01-2010 03:16 AM

@theNbomr

Thanks for replying, I shall study your advise in detail..

theNbomr 02-01-2010 11:47 AM

Quote:

Originally Posted by anishakaul (Post 3848013)
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 !!

Okay, now we know what you are actually trying to do. You need to look at something like libgd which allows you to generate your PNG format files directly with PHP or other programming languages.
--- rod.

Alien_Hominid 02-01-2010 11:51 AM

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.

theNbomr 02-01-2010 01:40 PM

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.

MTK358 02-01-2010 02:40 PM

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

Aquarius_Girl 02-01-2010 11:38 PM

Quote:

Originally Posted by theNbomr
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.

Thanks for replying :)
I shall consider GD as one more alternative for achieving the task.

Quote:

Originally Posted by MTK358
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.

Thanks for replying :)

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:

Originally Posted by Alien_Hominid
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.

Thanks for replying! But I failed to properly understand whatever you said, probably due to my lack of in depth python knowledge.

MTK358 02-02-2010 07:27 AM

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.

Alien_Hominid 02-02-2010 09:57 AM

with xgrab you can define what to grab, iirc

theNbomr 02-02-2010 10:10 AM

Quote:

Originally Posted by MTK358 (Post 3849488)
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.

You are correct that it is a kludgey arrangement. For that reason, your procedure of generating HTML as an intermediate step in the process is sub-optimal. Use one of the graphics toolkits mentioned (and there are others, too), that allow you to generate your image directly.

--- rod.


All times are GMT -5. The time now is 02:30 PM.