![]() |
|
Perl Monk, Perl Meditation | |
PerlMonks |
Re: Imagecat - show color images in a terminalby Anonymous Monk |
on Jun 29, 2023 at 12:25 UTC ( [id://11153210]=note: print w/replies, xml ) | Need Help?? |
Many thanks for great distraction! Actually, you rolled out manual color quantization to pre-defined palette; any decent graphics toolkit should provide this option. In Strawberry, I had to inject this fragment (same as in my code below) close to top so that your script works in standard Windows console/terminal:
I didn't do binmode STDOUT, ':utf8'; but continue, instead, to print octets for consistency with your code. Plus I won't digress why e.g. the "$OUT" has to be defined in outer scope, or else this node will be too long! (Same about few other details) One important thing is I'm setting the ENABLE_VIRTUAL_TERMINAL_PROCESSING bit ("4" above), therefore at least Win10 with an (automatic) update of a few years ago is required. I didn't investigate why the usual alternative route with Win32::Console::ANSI seems to be incompatible with printing Unicode. My test subject is ImageMagick's built-in [1] rose image which is 70 by 46; I hardcoded file name and totally omitted resizing in my code. Also, there is no error checking for e.g. incompatible color model, etc. To produce a file:
Another note: I had plans to add many more colors to palette, like e.g. whatever RGB triplet would be produced with trivial formula for "red medium shade block on cyan". However, it looks like fonts are very inconsistent in which area percentage is filled with Light/Medium/Dark blocks (click "View All" [2]). E.g. for Consolas, medium block fill is close to 25% rather than 50%. Hues would be very wrong. I decided to stop with pure hues on black and on white (the latter are kind of "diluted" hues). Color distortion would then be wrong contrast/gamma only. In fact, on Windows, colors 0-7 are "dimmed" anyway. Nevertheless, screenshots below were produced with "Raster 8x12" font in terminal, they have 50% for "medium" (DejaVu Sans Mono has 50%, too. That's too much digression already.) With your script, I don't see [3] "Full block" in neither red nor white in output (image certainly has a few pure white pixels). Don't know if it was intentional, but at least it partially explains differences with my code results. My script, and output [4]:
I'm using colorClosestHWB, as it produces slightly more pleasing picture than colorClosest (e.g. no unexpected magenta). The latter output is same if I'd copy (GD's method) source to destination of appropriate size and palette stuffed to capacity with e.g. dummy black. And very close, but not the same if I'd use Imager and its to_paletted instead of GD. Imager also allows to dither with error diffusion, but result looks very ugly for such small image and enlarged "pixels". OK, let's add "diluted" hues as BLOCK3 after BLOCK2 (palette sequence order doesn't matter; everything is resolved automatically):
Ah, here are [5] some hints of other hues than just red! But still "somewhat" far from true color image. Let's get serious and add some science! I'll map RGB triplets to palette indexes manually (just as you did in original script), but instead of whatever algorithm GD (or Imager, etc.) uses to find "closest color", I'll calculate distances and pick minimum in perceptually uniform color space (i.e. neither RGB nor HSV). Script is very similar but requires PDL and related modules from CPAN:
And isn't result [6] better? Looks like science works after all. In case anyone wonders, no black in output is because there is no black (nothing close to it) in original, but such conversions are better performed with "black point compensation" adjustment, which I didn't looked into how to add in this case. 1. https://imagemagick.org/image/rose.png 2. https://www.fileformat.info/info/unicode/char/2592/fontsupport.htm 3. https://i.ibb.co/R0WmtNF/original.png 4. https://i.ibb.co/cbt07mK/my-gd.png 5. https://i.ibb.co/JnCdFP7/my-gd.png 6. https://i.ibb.co/kHCLYCg/scientific.png
In Section
Cool Uses for Perl
|
|