#!/usr/bin/perl #-- ---------------------------------------------------------------------------- #-- From http://egb13.net/ #-- Last updated: 2011-09-01 #-- ---------------------------------------------------------------------------- #-- Copyright (C) 2011 by EGB13.net #-- #-- Permission is hereby granted, free of charge, to any person obtaining a copy #-- of this software and associated documentation files (the "Software"), to deal #-- in the Software without restriction, including without limitation the rights #-- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell #-- copies of the Software, and to permit persons to whom the Software is #-- furnished to do so, subject to the following conditions: #-- #-- The above copyright notice and this permission notice shall be included in #-- all copies or substantial portions of the Software. #-- #-- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR #-- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, #-- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE #-- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER #-- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, #-- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN #-- THE SOFTWARE. #-- ---------------------------------------------------------------------------- #-- removes (makes transparent) certain colors in a transparent GIF or PNG file #-- used to remove noise from NOAA weather radar images, tho at the loss of some #-- valid low-intensity returns. Intended for use on the radar-only images from #-- http://radar.weather.gov/Conus/RadarImg/ #-- #-- lately added "-c" option to specify a single color to remove, used for removing #-- the black background on images found at http://mesonet.agron.iastate.edu/docs/nexrad_composites/ #-- ##-- 2011-09-01: Switch to Image::Magick package as that's what works with ##-- ActiveState Perl for this utility. #-- ---------------------------------------------------------------------------- my @noise = ( '#019ff4' , '#04e9e7' , '#e3e3e3' , '#e6e6e6' , '#e8e8e8' , '#ebebeb' , '#eeeeee' , '#f0f0f0' ) ; my $darkblue = '#0300f4' ; # darkest blue use Image::Magick ; #-- Graphics::Magick may work, too; you'd have to change "Image::" where ever it appears require 'getopt.pl' ; sub Usage { my ($msg) = @_ ; print STDERR "$msg" if (defined ($msg) && $msg ne "") ; print STDERR "Usage: $0 -i inputfile -o outputfile [-f fuzzfactor] [-x] [-c #xxxxxx]\n" ; print STDERR " fuzzfactor is an integer (default=32)\n -x to get the extra bit of blue out\n -c specifies the ONLY color to remove\n" ; exit 1 ; } &Getopt('iofc') ; &Usage if (defined ($opt_h) ) ; &Usage ("Please specify input file\n") if (!defined ($opt_i) ) ; &Usage ("Please specify output file\n") if (!defined ($opt_i) ) ; $opt_f = 32 unless defined ($opt_f) ; &Usage ("Fuzz factor must be an integer.") unless ($opt_f =~ m/^\d+$/) ; push @noise, $darkblue if (defined ($opt_x) ) ; if (defined ($opt_c) ) { if ($opt_c =~ m/^\#?([0-9a-f]{6})$/i) { #-- works w/ or w/o the "#" in the argument @noise = ( "#".$1 ) ; } else { &Usage (sprintf "Don't recognize a color value in '%s'; use xxxxxx where 'x' is a hex digit.\n", $opt_c) ; exit 1 ; } } my $img = Image::Magick->new; my $res = $img->Read($opt_i) ; die "$opt_i: $!\n" if ($res) ; $img->Set(colorspace => RGB, type => TrueColor) ; foreach my $color (@noise) { $img->Transparent(color => $color, fuzz => $opt_f) ; } $res = $img->write(filename => $opt_o) ; die "$opt_o: $!\n" if ($res) ; undef $img ; exit 0 ;