Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

create PNG image

by advix (Initiate)
on Sep 25, 2010 at 15:32 UTC ( #861967=perlquestion: print w/ replies, xml ) Need Help??
advix has asked for the wisdom of the Perl Monks concerning the following question:

Hi ALL,

I've got numbers from 0 to 1023 and had to convert the numbers to PNG (grayscale). I used Image::Magick but couldn't find any method to do that.

That is to say,

my @pixel; $pixel[0] = 100l; $pixel[1] = 1023; ......... ......... $pixel[10000] = 5;

I want these pixel values to be converted to grayscale image in PNG format;

Can anybody help me do that?

Thanks

20100927 Janitored by Corion: Added formatting, code tags, as per Writeup Formatting Tips

Comment on create PNG image
Download Code
Re: create PNG image
by zwon (Monsignor) on Sep 25, 2010 at 15:41 UTC
      I've tried SetPixel method but couldn't find how to write pixel by pixel in grayscale with 10-bit values(0~1023). Thanks @pixels = $image->GetPixel(x=>1,y=>1); $pixels[0]*=0.5; $image->SetPixel(x=>1,y=>1,color=>\@pixels); # this is color information $image->Quantize(colorspace=>'gray');

        Here's some example code:

        use strict; use warnings; use Image::Magick; my $image = Image::Magick->new( size => '1024x100', type => 'Greyscale', depth => 16, ); $image->Read('xc:white'); for ( 0 .. 1023 ) { my $color = sprintf "gray(%2.3f%%)", 100 * $_ / 1023; my $res = $image->Draw( primitive => 'line', stroke => $color, points => "$_,0 $_,100", ); warn $res if $res; } for ( 0 .. 16 ) { $image->SetPixel( x => int( (16 - $_) * 1023 / 16 ), y => 50, color => [ ( $_ / 16 ) x 3 ], ); } my $res = $image->Write( filename => "grey.png" ); warn $res if $res;

        Why are you ignoring Markup in the Monastery? Also there's bold warning below post text area: Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!

        Update: added SetPixel demo

Re: create PNG image
by ambrus (Abbot) on Sep 25, 2010 at 22:50 UTC

    Write the numbers in binary (pack to a two byte word per number) and convert the raw binary file to PNG using imagemagick such as convert -size 64x64 -depth 16 gray:a.raw a.png (command untested). Alternately, write a PGM (grayscale PNM) file (whether text or binary) and convert that using imagemagick.

Re: create PNG image
by tod222 (Pilgrim) on Sep 26, 2010 at 16:03 UTC
    From /usr/share/doc/perlmagick/examples/demo/single_pixels.pl on Ubuntu 10.04.1:
    #!/usr/bin/perl # # Methods for to Get and Set single pixels in images using PerlMagick # use strict; use Image::Magick; # read image my $im=Image::Magick->new(); $im->Read('logo:'); # --- # Get/Set a single pixel as a string my $skin=$im->Get('pixel[400,200]'); print "Get('pixel[x,y]') = ", $skin, "\n"; $im->Set('pixel[1,1]'=>'0,0,0,0'); $im->Set('pixel[2,1]'=>$skin); $im->Set('pixel[3,1]'=>'green'); $im->Set('pixel[4,1]'=>'rgb(255,0,255)'); # --- # More direct single pixel access my @pixel = $im->GetPixel( x=>400, y=>200 ); print "GetPixel() = ", "@pixel", "\n"; # modify the pixel values (as normalized floats) $pixel[0] = $pixel[0]/2; # darken red value $pixel[1] = 0.0; # junk green value $pixel[2] = 0.0; # junk blue value # write pixel to destination # (quantization and clipping happens here) $im->SetPixel(x=>5,y=>1,color=>\@pixel); # --- # crop, scale, display the changed pixels $im->Crop(geometry=>'7x3+0+0'); $im->Set(page=>'0x0+0+0'); $im->Scale('1000%'); # Output the changed pixels $im->Write('win:'); $im->Write('single_pixels.gif');

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://861967]
Approved by igelkott
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (9)
As of 2014-09-03 07:53 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite cookbook is:










    Results (35 votes), past polls