Settings

The page will reload to apply your changes.
Theme

ASCII Art Font

ASCII Art Menu

Original Usenet message from alt.ascii-art, 18 Oct 2006.
Re: img->ascii

Re: img->ascii

In alt.ascii-art, Troy Piggins  <use...@piggo.com> wrote:
> * Eli the Bearded wrote:
> > Actually, now I have a colorizer that I use with it, too. Example
> > usage:
> >
> > djpeg IMAGE.jpg > IMAGE.ppm
> > pnmscale -yscale 0.25 -xscale 0.5 IMAGE.ppm > IMAGE-scaled.ppm
> > ppmtopgm IMAGE-scaled.ppm > IMAGE-scaled.pgm
> > # sometimes a pnminvert of the pgm file will help a lot
> > pgmtoascii IMAGE-scaled.pgm > IMAGE-scaled.txt
> > colorizeascii IMAGE-scaled.txt IMAGE-scaled.ppm > IMAGE-scaled.html
> 
> This line I got an error:
> 
> $ colorizeascii image.txt image-scaled.ppm > image.html
> colorizeascii: text error image.txt: not enough characters

Ah. That's right. The pgmtoascii previously posted has a bug that
drops the last few letters of the last line, most of the time.
Updated version at the end of this post.

> So close yet so far...

The colorizer doesn't need ASCII art for the text input. So if
you want to create stuff like the linux kernel colorized to show
Tux, you can do that. 

This stuff makes mod_gzip (or the like) a good idea.

Elijah
------
#!/usr/bin/perl -w
# Convert PGM to ASCII art.
# B. Elijah Griffin / Eli the Bearded	27 Dec 1999
use strict;
use integer;
use vars qw( $in $type $x $y $max $id %map $custommap $i $last );

$id = $0;
$id =~ s:.*/::;

$custommap = 0;
%map = (
 # The keys are the letter 'd' followed by a decimal number of a character.
 # Alternatively the raw character can be used, but the 'd' form will
 # override if both are defined.
# d000 => ' ',	# 00
# d051 => '.',	# 33
# d102 => ':',	# 66
# d153 => 'I',	# 99
# d204 => 'V',	# cc
# d255 => 'M',	# ff
  d000 => ' ',
  d016 => '.',
  d032 => ',',
  d048 => ';',
  d064 => '+',
  d080 => 'x',
  d096 => 'z',
  d112 => 'm',
  d128 => 'X',
  d144 => 'Y',
  d160 => 'U',
  d186 => 'C',
  d202 => 'O',
  d218 => 'M',
  d234 => 'W',
  d256 => '%',
);

# Fill in holes.
$last = ' ';
for ($i = 0; $i < 256; $i++) {
  $i = sprintf("%03d", $i);
  if (defined($map{"d$i"})) {
    $last = $map{"d$i"};
  } else {
    $map{"d$i"} = $last;
  }
}

while (defined($ARGV[0]) and $ARGV[0] =~ /^-(.*)/) {
  my $arg = $1; shift;
  if ($arg eq '-help' or $arg eq '-version' or $arg eq 'v') {
    &usage(0);
  } elsif ($arg eq 'm' or $arg eq '-map') {
    &readmap($ARGV[1]);
  }
}

# Add to map the character versions of the numerical mappings.
my @map = keys %map;
foreach (@map) {
  s/^d//;
  $map{chr($_)} = $map{"d$_"};
}

if ($ARGV[0]) {
  open(STDIN,"< $ARGV[0]") or die "$id: Can't open '$ARGV[0]': $!\n";
}

