1: =pod
   2: Those of you that have been reading my occasional posts may have deduced by now that 
   3: I'm quite fond of ASCII art, judging by the JAPH at Node 31485 (just another JAPH) and
   4: my favoritest node so far at 31871 (perl in multiple dimensions).  Consequently, this
   5: node should come as no surprise to any of you.
   6: 
   7: This program takes 2 or more arguments on the command line.  The first argument is
   8: an ASCII (P3) PPM image file, the second argument is an output html file.  The
   9: subsequent arguments can be any random text files you have lying around, or none
  10: if you'd prefer.
  11: 
  12: It then translates the PPM image into an HTML one.  So you can build ASCII based
  13: pictures out of any graphics you have kicking around (so long as you convert them
  14: to PPM first).
  15: 
  16: Need an example?  Check out this:
  17: 
  18: http://members.home.net/jimandkoka/perl/jim_ascii.html
  19: 
  20: Self-portrait of the artist/programmer.  Be warned that it's big (~1.1 megs).  Rather
  21: post-modern, writing a program to generate a photo of the programmer using other
  22: programs.  The characters making up my picture are the source to my Mail::Bulkmail and
  23: Carp::Notify modules.  :)
  24: 
  25: And it would be easy enough to hook into GD and use that to pull data out of JPEGs, but
  26: I could do PPM files myself, and besides, nominally useless perl programs probably deserve
  27: at least some degree of obfuscation to them.
  28: 
  29: =cut
  30: 
  31: my $image  = shift @ARGV or die "No PPM image";
  32: my $output = shift @ARGV or die "No output file";
  33: my @text   = ();
  34: 
  35: my $bgcolor         = "333333";
  36: my $removal_regex   = "3.3.3.";
  37: 
  38: while (chomp($_ = <>)){
  39: 	s/\s+//g;
  40: 	tr/<>&/###/;
  41: 	push @text, split //;	#egad.  The files are slurped into memory.
  42: };							#I'll fix this later.  :*)
  43: 
  44: open (PPM, $image);
  45: open (IMAGE, ">$output");
  46: 
  47: print IMAGE <<"eHTML";
  48: <HTML>
  49: 	<HEAD>
  50: 		<TITLE>IMAGE</TITLE>
  51: 	</HEAD>
  52: 	
  53: 	<STYLE TYPE = "text/css">
  54: 		BODY {
  55: 			font-size:3pt;
  56: 			font-family:monospace
  57: 		}
  58: 	</STYLE>
  59: 	
  60: 	<BODY BGCOLOR = #$bgcolor>
  61: 	
  62: eHTML
  63: 
  64: my ($type, $width, $height, $colors) = ();
  65: 
  66: while (defined(chomp(my $line = <PPM>))){
  67: 	$line =~ s/\s*#.*//;
  68: 	my @line = split ' ', $line;
  69: 	if ($line){
  70: 		foreach ($type, $width, $height, $colors){
  71: 			$_ = shift @line if @line && ! defined $_;
  72: 		};
  73: 	};
  74: 	
  75: 	if (defined $colors){
  76: 		seek PPM, -(length($line) - length $colors), 1 if @line;
  77: 		last;
  78: 	};
  79: };
  80: 
  81: if ($type ne "P3"){die "Not an ASCII PPM file\n"};
  82: 
  83: my $num_triples = 0;
  84: while (chomp($_ = <PPM>)){
  85: 	next if /^\s*#/;
  86: 	my @triples = split ' ';
  87: 	while (my ($r, $g, $b) = 
  88: 			map {
  89: 				$_ = $type eq "P3" ? $_ : ord($_);
  90: 				$_ = int ($_ / $colors * 255);
  91: 				uc sprintf("%02lx", $_)
  92: 			}
  93: 			splice(@triples, 0, 3)
  94: 		){
  95: 		$num_triples++;
  96: 		if ($removal_regex && "$r$g$b" =~ /^$removal_regex$/){
  97: 			print IMAGE "&nbsp;";
  98: 		}
  99: 		else {		
 100: 			my $char = $text[$i++ % @text] or "#"; 
 101: 			print IMAGE "<FONT COLOR = '#$r$g$b'>$char</FONT>";
 102: 		};
 103: 	};
 104: 	print IMAGE "<BR>" unless $num_triples % $width;
 105: };
 106: 	
 107: print IMAGE "\n\t</BODY>\n\n</HTML>";
 108: 

Replies are listed 'Best First'.
RE: HTML-ized photos
by merlyn (Sage) on Oct 04, 2000 at 00:08 UTC
      Ever notice how that happens, these rash of things? I think it's a result of two things. One is that ideas have a birthpoint, where the technology becomes simple enough to do something like this. Another is that someone hears about the idea, and writes their own. It's not necessarily a re-invention, so much as a parallel invention.

      There are a number of examples of this through out history. Edison won the lightbulb patent (I think it was this one), because he filed his patent *1* hour earlier than the other guy. Anyone remember his name? Probably not (well, tilly probably does, but then he knows all that triva...).

      So, no, we don't need the 'Reinvented Wheel' section of the monastery. We need the 'Parallel Evolution Showcase' section.

      --Chris

      e-mail jcwren
        I did take a peek at that Image->Table converter posted by bastard, and it is remarkably similar to my own. 'course using GD instead of parsing it yourself is the easy way out. ;-)

        The thing that I find most comical is that I actually developed my HTML-izer for the very same reason that bastard did: to get around the restriction on posting images for low ranking monks. I decided that the files were prohibitively too large to actually do it, mind you.

        So I definitely think that "parallel evolution" is the appropriate name for it.

        Oh, and I'm pretty sure you're actually referring to Alexander Graham Bell and the telephone. There was another phone inventor whose name escapes me at the moment (the letter "Z" keeps occurring to me, though), who was just beaten to the patent office by Graham. Graham had, in fact, paid off the patent clerk to make sure that his phone was the won patented, and the rest is history.