Settings

The page will reload to apply your changes.
Theme

ASCII Art Font

ASCII Art Menu

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

Re: img->ascii

In alt.ascii-art, Troy Piggins  <use...@piggo.com> wrote:
> * 3Dimka wrote:
> > Has anyone tried Characterizer?
> > it automatically converts images to text:
> > http://aolej.com/free.html#Chara
> Do you know of an equivalent for linux?

Get the PBM Tools (or Netpbm) package and use pbmtoascii (comes
with the package) or hunt the Google archives for my pgmtoascii
script.

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

Look for the colorizer at the end of this post.

Elijah
------
#!/usr/bin/perl -w
# Take a PPM file and a text file and create an html file with colorized
# text based on the PPM colors. The text file should have at least as
# many characters as the PPM file has pixels.
# Output is html, either HTML 3.2 with <font color> tags or HTML 4
# with a stylesheet and <span class> tags. A background color can be
# specified.
#
# 6 April 2006		B. Elijah Griffin	Eli the Bearded
use strict;
use vars qw( $ppmin $textin $mode $bgcolor $body $css $char $color $title
	     $id $w $h $p $ppm_r $p_r $text %colors @pixels $lastcolor
	     $rc $pc $cc $cssc $fontsize
           );
use Image::PBMlib;

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

$title = 'image';
$bgcolor = 'white';
$body = $css = '';
$mode = 'css';

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 'h' or $arg eq '-html') {
    $mode = 'html';
  } elsif ($arg eq 'c' or $arg eq '-css') {
    $mode = 'css';
  } elsif ($arg eq 'b' or $arg eq '-bgcolor') {
    $bgcolor = shift;
  } elsif ($arg eq 't' or $arg eq '-title') {
    $title = shift;
  } elsif ($arg eq 'f' or $arg eq '-fontsize') {
    $fontsize = shift;
  } else {
    die "$id: unrecognized option: -$arg\n";
  }
}

$textin = shift;
$ppmin  = shift;

if (!defined($textin) or !defined($ppmin)) {
  die "$id: need two input files: textin ppmin\n";
}
if (!(-f $textin) or !(-f $ppmin)) {
  die "$id: one or more input file does not exist\n";
}

if(!open(PPM, "< $ppmin")) {
  die "$id: can't open ppm file $ppmin: $!\n";
}
if(!open(TEXT, "< $textin")) {
  die "$id: can't open text file $textin: $!\n";
}

$ppm_r = readppmheader(\*PPM);

if(defined($$ppm_r{error})) {
  die "$id: ppm error $ppmin: $$ppm_r{error}\n";
}

if($$ppm_r{bgp} ne 'p') {
  die "$id: ppm error $ppmin: not a pixmap\n";
}

$w = $$ppm_r{width};
$h = $$ppm_r{height};
$p = ($w * $h);
@pixels = readpixels_raw(\*PPM, $$ppm_r{type}, $p);

{ local $/ = undef; $text = <TEXT>; }

$text =~ tr:\cJ\cM::d;

if($p > length($text)) {
  die "$id: text error $textin: not enough characters\n";
}

close TEXT;
close PPM;

$cc = $pc = $rc = 0;
while($p_r = shift(@pixels)) {
  # Grab the first character, and replace it with the empty string
  $char = substr($text, 0, 1, '');
  
  # Don't color spaces
  if($char !~ /\s/) {
    $color = sprintf("%02x%02x%02x",
    		ord($$p_r[0]), ord($$p_r[1]), ord($$p_r[2]));

    if(defined($lastcolor)) {
      if ($color ne $lastcolor) {
	&endcolor();
	&startcolor($color);
      }
    } else {
      &startcolor($color);
    }

    if ($char =~ /<>&/) {
      $char =~ s/&/&amp;/;
      $char =~ s/</&lt;/;
      $char =~ s/>/&gt;/;
    }
  }

  $body .= $char;

  $pc ++;
  $rc ++;

  if($rc == $w) {
    $body .= "\n";
    $rc = 0;
  }
}

&endcolor();

print <<"HTMLheader";
<html>
<head>
<title>$title</title>
HTMLheader

if($mode eq 'css') {

if(defined($fontsize)) {
  $fontsize = " font-size: $fontsize";
} else {
  $fontsize = '';
}

print <<"CSSheader";
<style type=text/css><!--
H1 { font-weight: bold; font-size: 18px }
Pre { background-color: $bgcolor; border: 1px gray; font-family: courier,monospace;
      padding: 0px 5px 20px 5px; margin: 10px 5px 10px 15px;$fontsize }
$css
--></style>
</head>
<body>
CSSheader

} else {

print <<"BODYheader";
</head>
<body bgcolor="$bgcolor">
BODYheader
}

print <<"FINISHpage";
<h1>$title</h1>
<pre>$body</pre>
</body>
</html>
FINISHpage

exit(0);

sub startcolor($) {
  $lastcolor = shift;

  if($mode eq 'css') {
    if(!defined($cssc = $colors{$lastcolor})) {
      $cssc = sprintf("c%x", $cc);
      $cc ++;
      $colors{$lastcolor} = $cssc;
      $css .= ".$cssc {color:#$lastcolor}\n";
    }
    $body .= "<span class='$cssc'>";
  } else {
    $body .= "<font color='#$lastcolor'>";
  }
} # end &startcolor

sub endcolor() {
  if($mode eq 'css') {
    $body .= "</span>";
  } else {
    $body .= "</font>";
  }
} # end &endcolor

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

  print "$id: usage\n", <<USAGEnotes;
  	colorizeascii [options] textfile ppmfile

Options:

	-h	--help		print this message
	-t	--title TITLE	use TITLE as html title
	-c	--css		use CSS for colors
	-h	--html		use HTML 3.2 for colors
	-b	--bgcolor COLOR	use COLOR as background color
	-f      --fontsize SIZE use SIZE as font size (CSS only)

USAGEnotes

  exit($exit);
} # end &usage 

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: Tue, 17 Oct 2006 22:31:58 +0000 (UTC)
Organization: Some absurd concept
Lines: 236
Message-ID: <eli$061...@qz.little-neck.ny.us>
References: <4529c70f$0$19626$882...@free.teranews.com> <200...@usenet.piggo.com>
NNTP-Posting-Host: panix2.panix.com
X-Trace: reader2.panix.com 1161124318 23165 166.84.1.2 (17 Oct 2006 22:31:58 GMT)
X-Complaints-To: abu...@panix.com
NNTP-Posting-Date: Tue, 17 Oct 2006 22:31:58 +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:2518
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