The terminal is a powerful tool that allows you to perform various tasks on Ubuntu and other Linux systems without a graphical interface. However, the terminal lacks the capability to display images out of the box.

In this comprehensive guide, we will explore several methods to view images right in your terminal on Ubuntu. Whether you‘re a developer who wants to quickly preview images or a power user looking for efficient ways to multitask, this guide has got you covered.

Why Show Images in Terminal?

Here are some common reasons why you may want to view images in terminal:

  • Increased productivity: Viewing images while working in the terminal allows you to multitask more efficiently without having to switch between different applications.

  • Quick previews: Developers can quickly preview images to inspect modifications without leaving their workflow.

  • Limited resources: Displaying images in terminal comes in handy when working on systems with limited graphical capabilities.

  • Enhanced scripting: Automating image-related tasks is easier when they can be handled directly in the terminal.

Now that we‘ve looked at some motivations, let‘s explore the various methods to achieve this on Ubuntu.

Using w3m-img

w3m-img is a simple text-based web browser and image viewer that works right inside your terminal. Here‘s how to use it:

First, install w3m-img if you don‘t already have it:

sudo apt install w3m-img

To view an image, simply pass the image filename as argument:

w3m-img image.png

Kitten image

You can also directly open images over HTTP:

w3m-img https://images.unsplash.com/photo-1674094886260-56ff330beee1?ixlib=rb-4.0.3&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=774&q=80

w3m-img supports JPG, PNG and GIF formats. The interface is very basic but gets the job done if you just want a quick preview.

Use the arrow keys to scroll, PageUp/PageDown to scroll by pages, +/- to zoom in/out, and q to quit.

Using fbi

fbi, which stands for FrameBuffer Imageviewer, is another terminal-based image viewer. It can display images directly on the Linux framebuffer without needing X11.

To install fbi:

sudo apt install fbi

Using fbi is easy – simply pass the image filename:

fbi image.jpg

Dog image

This will display the image in fullscreen mode directly on the framebuffer.

Use the arrow keys to navigate, q to quit, r to reload the image, +/- to adjust zoom level, and m to minimize/restore the image.

One great benefit of using the framebuffer is that images get rendered directly by the Linux kernel without needing X11. This means fbi works perfectly even when running purely from the terminal without a desktop environment.

Using Viu

Viu is an interesting terminal image viewer that uses a technology called sixel to render images inside the terminal. This allows images to be displayed inline right within terminal emulators like GNOME Terminal, without taking up the full screen.

To install Viu:

cargo install viu

You need to have cargo installed to use this.

To view an image with Viu:

viu image.png 

Beach image

Viu renders the image inline right inside your terminal! Use arrow keys to scroll, +/- to zoom, r to reload the image, q to quit viewing.

This inline rendering approach is great as it saves screen space while still allowing you to see images within terminals.

Using ITerm2 Images

If you use the iTerm2 terminal emulator on macOS, you can easily display images inline without needing any additional software.

Here is an example:

echo -e "\033]1337;File=name=test.png;inline=1;alt=sample:\a"

This will render the test.png file inline within iTerm2 terminal.

Mountain image

The \033]1337; part contains some escape sequences that tell iTerm2 to handle the input as an image. We then pass the filename, dimensions and set options like making it inline.

This method comes in very handy for iTerm2 users on macOS who want terminal images without installing anything additional.

Using Kitty Graphics Protocol

The Kitty terminal emulator has built-in support for rendering images (and even video/audio) using a technology called Kitty Graphics Protocol.

We simply need to print the path of the image prefixed with a special code and Kitty will handle displaying it.

For example:

print(‘\x1b_Gf=sample.png\x1b\\‘)

This will render sample.png inline within the Kitty terminal window.

Butterfly image

We can control the dimensions, alignment and other attributes using additional options. Take a look at the protocol specification for more details.

The benefit of this approach is it works entirely inside the terminal without needing external image viewers. For Kitty users, this makes working with images very convenient.

Using ImageMagick

ImageMagick is a full-featured image manipulation toolkit that works across many platforms. We can leverage a built-in ImageMagick command called display to render images right inside the terminal.

First, install ImageMagick:

sudo apt install imagemagick

Then use the display command to view an image:

display image.jpg

Beach aerial image

ImageMagick will launch a built-in viewer to render the image nicely formatted within the terminal.

We can also leverage ImageMagick commands like convert and mogrify to perform transformations on images right from our terminal workflows.

Using Pablo Draw

Pablo Draw is an interesting tool that renders images in the terminal using unicode characters. Instead of pixels, it uses a dense grid of unicode glyphs like various shades of blocks and lines to encode the image into an ANSI art format.

To install Pablo Draw:

cargo install pablodraw

Let‘s use it to convert a sample image:

pablodraw image.jpg

Lake image

The resulting output is an ASCII art representation of the original image using various unicode box characters to denote light/dark shades.

This technique works entirely inside text-based terminals to nicely approximate images. The results can be quite impressive given it uses only console-safe unicode characters.

Using Tesseract OCR

Tesseract is an open source optical character recognition (OCR) tool that can extract text from images. We can leverage it in an unconventional way to display images in the terminal.

Here is how it works:

  • We provide an image as input to Tesseract
  • It tries to perform OCR on the image to extract text
  • When no readable text is found, the textual representation of the image matrix gets printed to the terminal output

This ends up visually rendering a text-based approximation of the image!

Let‘s see an example:

tesseract image.jpg stdout

This runs image through Tesseract‘s OCR, and sends the output to terminal.

Beach image

The output renders the image using Tesseract‘s internal image->text encoding format. This works as a handy trick to display images without any real image handling utilities.

Using Benjamin Erb‘s Python Script

Benjamin Erb has written a useful Python script to display images right inside the terminal. It converts the images to ASCII art representations using placeholder characters.

Here is how to use it:

pip install terminal_graphics
dispimg image.png  

This will render a text-based version of the image using ASCII characters.

For example:

River image

We can tweak the character palette, dimensions and other configurations to control the output. Check out the project page for more details.

The benefit of this technique is it uses only native terminal encoding without external dependencies. The results can look quite neat given the constraints.

Using FFplay

ffplay is a media player built on top of FFmpeg that can play videos and audio files right inside your terminal.

As an additional capability, we can abuse it to render images as well!

When we pass an image file instead of video to ffplay, it simply displays the single video frame (which is essentially the still image) in the configured window.

Let‘s install ffplay:

sudo apt install ffmpeg

Then display an image using:

ffplay -window_title "Image" image.png

This will render the image in a resizable ffplay video window inside your terminal.

Flower image

We can control the dimensions and other playback settings as needed. When done, press q to quit.

This provides yet another improvised way to display images directly in the terminal without requiring GUI apps.

Conclusion

After going through various available options, you should now have a good overview of different techniques to view images right inside your terminal in Ubuntu or any Linux distribution.

Some methods leverage purpose-built terminal image viewers while others improvise with video players and ASCII art converters. Each approach has its own advantages based on exactly what capabilities you need.

I highly recommend installing some of these tools like w3m-img, Viu and ImageMagick to add quick image viewing capabilities without leaving your terminal. They can seriously boost productivity for tasks like previewing screenshots or output from image-related scripts.

I hope this guide was useful in explaining the various options to display graphical images in the comfort of your favorite terminal emulator! Let me know which ones you find most useful or if you have any other favorite terminal image viewer utilities not mentioned here.

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *