LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
LinkBack Search this Thread
Old 01-29-2010, 12:52 AM   #1
TheIndependentAquarius
Senior Member
 
Registered: Dec 2008
Posts: 4,598
Blog Entries: 29

Rep: Reputation: 880Reputation: 880Reputation: 880Reputation: 880Reputation: 880Reputation: 880Reputation: 880
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 TheIndependentAquarius; 02-01-2010 at 10:42 PM.
 
Old 01-29-2010, 08:15 AM   #2
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,388
Blog Entries: 2

Rep: Reputation: 900Reputation: 900Reputation: 900Reputation: 900Reputation: 900Reputation: 900Reputation: 900Reputation: 900
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.

Last edited by theNbomr; 01-29-2010 at 09:06 AM.
 
Old 01-29-2010, 10:01 AM   #3
paulsm4
Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
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
 
1 members found this post helpful.
Old 02-01-2010, 03:15 AM   #4
TheIndependentAquarius
Senior Member
 
Registered: Dec 2008
Posts: 4,598
Blog Entries: 29

Original Poster
Rep: Reputation: 880Reputation: 880Reputation: 880Reputation: 880Reputation: 880Reputation: 880Reputation: 880
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..
 
Old 02-01-2010, 03:16 AM   #5
TheIndependentAquarius
Senior Member
 
Registered: Dec 2008
Posts: 4,598
Blog Entries: 29

Original Poster
Rep: Reputation: 880Reputation: 880Reputation: 880Reputation: 880Reputation: 880Reputation: 880Reputation: 880
@theNbomr

Thanks for replying, I shall study your advise in detail..
 
Old 02-01-2010, 11:47 AM   #6
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,388
Blog Entries: 2

Rep: Reputation: 900Reputation: 900Reputation: 900Reputation: 900Reputation: 900Reputation: 900Reputation: 900Reputation: 900
Quote:
Originally Posted by anishakaul View Post
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.
 
Old 02-01-2010, 11:51 AM   #7
Alien_Hominid
Senior Member
 
Registered: Oct 2005
Location: Lithuania
Distribution: Hybrid
Posts: 2,247

Rep: Reputation: 53
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.

Last edited by Alien_Hominid; 02-01-2010 at 11:57 AM.
 
Old 02-01-2010, 01:40 PM   #8
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,388
Blog Entries: 2

Rep: Reputation: 900Reputation: 900Reputation: 900Reputation: 900Reputation: 900Reputation: 900Reputation: 900Reputation: 900
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.
 
1 members found this post helpful.
Old 02-01-2010, 02:40 PM   #9
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 713Reputation: 713Reputation: 713Reputation: 713Reputation: 713Reputation: 713Reputation: 713
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

Last edited by MTK358; 02-01-2010 at 02:42 PM.
 
1 members found this post helpful.
Old 02-01-2010, 11:38 PM   #10
TheIndependentAquarius
Senior Member
 
Registered: Dec 2008
Posts: 4,598
Blog Entries: 29

Original Poster
Rep: Reputation: 880Reputation: 880Reputation: 880Reputation: 880Reputation: 880Reputation: 880Reputation: 880
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.

Last edited by TheIndependentAquarius; 02-02-2010 at 02:42 AM.
 
Old 02-02-2010, 07:27 AM   #11
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 713Reputation: 713Reputation: 713Reputation: 713Reputation: 713Reputation: 713Reputation: 713
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.
 
1 members found this post helpful.
Old 02-02-2010, 09:57 AM   #12
Alien_Hominid
Senior Member
 
Registered: Oct 2005
Location: Lithuania
Distribution: Hybrid
Posts: 2,247

Rep: Reputation: 53
with xgrab you can define what to grab, iirc
 
1 members found this post helpful.
Old 02-02-2010, 10:10 AM   #13
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,388
Blog Entries: 2

Rep: Reputation: 900Reputation: 900Reputation: 900Reputation: 900Reputation: 900Reputation: 900Reputation: 900Reputation: 900
Quote:
Originally Posted by MTK358 View Post
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.
 
1 members found this post helpful.
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Script to create simple html from text file the_mulletator Programming 5 10-01-2009 04:23 PM
import c header file into a python script deathalele Programming 1 05-26-2009 09:50 AM
Copy file by script python ! BongHongXanh Programming 2 01-11-2006 08:26 PM
Html file to Jpeg or Png? akihandyman Mandriva 2 10-02-2004 10:00 AM
converting .tif images to an animated .gif file with a script illiniguy3043 Programming 2 06-01-2004 03:11 PM


All times are GMT -5. The time now is 04:56 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration