more useful options PerlMonks

### convert image to greyscale (color to black and white)

by PodMaster (Abbot)
 on Dec 21, 2003 at 11:24 UTC Need Help??
 Description: convert color image to grayscale convert color image to greyscale color to b&w color to black and white color to grayscale
```use strict;
use warnings;
use GD;

my \$in_file = qw[foo.png];
my \$ni_file = \$in_file.".ni.png";
my \$image = GD::Image->new(\$in_file);

my \$i = 0;
my \$t = \$image->colorsTotal();

while(\$i < \$t) {
my( @c ) = \$image->rgb( \$i );
my \$g = ( \$c[0] + \$c[1] + \$c[2] ) / 3; # Color::Calc::grey

\$image->colorDeallocate(\$i);
\$image->colorAllocate( \$g, \$g, \$g );
\$i++;
}

write_file( \$ni_file, \$image->png );
sub write_file {
my \$i = shift;
open DISPLAY, ">\$i" or die "can't clobber \$i \$!";
binmode DISPLAY;
print DISPLAY @_;
close DISPLAY;
}
```
Replies are listed 'Best First'.
Re: convert image to greyscale (color to black and white)
by tachyon (Chancellor) on Dec 21, 2003 at 12:10 UTC

For obvious reasons this conversion method is called the arithmetic mean method. There is also a geometric mean method (it basically sucks and uses (R*G*B)^1/3 - so if R,G, or B are 0 you get black).

### Proportional sum of the colors.

This algorithm is simple but the basis of it is much more complex. The formula looks like:

```Y = .30 * R + .59 * G + .11 * B.

The percentages here relate to how perceptive the eye is to a given color.

"The formula used in the GIMP is Y = 0.3R + 0.59G + 0.11B; this result is known as luminance. The weights used to compute luminance are related to the monitor's phosphors. The explanation for these weights is due to the fact that for equal amounts of color the eye is most sensitive to green, then red, and then blue. This means that for equal amounts of green and blue light the green will, nevertheless, seem much brighter. Thus, the image obtained by the normal averaging of an image's three color components produces a grayscale brightness that is not perceptually equivalent to the brightness of the original color image. The weighted sum that defines Y, however, does." - READ FULL TEXT, SEE PRETTY EXAMPLE PICTURES at http://gimp-savvy.com/BOOK/index.html?node54.html

cheers

tachyon

The formula used in the GIMP is Y = 0.3R + 0.59G + 0.11B; this result is known as luminance.
This formula is not only used in The Gimp, but also in television. In order to make the color signal grokkable by B&W TV sets, a backward compatible signal is used, consisting of this liminance as the main signal, which means it is what you get to see on B&W sets; plus two extra color difference signals modulated on top of that.

From what I gather, both NTSC (USA) and PAL (Europe) use the same mechanism.

Common abbreviations in literature are YUV and YCrCb — "Y" is the lumninance signal, U and V resp. Cr/CB are the two other, "chroma", signals . Look here for an intro, for example.

Re: convert image to greyscale (color to black and white)
by b10m (Vicar) on Dec 21, 2003 at 11:44 UTC
Just for the record, if you want to do this with Image::Magick (TIMTOWTDI ;-), your code could look something like this:
```use strict;
use Image::Magick;

# Define \$image
my \$image=Image::Magick->new;

# Define the image you want to convert

# Set grayscale
\$image->Quantize(colorspace=>'gray');

# Write the new file
\$new_image = \$image->Write('new_image.png');

And yes, this is well documented ...

--
b10m
Re: convert image to greyscale (color to black and white)
by liz (Monsignor) on Dec 21, 2003 at 11:31 UTC
my \$g = ( \$c[0] + \$c[1] + \$c[2] ) / 3; # Color::Calc::grey

Although this is the way in which Color::Calc::grey works, I wonder whether this will produce the best black and white version of a color image. This is because the human eye is differently sensitive to different colours (e.g. more sensitive to red, which is why traffic lights are red, or so I'm told).

I don't have another formula to be used at hand, just pointing out something that may warrant some further investigation.

Liz

May I suggest that the reason that green is used for night lighting isnt so much the eye sensitivity, tho that IS true. It has to do with persistance of vision. Green can be easily seen at night while preserving your night vision. Brighter colors like yellow kinda mess-up your night vision. Green is more "friendly" to your night vision. Some car instrument panels (speedometer, etc) are lit in yellow. While that is easier to see it is hard on your night vision. This may not be true for everybody, but it is generally true for most people.
Re: convert image to greyscale (color to black and white)
by theAcolyte (Pilgrim) on Dec 31, 2003 at 14:25 UTC
Hello Monks!

I've been a graphic artist for 10 years; I'm a programmer more as a hobby then anything else (and a bit for doing web-based stuff).

The formula posted above for colorTV to BW is correct to the best of my memory, but it doesn't represent the "best" conversion. It represents a conversion where the fewest colors will be "lost" (no contrast).

In reality, making each image look its best is something best done by hand. For example, in photoshop, you can use "convert to greyscale" or you can specifiy in the RGB channel mixer the percentages of each color to use in producing your greyscale image.

Many images actually look best with a Green % as high as 70%. I've seen a few that look great with Green at 80% (fairly rare).

I'm not sure this helps much ... as I'm basiclly saying that its difficult to get great greyscale from random color images in an automated fashion. If a group of images are similar (all daytime landscapes) you can probably find an ideal % set up by hand, then batch them.

# theAcolyte

Create A New User
Node Status?
node history
Node Type: snippet [id://316166]
help
Chatterbox?
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (4)
As of 2018-05-23 02:02 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
World peace can best be achieved by:

Results (166 votes). Check out past polls.

Notices?