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

Fellow Monks,

some time ago I got one of those programs that convert picture files to ASCII art files, I thought it's great.. and then never got to use it again... Now I need to convert some pictures to that ASCII art, and I figured they would look nicer in colors...

so I tried a test with a picture of my girlfriend; I scanned the picture, converted it with this program, put it in an html file and manually (sort of manually, I used Dreamweaver to select and create font tags quickly) crafted the colors, the results looked nice, and you can check them out yourselves here...

My aim is a Web application, where people enter the site, upload their picture, and have the script convert it to them as a colored HTML file..

Now I need to know, is there a utiliy to do that in Perl?? I tried to use Image::Magick but I can't seem to compile it correctly on my machine... and I can't access the server to install it...

so any thoughts about that??

I have an idea about the process, but don't know how to do it - if it is right that is...

any ideas??


He who asks will be a fool for five minutes, but he who doesn't ask will remain a fool for life.

Chady | http://chady.net/

Replies are listed 'Best First'.
Re: Colored ASCII Art from Pictures
by bastard (Hermit) on May 02, 2001 at 03:24 UTC
    Here are a few threads that cover similar topics.
    Pixel by Pixel reading of images
    Image to table converter (i did this one)
    one of merlyns webtechniques columns

    I like using the GD module, and from what I hear it's much faster than Image::Magick.

    What I might do to implement this:
    read up the image file
    GD.pm
    create a matrix of the pixels and color intensities
    Build a sub that returns the intensity based on the rgb.
    define some characters that would work as shades of gray intesities
    trial and error, perhaps analysing a single font of characters assigning a value based on positive coverage in a fixed area (fixed width fonts only)
    this would be worked out ahead of time and set into a file or array for the generation of the final product
    match neighbour colors inside a certain range as one color re-formatting the matrix
    Build a sub to do that, run the foreach loops for x and y of the image like thus (x = 2; x < size; x = x + 3) if a 3x3 sample size was desired
    another thing might be to look into the available modules and see if they support resizing, then do that first (or you could always require that the input file be sized already)
    print out the html file based on the matrix.
    Lots 'o print statements. I'd probably be printing to standard out during the x and y iterations of the pixels, converting each pixel and printing one line at a time.

    Hope it helps.

Re: Colored ASCII Art from Pictures
by dmckee (Scribe) on May 01, 2001 at 23:30 UTC
    When I was creating the first version of img2gif I used 'RAW' file format, exported from Paint Shop Pro, which looks like this:


    255 255 255
    128 128 128
    64 64 64
    0 0 0

    Which could be a 1x4, 4x1 or 2x2 image... it doesn't save the dimensions. That'd save messing about with parsing JPGs etc... but's useless on a website. Or you could try to use GD, which I found less useful than Image::Magick... only use PNGs, I think...

    Maybe you could try to pull the relevant parts of Image::Magick inline into your program? (Is this generally possible?)

    Just my .02 pence worth... ;-)

    Dave

    Eight years involved with the nuclear industry have taught me that when nothing can possible go wrong and every avenue has been covered, then is the time to buy a house on the next continent. Terry Pratchett

(tye)Re: Colored ASCII Art from Pictures
by tye (Sage) on May 01, 2001 at 23:53 UTC

    Since it is ASCII art, I'd consider writing out HTML.

    If you want to set background color per character, then use <table>. Otherwise, just use <pre> and <font>. Of course, this will not be a very compact representation (so be sure to avoid using a separate font tag for each letter when possible). (:

            - tye (but my friends call me "Tye")
      http://www.wiersma.org/Projects/pic2text/
      I tried this hunk of code out at one time and the results are what you are looking for. The problem is that this particular script translates each pixel into a colored ascii character. You would want to use Image::Magick to reduce the resolution of the picture to manageable dimensions for representation as ascii in an html page (so that you have a picture that fits into a browser window).
      It seems like a good place to start.
      Good luck
Re: Re: Colored ASCII Art from Pictures
by dooberwah (Pilgrim) on May 02, 2001 at 05:06 UTC
    Last post got messed up, here's what I ment to say: I'm not exactly sure what your method of reading the image in is (I've never done any images in Perl yet) but it seemed to me that the color should be encoded in hex in the image file. If this is true you can just have the Perl script generate a whole ton of really ugly HTML with. Just have the perl script put <font color="#color's_hex_value">. I'm pretty sure that will work.

    Just the ideas off the top of my head ...

Re: Colored ASCII Art from Pictures
by strredwolf (Chaplain) on May 03, 2001 at 00:12 UTC
    ImageMagic (with it's Perl Extention that auto-builds as you compile it) requires X Windows. Unfortunately, most servers don't have X Windows. :P

    --
    $Stalag99{"URL"}="http://stalag99.keenspace.com";