Settings

The page will reload to apply your changes.
Theme

ASCII Art Font

ASCII Art Menu

Original Usenet message from alt.ascii-art, 27 Mar 2001.
Re: -Very Simple Java JPG to ASCII Converter

Re: -Very Simple Java JPG to ASCII Converter

In alt.ascii-art, Markus Gebhard  <ukg...@rz.uni-karlsruhe.de> wrote:
> Heinz Flug wrote:
> > // Very simple JPG to ASCII converter.
> > // Written on a saturday night by Heinz Flug .. flu...@online.no
> > // The code cries out for improvement....
> Cool, but you should mention that your code needs Java2 and will not run
> in Java 1.1.
> 
> If your looking for improvement, I have an improved piece of Java work:
> Jave. It has a much more complicated image2ascii algorithm included. I
...

Personally, I feel that if it needs a network connection (or browser)
to run, it is going to be too slow. (But I do things like make ASCII
art movies.)

Here's a PGM to ASCII script I wrote last year, as an example of a
different, simple conversion. It is designed to work from the command
line. Like all good PBM/PGM/PPM tools, it can take the input from a
file or from standard input. (PBM: portable bitmap (black and white);
PGM: portable graymap (grayscale); PPM: portable pixmap (full color);
PBM/PGM/PPM tools are an old Unix graphics suite, perfect for automated
processing of images.)

Typical usage of this would be something like:

  djpeg image.jpg | pnmscale -yscale 0.5 | pnmscale -xsize 64 | \
	ppmtopgm | pgmnorm | pgmtoascii

This gives much better results than the pbmtoascii that comes with the
PBM/PGM/PPM tools. The first pnmscale adjusts for the non-square pixels
of ascii art, the second resizes to a the number of columns wide the
picture will be. The ppmtopgm converts a color image to grayscale,
the pgmnorm "normalizes the contrast in a portable graymap". I assume
white text on a black background. Throw in a pnminvert after the
normalization to change that. Then use pbmtext if you want an image
version of the text.

By way of sample output here is Mr T, 40 chars wide:

				   .            
		       ,.          ;;.          
		       ,.          .+,          
		       ,.          ,++,         
				  .,++;;,       
		       ...,;;;;+++;++xxx+;.     
	       ..     .,;+xzzzmmmmmzzzzzzz+     
	      .mx,,;..,;xzmXXXYYYYXXmmmXXXX+    
	     .,mmxmX+,;+zXXYUUUCCCCUYYUUUUCY.   
	    .;.,,+XUx,;+mYUUUUUCCCOCUUOWWWWU;   
	    ;x..,+xx;,;+mUUUUUUUCCCCUUWWWWUz+,  
	    ,+,,mCY,..,;zYUCOUUCCOCUUUOWWCzx+,  
	    ;m+XYUU+.,,+++zXYYYYYXmmzzXMWWmx;   
	    ,m+Om;;,;+mYX+;zYUYmzzx+zmYWWWYx.   
	     ++Um,;+zzmmz+,xYUYYYYYUUUUUWWY+    
	     ;+UY;;+zzmmx;;zYUYUCCCCCUYCWCm;    
	    .;+Yz,,;+mmx;;+mUYXYUOOCUYYWWX+.    
	     ,+Xx,,;xzx;,,;mUYXYUUUUUYCWU+,     
	     .;mz,.,,,,..,+mzzzXYYUYYXUUz;      
	      ;++,    .;xzzXXmx+;++xxxxxx,      
	     ;z+;..  .+xmmXYYYXz+,,,,;+xx.      
	   .;m+,;,.   .,+xmYUYXm+,,,;;xYUX;.    
	..;;;z; ,;,    .,,;+xx+,. ,;+zYCUCUz+;, 
	 .;,;,,. .      .,;++;.   ,+mUCUUCUYzmzx
	  ,;;;,+.               .,xYCUUYYUUUmzzz
	  .++++m+,..           .;xXYYYYYmUUUXzzz
	;;;;;,+mYXmx+;,.... .,;+zzXUYUUUXUUUmzzz
	...  .+mUYUYmXmzxxx;;zXXYYYUUUUUUCCUzzzz
	.... .;zUYUUmYXXmzm+;mYXXYYUUUYUUOOCmz


A better tool would use edge detection to emphasize changes of
"color" and some sort of de-speckle method to produce less
"noisy" output.

Elijah
------
#!/usr/bin/perl -w
# Convert PGM to ASCII art.
# 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.

 # Thirty-two 'color' scale derived from Scarecrow's ASCII Art FAQ.
  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;

while(!$done) {
  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+//;
    }
  }

  my $chr;
  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 ($pixel > ($x * $y)) {
    die "Too many pixels\nin=$in\n" unless $done;
  }
}

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 

__END__

Original message headers
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: f996b,7e54e5da9318d064
X-Google-Attributes: gidf996b,public
X-Google-ArrivalTime: 2001-03-26 16:17:44 PST
Path: supernews.google.com!sn-xit-02!supernews.com!news.tele.dk!213.38.238.69!cw-insnet-peer-00!newsfeed.media.kyoto-u.ac.jp!news-spur1.maxwell.syr.edu!news.maxwell.syr.edu!newsxfer.eecs.umich.edu!panix!news.panix.com!qz!not-for-mail
From: Eli the Bearded <*@qz.little-neck.ny.us>
Newsgroups: alt.ascii-art
Subject: Re: -Very Simple Java JPG to ASCII Converter
Date: 27 Mar 2001 00:15:54 GMT
Organization: Some absurd concept
Lines: 265
Message-ID: <eli$010...@qz.little-neck.ny.us>
References: <3ab...@hard.sjanker.heinzflug.com> <3AB...@rz.uni-karlsruhe.de>
NNTP-Posting-Host: panix5.panix.com
X-Trace: news.panix.com 985652154 18859 166.84.0.230 (27 Mar 2001 00:15:54 GMT)
X-Complaints-To: abu...@panix.com
NNTP-Posting-Date: 27 Mar 2001 00:15:54 GMT
X-Files: Used for sharpening claws and teeth on your hawk and hacksaw
X-Motto: "Erosion of rights never seems to reverse itself." -- kenny@panix
X-US-Congress: Moronic Fucks.
X-Attribution: EtB
X-Usenet-II: Because it is time for October.
Encrypted: double rot-13
X-Newsreader: Sony Playstation 5.0MIPS
Xref: supernews.google.com alt.ascii-art:4915
About this message. This message was posted publicly to the newsgroup alt.ascii-art in 2001 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.