Shell Bash Script to Remove Background From PNG,JPEG Images on Command Line Full Project For Beginners

 

 

script.sh

 

 

background_color=\#e2e2e2
for img in "src"/*
do
	echo "checking $img"
	if [ "${img: -4}" == ".png" ]; then
		name=$(basename $img .png)
		echo "converting $img"
		convert $img -transparent $background_color out/$name-nobg.png
	fi
done

 

 

background-remover-script

bash script to remove background from images

This script uses ImageMagick libraries to convert images.

How to use

To use this script, create a folder “src” where to put all the original images. Note that this works only if the source images are in .png format!

Then create an “out” folder where the converted images will be located. The script will take all the images in src folder, convert them and put into out folder. The two folders need to be at the same level of the script.

Simply run the script and enjoy!

Todos:

  • pass backgorund color as a parameter
  • pass src and out folder as a parameter

Leave a Reply