开发者

How would I use ruby to count the fractions of pixels that are white in a jpeg?

开发者 https://www.devze.com 2023-03-03 05:21 出处:网络
How would I use Ruby to count the fractions of pixels th开发者_StackOverflow社区at are white in a jpeg?You can use RMagick Gem

How would I use Ruby to count the fractions of pixels th开发者_StackOverflow社区at are white in a jpeg?


You can use RMagick Gem http://rmagick.rubyforge.org/

require 'RMagick'
include Magick

image_list = ImageList.new("file_name.jpg")
image = image_list.first
white_pixels_count = 0
image.each_pixel do |j|
  if j.red == 255 && j.green == 255 && j.blue == 255
    white_pixels_count += 1
  end
end

puts image.columns     #height
puts image.rows        #width
puts white_pixels_count
0

精彩评论

暂无评论...
验证码 换一张
取 消