Module: Catpix

Defined in:
lib/funny_terminal/catpix.rb,
lib/funny_terminal/catpix/private.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:

Some other minor features like centering and handling background colours are supplied directly by this module.

Class Method Summary collapse

Class Method Details

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.

Options Hash (options):

  • :limit_x (Float)

    A factor of the terminal window’s width. If present, the image will be scaled down to fit (proportions are kept). Using 0 will disable the scaling. [default: 1.0]

  • :limit_y (Float)

    A factor of the terminal window’s height. If present, the image will be scaled down to fit (proportions are kept). Using 0 will disable the scaling. [default: 0]

  • :center_x (Boolean)

    Center the image horizontally in the terminal window. [default: false]

  • :center_y (Boolean)

    Center the image vertically in the terminal window. [default: false]

  • :bg (String)

    Background colour to use in case there are any fully transparent pixels in the image. This can be a RGB value ‘#c0ffee’ or a tco alias ‘red’ or ‘blue’. [default: nil]

  • :bg_fill (Boolean)

    Fill the margins around the image with background colour. [default: false]

  • :resolution (String)

    Determines the pixel size of the rendered image. Can be set to ‘high`, `low` or `auto` (default). If set to `auto` the resolution will be picked automatically based on your terminal’s support of unicode.



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/funny_terminal/catpix.rb', line 49

def self.print_image(path, options={})
  options = default_options.merge! options

  if options[:resolution] == 'auto'
    options[:resolution] = can_use_utf8? ? 'high' : 'low'
  end
  @@resolution = options[:resolution]

  img = load_image path
  resize! img, options[:limit_x], options[:limit_y]

  margins = get_margins img, options[:center_x], options[:center_y]
  margins[:colour] = options[:bg_fill] ? options[:bg] : nil

  if high_res?
    do_print_image_hr img, margins, options
  else
    do_print_image_lr img, margins, options
  end
end


70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/funny_terminal/catpix.rb', line 70

def self.print_image_url(url)
  options = {
    resolution: 'auto',
    limit_x: 0.5,
    limit_y: 0,
    center_x: true,
    center_y: true,
    bg: "white",
    bg_fill: true,
  }

  if options[:resolution] == 'auto'
    options[:resolution] = can_use_utf8? ? 'high' : 'low'
  end
  @@resolution = options[:resolution]

  img = load_image_from_url url

  resize! img, options[:limit_x], options[:limit_y]

  margins = get_margins img, options[:center_x], options[:center_y]
  margins[:colour] = options[:bg_fill] ? options[:bg] : nil

  if high_res?
    do_print_image_hr img, margins, options
  else
    do_print_image_lr img, margins, options
  end
end