Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic

Changing setpixel size using

by bladx (Chaplain)
on Aug 22, 2001 at 03:24 UTC ( #106819=perlquestion: print w/replies, xml ) Need Help??
bladx has asked for the wisdom of the Perl Monks concerning the following question:

I have been tweaking more of the tiny code snippet that I have been working on (thanks again to myocom for the original snippet.

Currently, it works fine, but I would like to know if there is a way I can make the pixel size a lot bigger or smaller. Can GD do this? I have read through the documentation at Lincoln Stein's site where the module comes from, but it doesn't seem to have any additional commands for the "setpixel" command.

Here is the current code snippet:
#!/usr/bin/perl -w use strict; use GD; my ($width, $height) = (300,50); my ($rvalue, $gvalue, $bvalue, $total) = (255,255,255,256); my $im = new GD::Image($width,$height); #Allocate the colors that are going to be used my @colors; for (0..255) { $colors[$_] = $im->colorAllocate(int(rand($rvalue)),int(rand($gvalue +)),int(rand($bvalue))); } # Create image according to height/width proportions and given amount +of colors for my $width_length (0..$width-1) { for my $height_length (0..$height-1) { $im->setPixel($width_length,$height_length,$colors[int(rand($t +otal))]); } } # Set binmode to STDOUT binmode STDOUT; # Set output to be an image for CGI print "Content-Type: image/png\n\n"; # Standard output of created image print $im->png;

Andy Summers

Replies are listed 'Best First'.
Re: Changing setpixel size using
by ChemBoy (Priest) on Aug 22, 2001 at 05:26 UTC

    Well, the reason you can't find any documentation on doing other things with setpixel is that it would make no sense: a pixel is the atomic element of an image, like a character for a text file*. To attempt to manipulate a "bigger pixel" would be like asking tr to translate digraphs or words: that's not what it's for, and it doesn't really make sense to try to do it that way.

    On the other hand, as the analogy suggests, there are mechanisms for dealing with larger blocks, in most well-designed schemes for manipulation: in the case of manipulating text in Perl, we have s///, and in the case of manipulating images in GD, we have filledRectangle($x1,$y1,$x2,$y2,$color) (among others).

    The following code is an attempt to do what you seem to want to do: <disclaimer type="weasly">it is absolutely, completely and totally untested.</disclaimer>

    my $rect_width = 10; my $rect_height = 10; for my $i (0 .. ($width - 1)/ $rect_width) { my $x_left = $i * $rect_width; my $x_right = $x_left + $rect_width - 1; $x_right = $width - 1 if $x_left >= $width; for my $j (0 .. ($height - 1) / $rect_height) { my $y_top = $j * $rect_height; my $y_bottom = $y_top + $rect_height - 1; $y_bottom = $height - 1 if $y_bottom >= $height; $im->filledRectangle($x_left,$y_top,$x_right,$y_bottom,$colors +[int(rand($total))]); } }

    Preliminary testing indicates that this works--let me know if it doesn't. Have fun!

    Update: as indicated, I got intimidated into testing (and finding the stupid bug, which is fortunate, isn't it?). Using the above as a drop-in replacement for the relevant lines of bladx's code, see the results at

    *Note that a pixel may have a varying amount of data behind it--between 1 and 32 bits, IIRC--just as a character may have 7 (ASCII) to 16 (Unicode) significant bits behind it.

    If God had meant us to fly, he would *never* have given us the railroads.
        --Michael Flanders

Re: Changing setpixel size using
by myocom (Deacon) on Aug 22, 2001 at 05:10 UTC

    You can't make the pixel size any larger ("pixels is pixels"), so instead, what you'll want to do is write a function that will color the pixel of choice and the pixels around it (to make, say, a 3x3 pixel "block"). Think of the square as a grid of pixels, and it's easy to figure out the coordinates of the surrounding pixels.

    Warning: Your function will need to deal with boundary conditions (e.g., what happens when you try to color all of the pixels that surround the pixel at 0,0?).

    "One word of warning: if you meet a bunch of Perl programmers on the bus or something, don't look them in the eye. They've been known to try to convert the young into Perl monks." - Frank Willison
Re: Changing setpixel size using
by the_mind_ (Novice) on Aug 22, 2001 at 09:26 UTC
    a pixel is a pixel is a pixel

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://106819]
Approved by root
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (5)
As of 2018-03-24 16:53 GMT
Find Nodes?
    Voting Booth?
    When I think of a mole I think of:

    Results (299 votes). Check out past polls.