【问题标题】:Understanding and Converting Ruby's ChunkyPNG to Javascript Equivalent理解和转换 Ruby 的 ChunkyPNG 到 Javascript 等价物
【发布时间】:2019-08-01 09:04:36
【问题描述】:

我正在将一些 RUBY 代码转换为 Javascript。 此 RUBY 代码使用 ChunkyPNG Ruby 库。

这是 Ruby 代码:

def self.from_png(file)
  image = ChunkyPNG::Image.from_file(file)
  mask = Mask.new(image.height, image.width)

  mask.rows.times do |row|
    mask.columns.times do |col|
      if image[col, row] == ChunkyPNG::Color::BLACK
        mask[row, col] = false
      else
        mask[row, col] = true
      end
    end
  end

  mask
end

如何将上面的代码转换成Javascript? 我在转换方面面临的两行挑战是:

image = ChunkyPNG::Image.from_file(file)

if image[col, row] == ChunkyPNG::Color::BLACK

我可以使用什么 Javascript PNG 库来做同样的事情?

是什么

image[col, row]

参考?是RGB中的RG吗?或者它是什么?了解这一点可以帮助我在 Javascript PNG Libraries 中找到等效的 Javascript 方法...

【问题讨论】:

标签: javascript ruby chunkypng pngjs


【解决方案1】:

image[col, row] 指图像在位置 (col, row) 处的特定像素。 图像可以描述为 2D 颜色数组 - 在这种情况下,颜色是 ChunkyPNG::Color 的一个实例或只是一个数字。在您的代码示例中,所有黑色像素在掩码中标记为false,所有非黑色像素都标记为true

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-04-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-07
    • 2011-02-12
    相关资源
    最近更新 更多