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