Skip to content

Instantly share code, notes, and snippets.

@edvardm
Created November 14, 2012 14:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save edvardm/4072560 to your computer and use it in GitHub Desktop.
Save edvardm/4072560 to your computer and use it in GitHub Desktop.
rmagick sample
require 'RMagick'
ARGV.each do |file|
puts file
orig = Magick::Image::read(file).first
img = orig.quantize(256, Magick::GRAYColorspace)
puts " Format: #{img.format}"
puts " Geometry: #{img.columns}x#{img.rows}"
puts " Class: " + case img.class_type
when Magick::DirectClass
"DirectClass"
when Magick::PseudoClass
"PseudoClass"
end
puts " Depth: #{img.depth} bits-per-pixel"
puts " Colors: #{img.number_colors}"
puts " Filesize: #{img.filesize}"
puts " Resolution: #{img.x_resolution.to_i}x#{img.y_resolution.to_i} "+
"pixels/#{img.units == Magick::PixelsPerInchResolution ?
"inch" : "centimeter"}"
if img.properties.length > 0
puts " Properties:"
img.properties { |name,value|
puts %Q| #{name} = "#{value}"|
}
end
puts "Dumping pixel data"
(0...img.rows).each do |row|
line = (0...img.columns).map do |col|
# while printing grayscale values, normalize data to range 0..255,
# quantization just reduces colorspace to 256 different values
"%4d" % (img.pixel_color(col, row).red / 256)
end.join(',')
puts line
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment