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 " ";
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 | |
by jcwren (Prior) on Oct 04, 2000 at 00:14 UTC | |
by jimt (Chaplain) on Oct 06, 2000 at 20:40 UTC |