Hi, sometimes you have a whole bunch of rgb files which are just too dark or light, and you want to adjust them all in one step. Well, I used to launch gimp and adjust each one individually, but that is a pain if you have more than a few files. Well here is a way with Imager.

The idea is very simple. You increase(decrease) the rgb values to adjust brightness, and you adjust the contrast with +- intensity.

Experiment on 1 first to get the best setting, then run it in a whole dir full of images. If you keep the $r $g $b values identical, the brightness change will be uniform, otherwise you will be adjusting "tint" as well.

#!/usr/bin/perl use warnings; use strict; use Imager; use File::Basename; # Usage: "scriptname red blue green contrast" # example: " script 2.0 2.0 0.5 2.0" # 1.0 is "no change", values less than 1 reduce my ($r,$g,$b,$c) = @ARGV; $r = $r || 1; $g = $g || 1; $b = $b || 1; $c = $c || 1; umask 0022; my @pics= <*.jpg *.gif *.png>; my @exts = qw(.jpg .gif .png); my $img = Imager->new(); my @redmap = map { int( $r * $_) } 0..255; my @greenmap = map { int( $g * $_) } 0..255; my @bluemap = map { int( $b * $_) } 0..255; foreach my $file (@pics){ my ($basename,$path,$suffix) = fileparse($file,@exts); $img->open(file=>$file) or die $img->errstr(); my $newimg = $img->copy(); $newimg->map( red=>\@redmap, green=>\@greenmap, blue=>\@bluemap ); $newimg->filter(type=>'contrast', intensity=> $c); $newimg->write(file=>"$basename-b$suffix" ); }

Replies are listed 'Best First'.
Re: batch rgb brightness and contrast adjustment
by zentara (Cardinal) on Oct 10, 2005 at 17:55 UTC
    A comment on this script was made to me thru email, and so I'll post it here in case in may help someone looking at this snippet.

    edada wrote:

    Hi, I had some problems changing brightness with Imager using your code :

    my @redmap = map { int( $r * $_) } 0..255; my @greenmap = map { int( $g * $_) } 0..255; my @bluemap = map { int( $b * $_) } 0..255; [..] $newimg->map( red=>\@redmap, green=>\@greenmap, blue=>\@bluemap ); $newimg->filter(type=>'contrast', intensity=> $c);
    In Imager::Filters doc I can read that the contrast filter "scales each channel by intensity. Values of intensity < 1 will reduce the contrast". Your map do the same if $r => $b => $g ! It's supposed to change brightness and not contrast. I simply recommand you writing it with a :
    my @redmap = map { int( $r + $_) } 0..255;
    and using null value as default value.

    I'm not really a human, but I play one on earth. flash japh