13

I need to remove the white background from this image and make the background transparent. So it's just a black tick on the transparent background exported as a png.

e.g. Turn

enter image description here

Into

enter image description here

Any ideas?

mu is too short
  • 426,620
  • 70
  • 833
  • 800
Tom
  • 33,626
  • 31
  • 85
  • 109

3 Answers3

9

convert image.png -matte -fill none -fuzz 1% -opaque white result.png

Replaces anything white with transparency. The fuzz option includes anything that is almost-white.

Shaun
  • 549
  • 3
  • 10
  • @hadees this looks like a good start: [Understanding ImageMagick's convert and translating to Ruby RMagick](http://stackoverflow.com/questions/4132787/understanding-imagemagicks-convert-and-translating-to-ruby-rmagick) – Jeremy Thompson Apr 22 '13 at 01:22
4

I know I am pretty late to the party, but a lot has changed since this question was first posted, so here is how you can do it today using at least version 2.15.4 of rmagick

Assuming you have the image somewhere accessible:

image = Magick::Image.new(path_to_file)
image.background_color = 'none'

If you also want to trim the image so it's only as big as it boundaries, simply use .trim!

image.trim!

EDIT:

Turns out the solution above does not really work for all use cases. A more general solution is this:

# the image needs to be in 'PNG' format
image.format = 'PNG'

# set a fuzz on the image depending on how accurate you want to be
image.fuzz = '10%'

# get the image background color
background_color = image.background_color

# convert pixels based on their color to being transparent
# the fuzz set above controls how accurate the conversion will be
image.paint_transparent(background_color)
1

With v6.8.4-Q16 using the below command:

convert nike.png -matte -fill none -fuzz 1% -opaque white nikeOutput.png

Results in:

enter image description here

Here is the command I use:

convert nike.jpg -transparent white NikeProd.png

enter image description here

enter image description here

Jeremy Thompson
  • 61,933
  • 36
  • 195
  • 321