Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

rmagick pixel color value [duplicate]

I am using rmagick to deal with getting a each single pixel of a bitmap. I need to get the values of colors in depth of 8 (256 colors), but unfortunately when i use pixel.[color] (pixel. red for example), i am getting them in depth of 16. It is happening even after i used image.quantize(256).

Here is the code:

require 'RMagick'
include Magick

image = ImageList.new("image.bmp")
image3 = image.quantize(number_colors = 256)
puts image3.number_colors

image2 = Image.new(image.columns, image.rows)

(0..image.columns).each do |x|
    (0..image.rows).each do |y|
        pixel = image3.pixel_color(x, y)

    print pixel.red
    print ", "
    print pixel.green
    print ", "
    print pixel.blue
    print "\n"

    image2.pixel_color(x, y, pixel)
    end
end

What should I to get just values of 0..255?

like image 710
Michal Avatar asked Sep 29 '12 09:09

Michal


1 Answers

They are stored in a 'quantum depth' of 16-bits. You can rebuild the library to change this. Or you can simply divide each value by 257.

There's a function called MagickExportImagePixels which can get you the 8-bit pixel data that you want. Whenever you perform a transformation etc on an image it will get converted back to 16-bit pixels.

like image 104
Bert Avatar answered Sep 28 '22 03:09

Bert