teisipäev, 5. september 2017

Convert all JPGs to PNG and replace black frame with transparent

How to convert all number named JPG (like 1/2/3/123.jpg and 3/4/34.jpg) files in directory tree to PNG - replace black background with transparent, but preserve file extension (hold by database):
You need to have imagemagick.

shopt -s globstar
for 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.

Minu blogide loend