Print

Removing Image Backgrounds via Command Line

If you need to remove white backgrounds from JPEG images then you will need to follow two steps using the linux convert command. Use the first command to change the images to png files and add an alpha channel for transparency.

  1. find /home/account/public_html/images -type f -name "*.jpg" -print0 | while IFS= read -r -d $'' file; do convert -verbose "$file" -channel rgba -alpha set "$file.png"; done
find /home/account/public_html/images -type f -name "*.jpg" -print0 | while IFS= read -r -d $'' file; do convert -verbose "$file" -channel rgba -alpha set "$file.png"; done


Then, use this command to replace white area with transparent. The fuzz variable determines to what degree pixels with a slightly darker pigment will be effected.

  1. find /home/account/public_html/images -type f -name "*.png" -print0 | while IFS= read -r -d $'' file; do convert -verbose "$file" -fuzz 5% -transparent white "$file"; done
find /home/account/public_html/images -type f -name "*.png" -print0 | while IFS= read -r -d $'' file; do convert -verbose "$file" -fuzz 5% -transparent white "$file"; done


The commands require installation of ImageMagick for utilization of the "convert" command.
  • 0 Users Found This Useful
Was this answer helpful?