Last active
August 29, 2015 13:58
-
-
Save awesome/9964231 to your computer and use it in GitHub Desktop.
get hex color of pixel; implementation of answer here http://stackoverflow.com/a/8894580/1076207; solution to MiniMagick::CommandBuilder error in Rails 3.2.x
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/a/8894580/1076207 | |
# solution to MiniMagick::CommandBuilder error in Rails 3.2.x | |
# config/application.rb | |
module AwesomeAppName | |
class Application < Rails::Application | |
config.after_initialize do | |
require Rails.root.join('lib', 'gem_ext.rb') | |
end | |
end | |
end | |
# lib/gem_ext.rb | |
require "gem_ext/mini_magick" | |
# lib/gem_ext/mini_magick.rb | |
require "gem_ext/mini_magick/image" | |
# lib/gem_ext/mini_magick/image.rb | |
module MiniMagick | |
class Image | |
def pixel_at(x, y) | |
case run_command("convert", "#{escaped_path}[1x1+#{x}+#{y}]", "-depth 8", "txt:").split("\n")[1] | |
when /^0,0:.*(#[\da-fA-F]{6}).*$/ then $1 | |
else nil | |
end | |
end | |
end | |
end | |
# example | |
#$ rails console | |
#Loading development environment (Rails 3.2.13) | |
image = MiniMagick::Image.open(File.expand_path('~/Desktop/truck.png')) | |
#=> #<MiniMagick::Image:0x007f9bb8cc3638 @path="/var/folders/1q/fn23z3f11xd7glq3_17vhmt00000gp/T/mini_magick20140403-1936-phy9c9.png", @tempfile=#<File:/var/folders/1q/fn23z3f11xd7glq3_17vhmt00000gp/T/mini_magick20140403-1936-phy9c9.png (closed)>> | |
image.pixel_at(1,1) | |
#=> "#01A30D" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment