【问题标题】:RMagick remove white background from image and make it transparentRMagick 从图像中删除白色背景并使其透明
【发布时间】:2011-12-06 00:13:57
【问题描述】:

我需要从这张图片中删除白色背景并使背景透明。所以它只是导出为 png 的透明背景上的一个黑色勾号。

例如转

进入

有什么想法吗?

【问题讨论】:

    标签: ruby image imagemagick image-manipulation rmagick


    【解决方案1】:

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

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

    【讨论】:

    【解决方案2】:

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

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

    结果:

    这是我使用的命令:

    convert nike.jpg -transparent white NikeProd.png
    

    【讨论】:

      【解决方案3】:

      我知道我参加聚会已经很晚了,但是自从这个问题首次发布以来发生了很多变化,所以今天你可以使用至少版本 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)
      

      【讨论】:

        猜你喜欢
        • 2011-12-23
        • 2017-05-22
        • 2020-01-13
        • 1970-01-01
        • 2017-02-27
        • 2010-11-02
        • 1970-01-01
        • 2012-09-21
        相关资源
        最近更新 更多