RMagick如何去除图像的白色背景并使其透明化

13
我需要从这张图片中移除白色背景并将其转为透明的背景,这样就只剩下一个黑色勾选标志在透明背景上,导出为png格式。
例如:
enter image description here 转换为 enter image description here 有什么建议吗?
3个回答

9

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

该命令将白色替换为透明。模糊选项还可以包括几乎白色的部分。


@hadees 这看起来是一个不错的开始:理解ImageMagick的convert并转换为Ruby RMagick - Jeremy Thompson

4

我知道我来晚了,但是自从这个问题第一次发布以来,很多事情已经发生变化,所以现在至少使用版本2.15.4rmagick,以下是您今天可以执行的操作:

假设您有可访问的图像:

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)

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


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接