You need to have imagemagick.
shopt -s globstarfor i in **/*[0-9].jpg; do
echo "in: $i"
oname=`echo $i | cut -f1 -d'.'`."png"
echo "out: $oname"
convert $i -fill none -fuzz 5% -draw 'matte 0,0 floodfill' -flop -draw 'matte 0,0 floodfill' -flop -format png $oname
done
find . -name '*.png' -exec sh -c 'mv "$0" "${0%.png}.jpg"' {} \;
5% - can be changed to fit better.