Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Image to table converter

by bastard (Hermit)
on Sep 30, 2000 at 08:11 UTC ( #34725=sourcecode: print w/ replies, xml ) Need Help??

Category: HTML Utility
Author/Contact Info bastard
Description: I hacked this thing together during my quest to get around the "no images on the home node under level 5" rule. (yes i know there are other ways) I'm not sure how useful it is, but since someone requested it i'll post it here in case anyone else is interested. (I suppose the code could also provide a simple example of the use of the GD image module.)

What does it do you may ask? Basically it converts an image to a relatively optimized table representation of the image. It accepts one parameter which is the image file you are going to convert. It dumps the table to STDOUT. It can accept the following image types: PNG, JPEG, XPM and GD2

Warning, this will create very large and complex tables. I have created a 120k table from 6k PNG image, so this thing is not appropriate for larger images. (before the COLSPAN enhancements it could generate tables many times larger)

#!/usr/bin/perl -w

use strict;
use GD;        # this lib is so cool

my $imagename = shift;

# get the user specified image
my $image = GD::Image->new($imagename) || die "Could not open image: $
+imagename\n";

# get the image size
my ($width, $height) = $image->getBounds();

# build the seed color...
my $index = $image->getPixel(0, 0);
my ($r,$g,$b) = $image->rgb($index);

# now format it as 2 digit hex
my $color = sprintf "%02x%02x%02x", $r, $g, $b;

my $lastcolor = "";
my $count;

# the stylesheet is necessary to make the end result acceptably small
my $results = "<STYLE>.sm { font-size: 2px; font-family: sans-serif }<
+/STYLE>\n";

$results .= "<TABLE BORDER=0 CELLPADDING=0 CELLSPACING=0 WIDTH=$width 
+HEIGHT=$height>";

# loop through the pixels
for (my $x = 0; $x < $height; $x++) {
        $count = 0;
        $results .= "<TR>";
        for (my $y = 0; $y < $width; $y++) {
                $count++;

                # get the color of the current pixel
                $index = $image->getPixel($y, $x);
                ($r,$g,$b) = $image->rgb($index);
                $color = sprintf "%02x%02x%02x", $r, $g, $b;

                # we use this to build a COLSPAN length
                # this greatly enhances efficiency
                if ($color ne $lastcolor) {
                        $count = $count * 2;
                        $results .= "<TD CLASS=sm BGCOLOR=\"$lastcolor
+\" COLSPAN=$count>_</TD>";
                        $count = 0;
                }

                $lastcolor = $color;
        }
        $count = $count * 2;
        $results .= "<TD CLASS=sm BGCOLOR=\"$lastcolor\" COLSPAN=$coun
+t>_</TD>";
        $results .= "</TR>\n";
}
$results .= "</TABLE>";

print "$results\n";

Comment on Image to table converter
Download Code
RE: Image to table converter
by merlyn (Sage) on Sep 30, 2000 at 08:19 UTC
RE: Image to table converter
by AgentM (Curate) on Sep 30, 2000 at 21:59 UTC
    Bah! All you need is a form with an image submit button. <code> <form> <input type="image" src="http://gifurl"> </form> <code>
    AgentM Systems or Nasca Enterprises is not responsible for the comments made by AgentM- anywhere.
      Perhaps you didn't read the statement in the description:
      (yes i know there are other ways)

      Of course there are other ways, I know of a handful more than the very basic one you mentioned. That's not the point. I was getting around the rule not the filtering. Technically I don't have an image on my home node, it just looks like one.

Re: Image to table converter
by jerrygarciuh (Curate) on Sep 15, 2002 at 18:20 UTC
    Bastard,
    Thanks for posting this. One comment though...the GD::Image->new() method expects numeric dimensions as args for a brand new image. Your code worked nicely when I changed line 9 to

    my $image = GD::Image->newFromJpeg($imagename) || die "Could not open image: $imagename\n";

    HTH
    jg
    _____________________________________________________
    "The man who grasps principles can successfully select his own methods.
    The man who tries methods, ignoring principles, is sure to have trouble.
    ~ Ralph Waldo Emerson

Back to Code Catacombs

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (7)
As of 2014-07-13 23:37 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    When choosing user names for websites, I prefer to use:








    Results (252 votes), past polls