CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
How to save drawings on perl Tk::canvas into jpg png gif bmp tif file ?

 



kaiwang
New User

Jul 29, 2015, 2:24 PM

Post #1 of 6 (2794 views)
How to save drawings on perl Tk::canvas into jpg png gif bmp tif file ? Can't Post

Hi to all,

I use perl Tk::canvas module to draw. I want to save my drawings into png or jpg or gif or bmp or tif file, but I only find using canvas->postscript(-file=>'c:\canvas.ps') to save as ps file, how to save the drawing as other file format except ps format?

Thanks!


FishMonger
Veteran / Moderator

Jul 29, 2015, 2:46 PM

Post #2 of 6 (2792 views)
Re: [kaiwang] How to save drawings on perl Tk::canvas into jpg png gif bmp tif file ? [In reply to] Can't Post

I'm not sure if Tk::Canvas supports jpeg but try adding the -file_format attribute.

Code
$image->save (-file => $filename, -file_format => "jpeg")


If it doesn't support it, you may need to switch to using one of the other modules that does, such as Image::Base::Tk::Photo
http://search.cpan.org/~kryde/Image-Base-Tk-3/lib/Image/Base/Tk/Photo.pm


kaiwang
New User

Jul 31, 2015, 10:40 AM

Post #3 of 6 (2753 views)
Re: [FishMonger] How to save drawings on perl Tk::canvas into jpg png gif bmp tif file ? [In reply to] Can't Post

The Tk::canvas doesn't support saving drawing into other format except ps format.

And I have a try to use Tk::Photo module as following:

my $image = Image::Base::Tk::Photo->new (-for_widget =>$mw,
-width => 100,
-height => 100);
$image->rectangle (0,0, 99,99, 'white');
$image->xy (20,20, 'black');
$image->line (50,50, 70,70, '#FF00FF');
$image->line (50,70, 70,50, '#0000AAAA9999');
$image->save ('c:\filename.png');

unfortunately, the code ' $image->save ('c:\filename.png');' report "use of uninitilized value $format in lc at strawberry/perl/site/lib/Image/Base/Tk/Photo.pm line 150; Tk::Error: image file format "" is unknown at strawberry/perl/site/lib/Tk.pm line 251", so there is no output. How to fix this problem? and could you write me a perl demo by using Photo module like canvas module with drawing line,text,rectangle,oval, and then saving the drawings into png file?

Thanks!


(This post was edited by kaiwang on Jul 31, 2015, 10:41 AM)


FishMonger
Veteran / Moderator

Jul 31, 2015, 11:25 AM

Post #4 of 6 (2749 views)
Re: [kaiwang] How to save drawings on perl Tk::canvas into jpg png gif bmp tif file ? [In reply to] Can't Post

There is not $format var in the code you posted. Are you sure you executed the correct script?

Can you post the complete script so that I can test it to see if I get the same error?


FishMonger
Veteran / Moderator

Jul 31, 2015, 1:00 PM

Post #5 of 6 (2744 views)
Re: [kaiwang] How to save drawings on perl Tk::canvas into jpg png gif bmp tif file ? [In reply to] Can't Post

I just ran a test and it appears that the default file type is not defined, so you need to explicitly set it.

Add this just before the save command.

Code
$image->set(-file_format => 'PNG');


Without that set() statement, this is the error I get.

Quote
image file format "" is unknown



(This post was edited by FishMonger on Jul 31, 2015, 1:10 PM)


kaiwang
New User

Jul 31, 2015, 3:21 PM

Post #6 of 6 (2736 views)
Re: [FishMonger] How to save drawings on perl Tk::canvas into jpg png gif bmp tif file ? [In reply to] Can't Post

Yes! I add the $image->set( -file_format => 'png' ); before $image->save ('c:\filename.png'); It works!

Thanks!

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives