Minimagick – Retrieves hexadecimal code for given pixel colors

I have reduced resize_to_fill to a size of [1,1], thereby reducing the image to a single pixel containing basically the average color of the entire image (assuming the image has no Big difference, of course).
Now I am trying to retrieve the color of this single pixel in hexadecimal format.

From the terminal window I can run the convert command like this:

p>

convert image.png txt:
# ImageMagick pixel enumeration: 1,1,255,rgb
0,0: (154,135,116) #9A8774 rgb(154,135,116)< /pre>

But I am not sure how to run this command from inside the application in the before_save section of the model to which the image belongs.
Using carrierwave to upload and attach images

So far, I have Retrieved image:

image = MiniMagick::Image.read(File.open(self.image.path))

But I’m not sure How to start here.

You can 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 like Use it like this:

i = MiniMagick::Image.open("/pat h/to/image.png")
puts i.pixel_at(100, 100)

Output:

#34555B

I have reduced resize_to_fill to [1,1] size, thus reducing the image to a single pixel containing basically the average color of the entire image (assuming the image is between height and width There is no big difference, of course).
Now I am trying to retrieve the color of this single pixel in hexadecimal format.

From the terminal window I can run the convert command like this:< /p>

convert image.png txt:
# ImageMagick pixel enumeration: 1,1,255,rgb
0,0: (154,135,116) #9A8774 rgb(154,135,116)

But I am not sure how to run this command from inside the app in the before_save section of the model to which the image belongs.
Using carrierwave to upload and attach images

So far, I The image has been retrieved:

image = MiniMagick::Image.read(File.open(self.image.path))

But I don’t Determine how to start here.

You can 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

Then use it like this:

i = MiniMagick::I mage.open("/path/to/image.png")
puts i.pixel_at(100, 100)

Output:

#34555B

Leave a Comment

Your email address will not be published.