Hello,
I finally got RMagick working on my OS X machine and I'm trying to use it to accomplish some things that I was previously doing with ImageMagick through the %x mechanism. Specifically, I want to read in an image and spit out a list of individual rgb values. This seems easy enough:
@sourceimage=Image::read(@filename).first
@sourceimage.get_pixels(0, 0, @sourceimage.columns, @sourceimage.rows)
What I really want, though, is 8-bit color values (0-255) rather than the 16-bit values (0-65535) that get spit out. Does anyone know how to go about this? I feel like it must be possible, but I've been back and forth through the documentation and can't figure it out. Any help would be greatly appreciated.
Thanks,
miek
I happen to be doing something similar right now. Here's some code that
should show one way to do it... This prints out the 8-bit pixel values
for any image(s) passed on the command-line. (Just chopping off the low
order byte...)
Example: ruby foo.rb images/*.png
- --------------------------------
require 'RMagick'
imageList = Magick::ImageList.new(*ARGV)
imageList.each do |image|
puts "Bits Per Pixel: #{image.depth}"
view = image.view(0, 0, image.columns, image.rows)
0.upto(view.width) do |x|
0.upto(view.height) do |y|
red = view[y].red >> 8
green = view[y].green >> 8
blue = view[y].blue >> 8
puts "(#{red}, #{green}, #{blue})"
end
end
end
- -------------------------------
Peace,
Jeff
Michael Leonard wrote:
ยทยทยท
Hello,
I finally got RMagick working on my OS X machine and I'm trying to use
it to accomplish some things that I was previously doing with
ImageMagick through the %x mechanism. Specifically, I want to read in an
image and spit out a list of individual rgb values. This seems easy enough:
@sourceimage=Image::read(@filename).first
@sourceimage.get_pixels(0, 0, @sourceimage.columns, @sourceimage.rows)
What I really want, though, is 8-bit color values (0-255) rather than
the 16-bit values (0-65535) that get spit out. Does anyone know how to
go about this? I feel like it must be possible, but I've been back and
forth through the documentation and can't figure it out. Any help would
be greatly appreciated.
Thanks,
miek
For what it's worth:
If you don't mind recompiling ImageMagick, you can set the option
--with-quantum-depth=8. It's recommended to just use the 8 bit
processing if you're not doing work that needs the extra depth. It
has the added benefit of making your processing faster.
- Dimitri
That worked perfectly. Thank you very much. I was unaware of the ">>"
operator.