2011-10-12 108 views

回答

9

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

替换任何白色的透明度。模糊选项包括几乎是白色的任何东西。

+2

你如何将它转换为rmagick? – hadees

+0

@hadees这看起来像一个很好的开始:[了解ImageMagick的转换和翻译为Ruby RMagick](http://stackoverflow.com/questions/4132787/understanding-imagemagicks-convert-and-translating-to-ruby-rmagick) –

1

随着v6.8.4-Q16使用下面的命令:在

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

结果:

enter image description here

这里是我使用命令:

convert nike.jpg -transparent white NikeProd.png 

enter image description here

enter image description here

2

我知道我非常迟到了,但很多已经改变,因为这个问题是第一次公布,所以这里是如何今天可以使用至少rmagick

2.15.4版本假设做你有地方可访问的图像:

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

如果你也想裁剪图像所以才大,因为它的边界,只需使用.trim!

image.trim! 

编辑:

原来上面的解决方案并没有真正适合所有情况的工作。更通用的解决方案是这样的:

# 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)