Skip to content

Instantly share code, notes, and snippets.

@awesome
Last active August 29, 2015 13:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save awesome/9964231 to your computer and use it in GitHub Desktop.
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
# 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