Forked from awesome/minimagick-pixel-at-get-hex-color.rb
Last active
August 29, 2015 14:07
-
-
Save peter-leonov/5c9860d7a4a494ffb341 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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