unhappychoice/terminal_image
关于
:desktop_computer: Show high resolution images on your terminal !
设计与媒体实测星标增长
- 24小时
- 0
- 7天
- 0
- 15天
- 0
- 1个月
- 正在收集历史
各周期内的星标净变化,周期相互重叠,并非每日新增。缺失的测量保持空白。星标代表关注度,不代表软件质量。
README.md
原文TerminalImage
TerminalImage is a library to show images on terminals. TerminalImage supports iTerm2, sixel via libsixel, and 24-bit ANSI colors (see Fallback). Support for Kitty is unreleased.

Installation
To install from RubyGems, add this line to your application's Gemfile. RubyGems version 1.1.0 does not support Kitty.
gem 'terminal_image'
To use Kitty graphics and the size: option, install from master with this Git dependency
in your Gemfile and run bundle install.
gem 'terminal_image', github: 'unhappychoice/terminal_image', branch: 'master'
For sixel output, install libsixel's img2sixel command on the host running Ruby.
Usage
See the getting started guide for a first walkthrough.
Command line
The CLI is unreleased. With Git and Ruby 3.3 or later, build and install the gem from
master to use the executable with --size.
git clone --branch master https://github.com/unhappychoice/terminal_image.git
cd terminal_image
gem build terminal_image.gemspec
gem install ./terminal_image-1.1.0.gem
Make sure the EXECUTABLE DIRECTORY shown by gem environment is on PATH. Then pass a local image path or an HTTP(S) URL.
terminal_image --help
terminal_image --size 40x12 images/sample.png
terminal_image https://raw.githubusercontent.com/unhappychoice/terminal_image/master/images/sample.png
Once a RubyGems release includes the CLI and --size, install it with gem install terminal_image.
To run the CLI directly from a checkout of master, run bundle install in the checkout and use:
bundle exec exe/terminal_image --help
bundle exec exe/terminal_image --size 40x12 images/sample.png
bundle exec exe/terminal_image https://raw.githubusercontent.com/unhappychoice/terminal_image/master/images/sample.png
The command writes previews to stdout and reports errors on stderr with a nonzero exit status.
Ruby
# Print image by File object
TerminalImage.show(File.open('your-image-path.png'))
# Print image from a trusted URL
TerminalImage.show_url('https://raw.githubusercontent.com/unhappychoice/terminal_image/master/images/sample.png')
# Get encoded string ready to be displayed
string = TerminalImage.encode(File.open('your-image-path.png'))
# Get encoded string ready to be displayed from a trusted URL
string = TerminalImage.encode_url('https://raw.githubusercontent.com/unhappychoice/terminal_image/master/images/sample.png')
Pass only trusted URLs to TerminalImage.show_url and TerminalImage.encode_url. Both methods make network requests from the calling process before inspecting the image, so they can reach destinations accessible only from that host.
If your application accepts untrusted URLs, use a downloader that enforces your destination policy before sending the initial request and before following each redirect. Pass the downloaded file to TerminalImage.show or TerminalImage.encode. Validating the image response alone cannot enforce the destination policy because the request has already been sent.
When using the iTerm2 backend, encode, show, and their URL variants raise TerminalImage::InvalidImage
(a subclass of TerminalImage::UnsupportedFormat) when the image width or height
cannot be determined. In that case, show and show_url write nothing to stdout.
In the unreleased version, both URL methods allow 30 seconds for the entire download, including redirects.
Pass download_timeout: 10 to set a different deadline in seconds. The value must
be finite and positive. If the deadline expires, the methods raise
Timeout::Error before rendering.
show_url and encode_url accept HTTP and HTTPS responses up to 10 MiB.
Larger responses raise TerminalImage::ImageTooLarge before rendering.
Preview dimensions
Pass --size COLSxROWS on the command line or size: [columns, rows] to any of the
four Ruby methods. Both limits must be positive integers. For example:
File.open('screenshot.png', 'rb') { |file| TerminalImage.show(file, size: [40, 12]) }
Sized previews fit within both limits, preserve the aspect ratio to the nearest pixel, and only scale down. ANSI uses two image pixel rows per terminal row and also respects the detected terminal width. Native graphics use pixel dimensions reported by the terminal. If that information is unavailable, a sized preview uses ANSI with the format requirements described in Fallback.
Kitty
Kitty support is unreleased. Use the Git dependency in Installation to enable it.
PNG images use the Kitty graphics protocol automatically when TERM=xterm-kitty and neither TMUX nor STY is set to a nonempty value, even when TERM_PROGRAM=iTerm.app. The PNG is sent inline without external tools and is resized in Ruby when it exceeds a requested size.
Fallback
All four methods accept a sixel: keyword. For a known destination, pass sixel: false to select ANSI
or sixel: true to select sixel when img2sixel is available, with ANSI as the fallback.
When omitted or set to nil, the library tries Kitty for PNG images under the conditions above, then iTerm2
when TERM_PROGRAM is iTerm.app, then sixel if img2sixel is available, then ANSI.
ANSI output uses 24-bit ("truecolor") colors.
Each character cell represents two vertically adjacent pixels. When the top pixel is visible, an upper half block (▀)
uses the top pixel as its foreground color and the bottom pixel as its background color if the bottom pixel is visible.
When only the bottom pixel is visible, a lower half block (▄) uses the bottom pixel as its foreground color.
When neither pixel is visible, the cell is a space. Pixels with alpha below 128 (on a 0–255 scale) use the
terminal's default background.
The image is scaled down to the terminal width (IO.console, then $COLUMNS, then 80 columns) and is never scaled up.
The ANSI fallback raises TerminalImage::ImageTooLarge if the source image exceeds 50 million pixels (width × height)
before resizing, even if the resized image would fit.
Supported formats:
- PNG is decoded with the pure-Ruby chunky_png gem and needs nothing else.
- JPEG, GIF, and WebP are converted to PNG with the ImageMagick CLI
(
magick, orconvertfor ImageMagick 6) when it is installed. Only the first frame of an animated image is shown. Without ImageMagick they raiseTerminalImage::UnsupportedFormat, a subclass ofTerminalImage::UnsupportedTerminal. Conversion also raises this error when the bundled security policy cannot be verified, including on Windows and when a per-user ImageMagick policy is configured. Each ImageMagick version probe has a 2-second deadline. A version probe that times out is treated as unavailable. Policy verification has the same deadline. Conversion has a 10-second deadline and raisesTerminalImage::ConversionTimeout, a subclass ofTerminalImage::UnsupportedFormat, if the deadline expires. - Other formats raise
TerminalImage::UnsupportedFormatin the ANSI fallback.
Contributing
Bug reports and pull requests are welcome on GitHub at https://github.com/unhappychoice/terminal_image. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the Contributor Covenant code of conduct.
License
The gem is available as open source under the terms of the MIT License.
Code of Conduct
Everyone interacting in the TerminalImage project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the code of conduct.
Author
探索相关项目
加载中…