Automatically adding a transparent background with ImageMagick

Posted on Wed 18 January 2017 in misc • 1 min read

Yesterday I had to convert some JPEG images with a white background into a PNG with a transparent background. While it is possible to do this by just replacing white with transparent, this also replaces white in other parts of the image where this is not intended. So the better way is to select all the white regions connected to a certain point, using a fuzzy matcher to avoid leaving fragments. This worked as intended:

convert infile.jpg -fill transparent -fuzz 10% -floodfill +0+0 white outfile.png

From various posts on the ImageMagick forum and Stackoverflow.