I have used resize_to_fill down to a [1,1] size thus reducing the image to a single pixel containing what is basically the average color of the entire image (provided the image does not have a huge disparity between height and width, of course). Now I'm attempting to retrieve the color of this single pixel in hex format.
From the terminal window I am able to run the convert command like this:
convert image.png txt:
# ImageMagick pixel enumeration: 1,1,255,rgb
0,0: (154,135,116) #9A8774 rgb(154,135,116)
I am however uncertain of how I could run this command from inside the application during the before_save section of the model that the image belongs to. The image is uploaded and attached using carrierwave
So far I have retrieved the image:
image = MiniMagick::Image.read(File.open(self.image.path))
But I'm not quite certain how to procede from here.
You could add a pixel_at
method like this:
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
And then use it like this:
i = MiniMagick::Image.open("/path/to/image.png")
puts i.pixel_at(100, 100)
Outputs:
#34555B
For recent versions of MiniMagick change escaped_path
to path
like this:
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With