Check if image is colorized
path = '/Users/kai/Downloads/color_or_colorized/color/*'
path = '/Users/kai/Downloads/color_or_colorized/colorized_or_grayscale/*'
# path = '/Users/kai/Downloads/color_or_colorized/tricky/*'
Dir[path].each do |file_path|
image = ::Magick::Image::read(file_path).first.resize(500, 500).quantize(256, Magick::RGBColorspace)
histogram = image.color_histogram
average_hue = 0
average_hue_deviation = 0
average_saturation = 0
average_saturation_deviation = 0
total_values = 0
# Calculate average hue and saturation
histogram.each do |color,n|
average_hue += n * color.to_hsla[0]
average_saturation += n * color.to_hsla[1]
total_values += n
end
average_hue /= total_values
average_saturation /= total_values
# calculate average deviation from average hue and saturation.
histogram.each do |color,n|
average_hue_deviation += n * (average_hue - color.to_hsla[0]).abs
average_saturation_deviation += n * (average_saturation - color.to_hsla[1]).abs
end
average_hue_deviation /= total_values
average_saturation_deviation /= total_values
puts "--------"
puts file_path
puts "#{average_hue}, #{average_hue_deviation}"
puts "#{average_saturation}, #{average_saturation_deviation}"
image.destroy!
end