Created
November 14, 2012 14:53
-
-
Save edvardm/4072560 to your computer and use it in GitHub Desktop.
rmagick sample
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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