Module: Catpix
- Defined in:
- lib/catpix.rb,
lib/catpix/private.rb,
lib/catpix/version.rb
Overview
Provides a function to print images in the terminal. A range of different formats is supported (check out what ImageMagick supports). Under the hood, this module uses two components:
-
[rmagick](rmagick.github.io/) to read and scale the images and
-
[tco](github.com/pazdera/tco) to map their pixels on the extended colour palette in the terminal.
Some other minor features like centering and handling background colours are supplied directly by this module.
Constant Summary collapse
- VERSION =
Version of the gem
"0.1.0"
Class Method Summary collapse
-
.print_image(path, options = {}) ⇒ Object
Print an image to the terminal.
Class Method Details
.print_image(path, options = {}) ⇒ Object
Print an image to the terminal.
All formats supported by ImageMagick are supported. The image’s colours will be mapped onto the extended 256 colour palette. Also by default, it will be scaled down to fit the width of the terminal while keeping its proportions. This can be changed using the ‘options` parameter.
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/catpix.rb', line 41 def self.print_image(path, ={}) = .merge! img = load_image path resize! img, [:limit_x], [:limit_y] margins = get_margins img, [:center_x], [:center_y] margin_colour = [:bg_fill] ? [:bg] : nil print_vert_margin margins[:top], margin_colour # print left margin for the first row print_horiz_margin margins[:left], margin_colour # print the image img.each_pixel do |pixel, col, row| if pixel.opacity == 65535 print_pixel [:bg] else print_pixel get_normal_rgb pixel end if col >= img.columns - 1 print_horiz_margin margins[:right], margin_colour puts unless row == img.rows - 1 print_horiz_margin margins[:left], margin_colour end end end print_vert_margin margins[:bottom], margin_colour end |