【问题标题】:Retrieving the hex code of the color of a given pixel检索给定像素颜色的十六进制代码
【发布时间】:2012-01-17 11:56:45
【问题描述】:

我已经使用 resize_to_fill 缩小到 [1,1] 大小,从而将图像缩小到包含基本上是整个图像的平均颜色的单个像素(假设图像在高度和宽度之间没有巨大的差异,当然)。 现在我正在尝试以十六进制格式检索这个单个像素的颜色。

从终端窗口我可以像这样运行转换命令:

convert image.png txt:
# ImageMagick pixel enumeration: 1,1,255,rgb
0,0: (154,135,116) #9A8774 rgb(154,135,116)

但是,我不确定如何在图像所属模型的 before_save 部分从应用程序内部运行此命令。 图片是使用carrierwave上传和附加的

到目前为止,我已经检索到图像:

image = MiniMagick::Image.read(File.open(self.image.path))

但我不太确定如何从这里开始。

【问题讨论】:

    标签: minimagick


    【解决方案1】:

    您可以像这样添加pixel_at 方法:

    module MiniMagick
      class Image
        def pixel_at(x, y)
          case run_command("convert", "#{escaped_path}[1x1+#{x}+#{y}]", "-depth 8", "txt:").split("\n")[1]
          when /^0,0:.*(#[\da-fA-F]{6}).*$/ then $1
          else nil
          end
        end
      end
    end
    

    然后像这样使用它:

    i = MiniMagick::Image.open("/path/to/image.png")
    puts i.pixel_at(100, 100)
    

    输出:

    #34555B
    

    【讨论】:

    • 酷。我现在确定如果你在图像之外戳或者如果有 alpha 通道会怎么样。所以请注意:)
    • 我认为首先将图像缩小到 1px x 1px 大小应该可以消除这个问题,但对于另一个实现绝对值得牢记。
    • 什么是 escaped_pa​​th? 〜我实际上正在尝试这个并得到undefined method `escaped_path' for #<MiniMagick::CommandBuilder:0x8dc9e68 @tool="mogrify", @args=[]> (NoMethodError)
    • 这是MiniMagick::Image 中的一个实例方法,返回图像的shell 转义路径,请参阅github.com/probablycorey/mini_magick/blob/master/lib/… 但是根据您的错误消息,看起来某些代码在@ 上或@ 内调用escaped_path 987654330@ 实例。
    • Rails 3.2.x这个答案的实现和MiniMagick::CommandBuilder的解决方法错误:gist.github.com/awesome/…
    【解决方案2】:

    对于 MiniMagick 的最新版本,将 escaped_path 更改为 path,如下所示:

    module MiniMagick
      class Image
        def pixel_at x, y
          run_command("convert", "#{path}[1x1+#{x.to_i}+#{y.to_i}]", 'txt:').split("\n").each do |line|
            return $1 if /^0,0:.*(#[0-9a-fA-F]+)/.match(line)
          end
          nil
        end
      end
    end
    

    【讨论】:

      【解决方案3】:

      要与 Rails 4 一起使用,代码需要稍有不同:

      # config/application.rb
      
      module AwesomeAppName
        class Application < Rails::Application
          config.after_initialize do
            require Rails.root.join('lib', 'gem_ext.rb')
          end
        end
      end
      
      # lib/gem_ext.rb
      require "gem_ext/mini_magick"
      
      # lib/gem_ext/mini_magick.rb
      require "gem_ext/mini_magick/image"
      
      # lib/gem_ext/mini_magick/image.rb
      module MiniMagick
        class Image
          def pixel_at(x, y)
            case run_command("convert", "#{path}[1x1+#{x}+#{y}]", "-depth", '8', "txt:").split("\n")[1]
            when /^0,0:.*(#[\da-fA-F]{6}).*$/ then $1
            else nil
            end
          end
        end
      end
      
      # example
      #$ rails console
      image = MiniMagick::Image.open(File.expand_path('~/Desktop/truck.png'))
      #=> #<MiniMagick::Image:0x007f9bb8cc3638 @path="/var/folders/1q/fn23z3f11xd7glq3_17vhmt00000gp/T/mini_magick20140403-1936-phy9c9.png", @tempfile=#<File:/var/folders/1q/fn23z3f11xd7glq3_17vhmt00000gp/T/mini_magick20140403-1936-phy9c9.png (closed)>>
      image.pixel_at(1,1)
      #=> "#01A30D"
      

      【讨论】:

        猜你喜欢
        • 2011-10-25
        • 2017-09-26
        • 2011-09-20
        • 2017-10-01
        • 2014-05-02
        • 2019-11-28
        • 1970-01-01
        • 2017-12-05
        • 1970-01-01
        相关资源
        最近更新 更多