The script works by manipulating indexes, replacing the palette entries with one of two rgb values depending upon the threshhold calculation. GD doesn't make any attempt to compress the pallete. You've told it that the image contains 256 'colors'--despite that they all map to one of two sets of rgb values--so it doesn't attempt to discard any 'reduntant' palette entries.

You can achieve what you want by creating a new image that has only two palette entries and mapping the pixels across yourself. This runs more slowly, but results in a 1-bit BW image output rather than a 8-bit color with all the entries mapped to black or white. Ie. Much smaller images.

The following code does that, and produces an output file with the same name as the input prefixed with 'BW-'. I've also made the threashhold a command line parameter -T=nn: values range from 0 .. 255 per the original algorithm.

#! perl -slw use strict; use GD; our $T ||= 150; my $file = shift @ARGV; die "$file not found" unless -e $file; my $in = GD::Image->new( $file ) or die $!; my( $w, $h ) = $in->getBounds; ## New palette image with just two colors my $out = GD::Image->newPalette( $w, $h ); my $black = $out->colorAllocate( 0, 0, 0 ); my $white = $out->colorAllocate( 255, 255, 255 ); for my $y ( 0 .. $h - 1 ) { for my $x ( 0 .. $w -1 ) { my( $r, $g, $b ) = $in->rgb( $in->getPixel( $x, $y ) ); ## Map the pixels relative to the threshhold my $index = (( $r = $g +$b ) / 3) > $T ? $black : $white; $out->setPixel( $x, $y, $index ); } } open OUT, '>:raw:perlio', "BW-$file" or die $!; print OUT $out->png( 9 ); close OUT; #system 1, "BW-$file"; ## display image

Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.
"Too many [] have been sedated by an oppressive environment of political correctness and risk aversion."

In reply to Re: GD - Two colors? by BrowserUk
in thread GD - Two colors? by Anonymous Monk

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.