#!/bin/bash ## img2transparent -- make the background be transparent -- by Eugene Reimer 2005July ## USAGE: ## img2transparent InputFile [ResultFile] -- produces InputFile.gif when ResultFile omitted; handles ResultFile same as InputFile F=${1##*/}; B=${F%.*}; OF=$B.gif ##default resultfile is $1 without directory-part, and with suffix replaced by gif [ $# -ge 2 ] && OF=$2; ##optional 2nd param is the result-name; 2007apr: added, so cam make a PNG echo "--img2transparent: Input=$1 Output=$OF" ##DEBUG [ -e "$OF" ] && cp -pfv $OF $OF~ ##make bkup of file to be overwritten if [[ $1 == *ppm ]];then PPM=$1; else PPM=tmp$$.ppm; convert $1 $PPM; fi ##Convert input image to PPM pnmcut 0 0 1 1 $PPM |pamtopnm -plain >tmp$$C1plain.ppm ##Isolate the top-left pixel, as a plain-ppm { read; read; read; ##read that plain-text ppm... read -a PIX; ((Red=PIX[0])); ((Green=PIX[1])); ((Blue=PIX[2])); ##get that pixel's color as Red, Green, Blue components } $OF ##create transparent GIF; works but without fuzzy-matching ##-- 2006nov26: METHOD-3A (convert-draw with fuzzy-match): ##convert -fuzz 10% -draw 'matte 0,0 replace' $1 $OF ##create transparent GIF/PNG -- DOES NOTHING (did I misread the documentation??) ##convert -fill none -fuzz 10% -draw 'matte 0,0 replace' $1 $OF ##create transparent GIF/PNG -- does something, but not the fuzzy-matching... ##convert -fuzz 10% -fill none -draw 'matte 0,0 replace' $1 $OF ##create transparent GIF/PNG -- also no fuzzy-matching ==> give up on -matte method ##-- 2006nov26: METHOD-2A (convert-transparent with fuzzy-match): ##convert -fuzz 1% -transparent $color $PPM $OF ##create transparent GIF/PNG -- DOES FUZZY-MATCHING (-fuzz must precede -transparent) ##fuzz:10% fine for most logos; but needed 1% for DonateNow3BA, NathanVadeboncoeur-FigA ##fuzz:1% fuzz:1 both match way too too much on the Debwendon-logo (both std+forDark) ##convert's fuzzy-match uses transitive-closure => is unusable on antialiased img:-( ##-- 2008-04-27: METHOD-1A (with ppmchange fuzzy-matching) ppmto=ppmtogif; if [[ $OF == *png ]];then ppmto=pnmtopng; fi ##handle gif or pgm output if [[ $OF == *png ]];then ppmchange -closeness=12 $color $color $PPM |$ppmto -transparent $color >$OF ##NetPBM fuzzy-matching; skip ppmquant for PNG output else ppmchange -closeness=12 $color $color $PPM |ppmquant -meanpixel 256 |$ppmto -transparent $color >$OF ##NetPBM fuzzy-matching for GIF output fi rm tmp$$*ppm exit ## COMPARISON OF METHODS: ## ====================== ## 1. ppmtogif -transparent $color x.ppm >x.gif ## -- doesn't need ImageMagick; is exact-match only, and no neighbouring-pixels variant; ## -- produces Compressed-GIF! (my RH7 version of ImageMagick-convert didn't) ## ## 2. convert -transparent $color x.ppm x.gif ## -- Variant-A: supply -fuzz NN in order to get fuzzy-matching ## ## 3. convert -draw 'matte 0,0 replace' x.ppm x.gif ## -- Variant-A: supply -fuzz NN in order to get fuzzy-matching; ## -- Variant-B: using "floodfill" in place of "replace" produces a neighbours-only replacement method; ## useful when interior pixels of that color are to retain that colour (usually NOT wanted); ## -- Variant-C: can use "color" in place of "matte" to replace with a specified colour (instead of clear); it is specified as: -fill COLOR ## ## CHANGE-LOG: ## =========== ## 2005-Jul: implemented METHOD-1 (ppmtogif); ## 2006-Nov: when trying photographic-background on the index.htm page, noticed the logo looked hideous on that darker background (way too much near-white); ## WANTED: fuzzy-matching Method-2/3 Variant-A (OpenOffice fine with fuzzy-match:30 on 200x200 logo; convert seems ok with 10% = 25.6 absolute) ## 2007-Apr: respect the filetype-suffix of 2nd param -- so can make a transparent PNG (default remains Inputfile.gif) ## 2007-June: encountered situations where Bottom-Left or Bottom-Right corner was needed to get the right background; ## and where "neighbours only" replacement was needed -- handled them semi-manually... ## 2008-Apr: couldn't get convert's fuzzy-matching to behave on Debwendon logo; deduced it uses transitive-closure on the near-test - altho that's downright stupid!! ## switched to the NetPBM-equivalent; need ppmquant that leaves "dominant" colours unchanged -- with the -meanpixel option it comes fairly close to what I want; ## 2008-Nov: minor changes to tidy up.