36.19 Image Transparent

20190303

A common requirement when working with graphics files that you want to include in a colourful document or to place on your web page is to convert its background from white to being transparent. Both GIF and PNG images support transparency but JPG does not. With a transparent background an image will often blend nicely with the web page or presentation background colour.

We can replace the white background of a JPG image with a transparent background to create a PNG image using convert from imagemagick. The key option is -transparent:

$ convert -transparent white orig.jpg trans.png
$ display trans.png

Note that after conversion we display to image to ensure all is as expected.

If the white in the original is not pure, as is often the case, we can introduce some fuzziness to capture more of the background of the image as transparent using -fuzz:

$ convert -transparent white -fuzz 15% orig.jpg trans.png
$ display trans.png

Other colours can be converted to transparent:

$ convert -transparent "#74acea" orig.jpg trans.png
$ display trans.png


Your donation will support ongoing availability and give you access to the PDF version of this book. Desktop Survival Guides include Data Science, GNU/Linux, and MLHub. Books available on Amazon include Data Mining with Rattle and Essentials of Data Science. Popular open source software includes rattle, wajig, and mlhub. Hosted by Togaware, a pioneer of free and open source software since 1984. Copyright © 1995-2022 Graham.Williams@togaware.com Creative Commons Attribution-ShareAlike 4.0