Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save peter-leonov/5c9860d7a4a494ffb341 to your computer and use it in GitHub Desktop.
Save peter-leonov/5c9860d7a4a494ffb341 to your computer and use it in GitHub Desktop.
# get hex color of pixel
# implementation of answer here http://stackoverflow.com/questions/8894194/retrieving-the-hex-code-of-the-color-of-a-given-pixel
# solution to 'undefined method for MiniMagick::CommandBuilder in mini_magick'
require 'mini_magick'
module MiniMagick
class Image
def pixel_at(x, y)
run_command("convert", "#{path}[1x1+#{x.to_i}+#{y.to_i}]", 'txt:').split("\n").each do |line|
return $1 if /^0,0:.*(#[0-9a-fA-F]+)/.match(line)
end
nil
end
end
end
# example
image = MiniMagick::Image.open(File.expand_path('~/Desktop/truck.png'))
image.pixel_at(1,1)
#=> "#01A30D"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment