我需要从这张图片中移除白色背景并将其转为透明的背景,这样就只剩下一个黑色勾选标志在透明背景上,导出为png格式。
例如:
将
转换为
有什么建议吗?
例如:
将
![enter image description here](https://istack.dev59.com/WYAWe.webp)
![enter image description here](https://istack.dev59.com/obv1J.webp)
convert image.png -matte -fill none -fuzz 1% -opaque white result.png
该命令将白色替换为透明。模糊选项还可以包括几乎白色的部分。
我知道我来晚了,但是自从这个问题第一次发布以来,很多事情已经发生变化,所以现在至少使用版本2.15.4
的rmagick
,以下是您今天可以执行的操作:
假设您有可访问的图像:
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)
convert nike.png -matte -fill none -fuzz 1% -opaque white nikeOutput.png
convert nike.jpg -transparent white NikeProd.png