How would I use ruby to count the fractions of pixels that are white in a jpeg?
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
精彩评论