RMagick从图像中删除白色背景并使其透明

我需要从此图像中删除白色背景并使背景透明。 所以它只是透明背景上的黑色刻度,导出为png。

转过来

在此处输入图像描述

在此处输入图像描述

有任何想法吗?

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! 

编辑:

事实certificate上面的解决方案并不适用于所有用例。 更通用的解决方案是:

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

使用以下命令使用v6.8.4-Q16:

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

结果是:

在此处输入图像描述

这是我使用的命令:

 convert nike.jpg -transparent white NikeProd.png 

在此处输入图像描述

在此处输入图像描述