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

时间:2022-11-22 09:01:22

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.

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

e.g. Turn

例如转

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

Into

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

Any ideas?

有任何想法吗?

3 个解决方案

#1


9  

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

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.

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

#2


2  

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

我知道我参加派对的时间已经很晚了,但是自从这个问题首次发布以来已经发生了很多变化,所以今天至少使用2.15.4版本的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!

如果您还想修剪图像,使其仅与边界一样大,只需使用.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)

#3


1  

With v6.8.4-Q16 using the below command:

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

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

Results in:

结果是:

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

Here is the command I use:

这是我使用的命令:

convert nike.jpg -transparent white NikeProd.png

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

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

#1


9  

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

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.

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

#2


2  

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

我知道我参加派对的时间已经很晚了,但是自从这个问题首次发布以来已经发生了很多变化,所以今天至少使用2.15.4版本的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!

如果您还想修剪图像,使其仅与边界一样大,只需使用.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)

#3


1  

With v6.8.4-Q16 using the below command:

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

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

Results in:

结果是:

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

Here is the command I use:

这是我使用的命令:

convert nike.jpg -transparent white NikeProd.png

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

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