Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Random Pictures Using .pngs ... how?

by bladx (Chaplain)
on Aug 21, 2001 at 03:05 UTC ( [id://106421]=perlquestion: print w/replies, xml ) Need Help??

bladx has asked for the wisdom of the Perl Monks concerning the following question:

Monks:

Someone casually asked me the other day on the topic of Perl and .pngs. This person was wondering if it was possible to create random pixels of color in order to create pictures (at least muddled pictures,) within given height, and width dimensions (i.e. like 100x100 pixels, and so on.)

After being able to say that I don't know how one could do this, I did some searches on this topic using super search, but to no avail, did not find anything useful for what I would want to try out.

Basically, my question is: Is there a way to do this? (Create random pictures using random pixels in given dimensions.) If so, is there an easier way to do it using some sort of module as well?

But even if I figured out how to make these pictures with certain Perl code, I still don't know how to interact with my XWindows system on linux. Is there documentation I could read up on to find out how to interact with the linux XWindows environment?

Thanks for any help to either of the questions!

Andy Summers

Replies are listed 'Best First'.
Re: Random Pictures Using .pngs ... how?
by myocom (Deacon) on Aug 21, 2001 at 03:31 UTC

    GD can do this (and more). Here's a quick 'n' dirty example that will create a 100x100 pixel PNG and spew it to STDOUT (so make sure you redirect to a file in your shell).

    use strict; use GD; my $im = new GD::Image(100,100); my @colors; # Preallocate colors, since PNG has a limited number of co +lors for (0..255) { $colors[$_] = $im->colorAllocate(int(rand(256)),int(rand(256)),int(r +and(256))); } for my $x (0..99) { for my $y (0..99) { $im->setPixel($x,$y,$colors[int(rand(255))]); } } binmode STDOUT; print $im->png;
    "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
      Before your print add:
      print "Content-Type: image/png\n\n";
      and your script is now a CGI script that can be put behind a webserver. There is now no need for a temporary file!
      Thanks, myocom for the quick intro to the awesome GD module! It is a very useful module for me, and the documentation is very lengthy, however very useful.

      Thanks tilly for showing me how to make this useful for CGI, since I tend to program in CGI more often than the shell usually.

      I will be continuing to experiment with the GD module, and see if I can't get really good at utilizing this module for some really cool Perl artwork *hopefully*!

      Andy Summers
Re: Random Pictures Using .pngs ... how?
by koolade (Pilgrim) on Aug 21, 2001 at 08:38 UTC

    I was actually sitting on some code for this for a while, and your post was finally the inspiration for me to get to work and add it to Data::Random. I just uploaded the new version (0.03), so it might be a while before it hits all the mirrors. When you get it, take a look at the rand_image() function.

    Let me know if you have any feedback.

Interacting with X
by elbie (Curate) on Aug 21, 2001 at 16:57 UTC
    As for your Xwindows question, depends on what you're looking for. If you want a way on unix/perl to create graphical apps, look at the Tk module.

    If instead you are looking for a way to use X from your windows machine, there are a number of good applications. One of the commercial apps that I like is Exceed by Hummingbird. A nice free app is VNC, which is a little like PCAnywhere, but works on more than just Windows.

    elbieelbieelbie

Re: Random Pictures Using .pngs ... how?
by t'mo (Pilgrim) on Aug 21, 2001 at 17:06 UTC

    When I read this, I immediately thought of this node.

    Sometimes, this neural network that rides atop my shoulders is even better than SuperSearch...

Is there a way that I can use this new image in a web page?
by bladx (Chaplain) on Aug 21, 2001 at 08:58 UTC
    I have a new question about this.

    Is there a way that I can use this newly created .png picture in a Perl generated HTML file, as a regular image?

    If so, this would prove very useful for a variety of programming exercises I wish to perform eventually using this technique.

    Andy Summers

      There certainly is... all you need to do is put a link to the "image" into your html, e.g. <img src="random.pl" height="100" width="100" />.

      What I did on one of my servers is actually to tell Apache that all the *.png-files in a certain directory are to be handled as perl-scripts, so the user on the other end thinks s/he's getting real *.png images.

      Both methods in this post require your script to print "content-type: image/png\n\n"; before the actual image.

      Hope that helps
      BrotherAde

Re: Random Pictures Using .pngs ... how?
by count0 (Friar) on Aug 21, 2001 at 18:31 UTC
    Also, if you can get your hands on it, O'Reilly Programming Web Graphics (Shawn Wallace) not only goes into great explanations of GD, but also covers PerlMagick, gimp, postscript, etc.
Re: Random Pictures Using .pngs ... how?
by FoxtrotUniform (Prior) on Aug 21, 2001 at 21:13 UTC

    Since other people have talked about the mechanics of writing PNGs with perl, I figured I'd talk about the random-pixels bit (since that's fairly close to my area of specialization).

    If all you want is random, unrelated pixels, that's easy enough to do, but you'll probably want some way of restricting their intensity and color (to make it easy to generate a bunch of pixels that are all mostly red and fairly dark, for instance -- in my experience, once you have a random image generator with a bunch of knobs and widgets, you spend hours playing with it :-). You might also want to be able to set "gain" on the data -- higher gain would mean more pixels at high and low intensities, lower gain would mean most of the pixels end up at moderate intensities.

    A more interesting (to me) kind of random image data is Perlin (or coherent) noise. Well-implemented Perlin noise has a bunch of cool statistical properties, but at a glance it's just smooth noise. I'd rant about all the cool things you can do with it, but I have deadlines to attend to, so I'll leave you with a url: this page, by Hugo Elias.

    If you're really interested in this stuff, the canonical reference is Texturing and Modeling: A Procedural Approach, which is damn good.

    --
    :wq

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others taking refuge in the Monastery: (11)
As of 2024-03-28 09:07 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found