terminal-image
Display images in the terminal
Last updated 3 months ago by sindresorhus .
MIT · Repository · Bugs · Original npm · Tarball · package.json
$ cnpm install terminal-image 
SYNC missed versions from official npm registry.

terminal-image Build Status

Display images in the terminal

Works in any terminal that supports colors.

In iTerm, the image will be displayed in full resolution, since iTerm has special image support.

Install

$ npm install terminal-image

Usage

const terminalImage = require('terminal-image');

(async () => {
	console.log(await terminalImage.file('unicorn.jpg'));
})();

Optionally, you can specify the height and/or width to scale the image. That can be either the percentage of the terminal window or number of rows and/or columns. Please note that the image will always be scaled to fit the size of the terminal. If width and height are not defined, by default the image will take the width and height of the terminal.

It is recommended to use the percentage option.

const terminalImage = require('terminal-image');

(async () => {
	console.log(await terminalImage.file('unicorn.jpg', {width: '50%', height: '50%'}));
})();

You can set width and/or height as columns and/or rows of the terminal window as well.

const terminalImage = require('terminal-image');

(async () => {
	console.log(await terminalImage.file('unicorn.jpg', {width: 50}));
})();

By default, aspect ratio is always maintained. If you don't want to maintain aspect ratio, set preserveAspectRatio to false. However, your image will be scaled to fit the size of the terminal.

const terminalImage = require('terminal-image');

(async () => {
	console.log(await terminalImage.file('unicorn.jpg', {width: 70, height: 50, preserveAspectRatio: false}));
})();

API

Supports PNG and JPEG images. Animated GIFs are also supported with .gifBuffer and .gifFile.

terminalImage.buffer(imageBuffer, options?)

terminalImage.file(filePath, options?)

terminalImage.gifBuffer(imageBuffer, options?)

terminalImage.gifFile(filePath, options?)

Returns a Promise<string> with the ansi escape codes to display the image.

options

Type: object

height

Type: string | number

Custom image height.

Can be set as percentage or number of rows of the terminal. It is recommended to use the percentage options.

width

Type: string | number

Custom image width.

Can be set as percentage or number of columns of the terminal. It is recommended to use the percentage options.

preserveAspectRatio

Type: boolean
Default: true

Whether to maintain image aspect ratio or not.

maximumFrameRate

Only works for terminalImage.gifBuffer or terminalImage.gifFile

Type: number
Default: 30

Maximum framerate to render the GIF. This option is ignored when using iTerm.

renderFrame

Only works for terminalImage.gifBuffer or terminalImage.gifFile

Type: (text: string) => void
Default: log-update

Custom handler which is run for each frame of the GIF.

This can be set to change how each frame is shown.

renderFrame.done

Only works for terminalImage.gifBuffer or terminalImage.gifFile

Type: () => void
Default: log-update

Custom handler which is run when the animation playback is stopped.

This can be set to perform a cleanup when playback has finished.

Tip

Display a remote image

const terminalImage = require('terminal-image');
const got = require('got');

(async () => {
	const body = await got('https://sindresorhus.com/unicorn').buffer();
	console.log(await terminalImage.buffer(body));
})();

Related

Current Tags

  • 1.2.1                                ...           latest (3 months ago)

9 Versions

  • 1.2.1                                ...           3 months ago
  • 1.2.0                                ...           3 months ago
  • 1.1.0                                ...           4 months ago
  • 1.0.1                                ...           6 months ago
  • 1.0.0                                ...           6 months ago
  • 0.2.0                                ...           2 years ago
  • 0.1.2                                ...           2 years ago
  • 0.1.1                                ...           2 years ago
  • 0.1.0                                ...           2 years ago
Maintainers (1)
Downloads
Today 1
This Week 3
This Month 19
Last Day 0
Last Week 10
Last Month 50
Dependencies (5)
Dev Dependencies (4)
Dependents (206)

Copyright 2014 - 2016 © taobao.org |