Search code examples
imagemagickrmagick

Read with 8 bits in Rmagick doesn't work correctly?


I have Rmagick using an ImageMagick compiled for 16bit.

However, I am able to read a file by doing this:

file = Magick::Image.read(my_file){self.depth = 8}.first

And when doing an inspect of that file, I see that it is

PNG 1400x768 1400x768+0+0 DirectClass 8-bit 333kb

However, when accessing any of the pixels, the values are bigger than 0-255.

Why is that? What am I missing?

I access it like this:

red = image.pixel_color(x,y).red 

I don't understand why the value would be from 0 to 2^16, when I specifically have opened the image with 8 bits of depth.


Solution

  • The version of ImageMagick you are using is Q16 (16-bit). This means that ImageMagick will always use an unsigned short internally to store a channel of a pixel. You could switch to the Q8 version of ImageMagick if all your images are 8-bit. More info about the architecture of ImageMagick can be found here: http://www.imagemagick.org/script/architecture.php