Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked

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:


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?


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

Replies are listed 'Best First'.
Re: create PNG image
by zwon (Abbot) 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/ 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?

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

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (2)
As of 2021-04-18 16:51 GMT
Find Nodes?
    Voting Booth?

    No recent polls found