How to use ruby ​​to count fractions of pixels that are white in jpeg?

How can I use Ruby to count the fractions of pixels that are white in jpeg?

+4
source share
1 answer

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 
+3
source

All Articles