It also needs a binmode to prevent Unicode warnings. It is fun to play with. Here is a bit of a cleaned up version that uses Unicode, map, say and ternary. It also resets the color at the end of each line: not all peple prefer a black background.

#!/usr/bin/perl use 5.036001; use warnings; our $VERSION = "0.01 - 20230629"; our $CMD = $0 =~ s{.*/}{}r; sub usage { my $err = shift and select STDERR; say "usage: $CMD ..."; exit $err; } # usage use Carp; use Convert::Color; use Term::ANSIScreen qw(:color); use GD; use Data::Peek; use Term::ReadKey; use Getopt::Long qw(:config bundling); GetOptions ( "help|?" => sub { usage (0); }, "V|version" => sub { say "$CMD [$VERSION]"; exit 0 +; }, "d|dark". "|dark-background!" => \ my $dark, "v|verbose:1" => \(my $opt_v = 0), ) or usage (1); my $bgc = $dark ? "black" : "white"; my ($cols, $rows, $wpixels, $hpixels) = GetTerminalSize (); # Prevent auto-wrapping $cols--; $rows--; # "Greyscale" unicode blocks binmode STDOUT, ":encoding(utf-8)"; # This could be specified nicer, but seems to be a problem when postin +g to PerlMonks, see comment below my @shades = ("\N{SPACE}", "\N{LIGHT SHADE}", "\N{MEDIUM SHADE}", "\N{ +DARK SHADE}", "\N{FULL BLOCK}"); # Pre-generate colors my @termcolors = map { color $termcolor . " on $bgc" } qw( red yellow green cyan blue magenta white ); # White won't "wor +k" on light background # Iterate through all image filenames given on command line foreach my $fname (@ARGV) { say "------ $fname ------"; my $ok = 0; eval { printImage ($fname); $ok = 1; }; $ok ? say "" : warn "ERROR: ", $! ? "$!" : $@, "\n"; } sub printImage ($fname) { -s $fname or croak ("Non-existing or empty image"); my $img = GD::Image->new ($fname) or croak "GD::Image cannot proce +ss $fname"; my ($origw, $origh) = $img->getBounds (); my ($w, $h) = ($origw + 0, $origh + 0); my $reset = color "reset"; my $divfactor = 1; if ($w > $cols) { my $tmp = $w / $cols; #say "$w / $cols / $tmp"; $tmp > $divfactor and $divfactor = $tmp; } if ($h > $rows) { my $tmp = $h / $rows; #say "$h / $rows / $tmp"; $tmp > $divfactor and $divfactor = $tmp; } if ($divfactor > 1) { $w = int ($w / $divfactor); $h = int ($h / $divfactor); my $newpic = GD::Image->new ($w, $h, 1); $newpic->copyResized ( $img, 0, 0, # DEST X Y 0, 0, # SRC X Y $w, $h, # DEST W H $origw, $origh, # SRC W H ); $img = $newpic; } for (my $y = 0; $y < $h; $y++) { my $lastcindex = -1; for (my $x = 0; $x < $w; $x++) { my $index = $img->getPixel ($x, $y); my ($r, $g, $b) = $img->rgb ($index); #my $grey = int(($r + $g + $b) / 3); my $basecolor = Convert::Color->new ("rgb8:" . join "," => + $r, $g, $b); my ($ph, $ps, $pv) = $basecolor->as_hsv->hsv; my $colormode = $ps > 0.5 ? 1 : 0; # Map the brightness to the corresponding Unicode characte +rs my $brightness = int ($pv * 4); defined ($shades[$brightness]) or croak ("Undefined $pv -> $brightness"); my $shade = $shades[$brightness]; # Map the color to roughly their corresponding ANSI termin +al color code. my $cindex = $ps < 0.5 ? 6 : # White $ph > 34 && $ph <= 82 ? 1 : # Yellow $ph > 82 && $ph <= 159 ? 2 : # Green $ph > 159 && $ph <= 205 ? 3 : # Cyan $ph > 205 && $ph <= 270 ? 4 : # Blue $ph > 270 && $ph <= 330 ? 5 : # Magenta 0 ; # Red unless ($cindex == $lastcindex) { print $termcolors[$cindex]; $lastcindex = $cindex; } print $shade; } say $reset; } print $reset; return; } # printImage

Enjoy, Have FUN! H.Merijn

In reply to Re: Imagecat - show color images in a terminal by Tux
in thread Imagecat - show color images in a terminal by cavac

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.