$in = '#';
while($in =~ /^#/) { $in = <STDIN> }

if ($in =~ s/^P([25])\s*//) {
  $type = $1;
} else {
  warn "$id: no portable graymap image file found\n";
  exit(1);
}

read(STDIN,$in,1024,length($in));

# Eat comments, if the version allows comments
if ($type < 4 and $in =~ tr:#:#:) {
  while ($in =~ s/(^|\cm|\cj)#[^\cj\cm]*([\cm\cj])/$1$2/g) { 
    read(STDIN,$in,256,length($in));
  }
}

if ($in =~ s/^\s*(\d+)\s+(\d+)\s+//) {
  $x = $1;
  $y = $2;
} else {
  warn "$id: no dimensions found in image\n";
  exit(1);
}

if ($in =~ s/^\s*(\d+)\s+//) {
  $max = $1;
} else {
  warn "$id: no max value found in image\n";
  exit(1);
}

if ($max != 255 and !$custommap) {
  warn "$id: Internal map is for 255 grays, this has $max grays\n";
}

my $done = 0;
my $col  = 0;
my $pixel = 0;

my $chr;
while($done < 2) {
  if (length($in) < 4) {
    if (!read(STDIN,$in,1024,length($in))) {
      $done = 1;
    } elsif ($type == 2) {
      # Nuke any leading whitespace if this is a text based PGM file.
      $in =~ s/^\s+//;
    }
  }

  if ($type == 5) {
    $in =~ s/^(.)//s;
    $chr = $1;
  } else {
    if ($done) {
      ($chr) = $in =~ s/^(\d+)\s*//s;
    } else {
      ($chr) = $in =~ s/^(\d+)\s+//s;
    }
    if (!defined($chr)) {
      warn "$id: Missing gray value!\n";
      next;
    }
    $chr = sprintf("d%03d", $chr);
  }

  if (!defined($map{$chr})) {
    my $pchar = $chr;
    my $new = ' ';
    if (!($pchar =~ /^d(\d+)/)) {
      $pchar = sprintf("%03d",$1);
    }
    if ($pchar > ($max /2)) {
      $new = 'M';
    }
    warn "$id: Gray $pchar missing from map using <$new>\n";
    $map{$chr} = $new;
  }

  print $map{$chr};
  $pixel ++;
  $col++;
  if ($col == $x) {
    print "\n";
    $col = 0;
  }
  if($done and $in eq '') {
    $done = 2;
  }
  if ($pixel > ($x * $y)) {
    die "Too many pixels\nin=$in\n" unless $done == 2;
  }
}

print "\n";
exit(0);


sub usage ($) {
  my $exit = shift;

  print "$id: usage\n", <<USAGEnotes;
  	pgmtoascii [option] [file]

Options:

	-h	--help		print this message
	-m	--map MAPFILE	use MAPFILE for gray to ascii map

USAGEnotes

  exit($exit);
} # end &usage 

# A really crude way to read in the graymapping file.
sub readmap ($) {
  my $file = shift;

  do $file or die "$id: Can't read map: $!\n"

} # end &readmap 

Original message headers
X-Google-Thread: f996b,d2eee4c8b9d7cb21
X-Google-Attributes: gidf996b,public
X-Google-Language: ENGLISH,ASCII-7-bit
Path: g2news2.google.com!news4.google.com!newsfeed2.dallas1.level3.net!news.level3.com!panix!qz!not-for-mail
From: Eli the Bearded <*@eli.users.panix.com>
Newsgroups: alt.ascii-art
Subject: Re: img->ascii
Date: Wed, 18 Oct 2006 21:18:09 +0000 (UTC)
Organization: Some absurd concept
Lines: 225
Message-ID: <eli$061...@qz.little-neck.ny.us>
References: <4529c70f$0$19626$882...@free.teranews.com> <200...@usenet.piggo.com> <eli$061...@qz.little-neck.ny.us> <200...@usenet.piggo.com>
NNTP-Posting-Host: panix2.panix.com
X-Trace: reader2.panix.com 1161206289 28771 166.84.1.2 (18 Oct 2006 21:18:09 GMT)
X-Complaints-To: abu...@panix.com
NNTP-Posting-Date: Wed, 18 Oct 2006 21:18:09 +0000 (UTC)
X-Liz: It's actually happened, the entire Internet is a massive game of Redcode
X-Motto: "Erosion of rights never seems to reverse itself." -- kenny@panix
X-US-Congress: Moronic Fucks.
X-Attribution: EtB
Encrypted: double rot-13
User-Agent: Vectrex rn 2.1 (beta)
X-Comments: "(Welcome to the desert of the real.)" -- eck@panix
	    "... terrorism is open-source war ..." -- srini@unamerican
Xref: g2news2.google.com alt.ascii-art:2522
About this message. This message was posted publicly to the newsgroup alt.ascii-art in 2006 and is mirrored here unchanged as part of the Historic Archives – only email addresses are masked. The artwork and text belong to their original authors: if you copy a piece, keep the artist's initials or signature intact and credit them where you can. Are you the author? Contact us to get your posts attributed, connected to your artist profile, or removed.

Report this message

Help us keep the archive clean and accurate. Reports are reviewed by a person – nothing is changed automatically.

Help classify this post