[SOLVED] Converting a HTML file to a PNG file through Python script
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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 !!
Last edited by Aquarius_Girl; 02-01-2010 at 10:42 PM.
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.
*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 !!
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.
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.
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.
Last edited by Aquarius_Girl; 02-02-2010 at 02:42 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.
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.