Module: FlickrCli::ImageCutter
- Defined in:
- lib/image_cutter.rb
Constant Summary collapse
- CHARS =
[ 'W', 'M', '$', '@', '#', '%', '^', 'x', '*', 'o', '=', '+',':', '~', '.', ' ' ]
- FONT_ROWS =
8
- FONT_COLS =
4
Class Method Summary collapse
Class Method Details
.convert_to_ascii(file) ⇒ Object
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/image_cutter.rb', line 8 def self.convert_to_ascii(file) img = Magick::Image.read(file).first # Resize too-large images. The resulting image is going to be # about twice the size of the input, so if the original image is too # large we need to make it smaller so the ASCII version won't be too # big. The `change_geometry' method computes new dimensions for an # image based on the geometry argument. The '320x320>' argument says # "If the image is too big to fit in a 320x320 square, compute the # dimensions of an image that will fit, but retain the original aspect # ratio. If the image is already smaller than 320x320, keep the same # dimensions." img.change_geometry('728x728>') do |cols, rows| img.resize!(cols, rows) if cols != img.columns || rows != img.rows end # Compute the image size in ASCII "pixels" and resize the image to have # those dimensions. The resulting image does not have the same aspect # ratio as the original, but since our "pixels" are twice as tall as # they are wide we'll get our proportions back (roughly) when we render. pr = img.rows / FONT_ROWS pc = img.columns / FONT_COLS img.resize!(pc, pr) img = img.quantize(16, Magick::GRAYColorspace) img = img.normalize # Draw the image surrounded by a border. The `view' method is slow but # it makes it easy to address individual pixels. In grayscale images, # all three RGB channels have the same value so the red channel is as # good as any for choosing which character to represent the intensity of # this particular pixel. border = '+' + ('-' * pc) + '+' puts border img.view(0, 0, pc, pr) do |view| pr.times do |i| putc '|' pc.times { |j| putc CHARS[view[i][j].red/16] } puts '|' end end border end |