Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ImageMagick: How to change transparent background to a color?

Given an input image sample.i.png with a transparent background :

enter image description here

How to convert its background into a file sample.o.png with background rgb(160,160,255), opacity 100% (or 1).


Answer used:

convert source.png -background "rgb(160,160,255)" -flatten out.png 

Please +1 emcconville's answer !

like image 276
Hugolpz Avatar asked Aug 08 '14 16:08

Hugolpz


People also ask

Does PNG support transparency?

The GIF and PNG formats also both support transparency. If you need any level of transparency in your image, you must use either a GIF or a PNG. GIF images (and also PNG) support 1-color transparency. This basically means that you can save your image with a transparent background.

How do I make the background of an element transparent?

To set the opacity of a background, image, text, or other element, you can use the CSS opacity property. Values for this property range from 0 to 1. If you set the property to 0, the styled element will be completely transparent (ie. invisible).


1 Answers

From Mark Random's suggestion. Create a background image with your color, and flatten the source image over it.

 convert -size 150x150 xc:"rgb(160,160,255)" source.png -layers flatten out.png

example

Edit If you don't want to worry about image size, just change the background directly.

convert source.png -background "rgb(160,160,255)" -flatten out.png 

Add the -flatten to re-set the background color

like image 132
emcconville Avatar answered Oct 13 '22 22:10

emcconville