Get Rewarded! We will reward you with up to €50 credit on your account for every tutorial that you write and we publish!

Viewing and editing images in terminal

profile picture
Author
Karol Sarbiewski
Published
2024-11-05
Time to read
9 minutes reading time

Introduction

In this tutorial I will talk a little about images in the terminal. Probably everyone has had a problem at some point... when they were designing a page and wanted to see if a particular image was that particular image.... had to upload it to Apache or download it. Or they wanted to make the image smaller or crop it. What if I tell you that it is possible to do all this from a terminal? Two things are already enough for this: the imagemagick library and the tiv program.

I will be using two example images throughout this tutorial:

code.jpg server.jpg

Step 1 - Installing packages

To begin with, we will check if we have installed the make and g++ we need. To do this, type make --version — if we get a response about the version — it means that make is installed. We do the same with: g++ --version — if we get a version then we have this package installed. Below I will attach commands to install all packages — as if any of them is not installed.

  • Ubuntu

    sudo apt update
    sudo apt install g++ make imagemagick
  • Fedora

    sudo dnf update
    sudo dnf install gcc-c++ make ImageMagick
  • CentOS/RHEL

    # epel-releases are additional packages that are not in the standard repositories but necessary to install ImageMagick
    
    sudo yum update
    sudo yum install gcc-c++ make ImageMagick
    sudo yum install epel-release
  • Arch Linux

    sudo pacman -Syu
    sudo pacman -S gcc make imagemagick
  • openSUSE

    sudo zypper refresh
    sudo zypper install gcc-c++ make ImageMagick

Step 2 - View images in the terminal

Once we have gone through the installation of all the required packages, we can start by installing the tiv tool — TerminalImageViewer. With it we will be able to preview the image without leaving the terminal window.

How does it work? The image that we point to tiv is divided into small segments. The color of these segments is averaged. Then these segments are displayed to us in the terminal as ▎▖ characters with previously taken colors. In practice, what comes out is a block of text that looks a bit like pixel-art. Below is a picture:

image inside terminal - tiv

As you can see in the image above — you can easily see what the graphic shows.

Installing TerminalImageViewer

To install tiv, we need to type these commands in the terminal — one by one:

git clone https://github.com/stefanhaustein/TerminalImageViewer.git
cd TerminalImageViewer/src
make
sudo make install

Now we already have a preview of the images from the terminal. To test, you can run:

tiv <image_name>.jpg

Step 3 - Editing images in the terminal using imagemagick

Below I will describe the main commands for imagemagick and show what they do.

  • Percentage reduction/enlargement of images

    To increase or decrease the size of an image by a given percentage we will use:

    convert INPUT_IMAGE.jpg -resize XX% OUTPUT_IMAGE.jpg

    In the place where XX% is, we specify the resize percentage. For example, 50% or 150%.

    server.jpg 50% smaller


  • Changing the image to a specific dimension

    Here we will also use the -resize flag but we will give specific dimensions:

    convert INPUT_IMAGE.jpg -resize WWWxHHH! OUTPUT_IMAGE.jpg

    In the place of WWWxHHH! we specify the dimensions to which the image should be scaled. For example: 100x120! or 800x600! (remember the exclamation point at the end).

    code.jpg with 200x100 size


  • Resize the image by specifying only one of the parameters

    With the -resize flag we can also specify only one of the parameters (e.g. height) then the other parameter (e.g. width) will be calculated and the aspect ratio will be preserved.

    convert INPUT_IMAGE.jpg -resize xHHH OUTPUT_IMAGE.jpg

    In the place of xHHH we specify the height of the image so that the width is adjusted proportionally — for example: x500. We can do it analogously with the height of the image — for example: 200x.

    server.jpg with 100px height


  • Adding a border around an image

    To add an outline to an image, use this command:

    convert INPUT_IMAGE.jpg -bordercolor COLOR -border SSxDD OUTPUT_IMAGE.jpg

    In the place where we see COLOR we specify a color in words — for example red or black (to display a list of available colors we use the convert -list color command). In place of SSxDD we specify the dimensions of the frame. The first size corresponds to the left and right sides. The second size corresponds to the top and bottom sides. For example, we can specify 20x10 or 0x10. Our whole command can look like this:

    convert INPUT_IMAGE.jpg -bordercolor red -border 10x20 OUTPUT_IMAGE.jpg

    code.jpg with 0x20 border


  • Crop image in the middle

    To crop the image to a specific size in the middle we use the command:

    convert INPUT_IMAGE.jpg -gravity center -crop WWxHH+0+0 +repage OUTPUT_IMAGE.jpg

    In this command we change WWxHH to the size we are interested in — for example: 100x150. We remember that the additions to the dimension +0+0 must remain. That is, the whole command will look like:

    convert INPUT_IMAGE.jpg -gravity center -crop 100x150+0+0 +repage OUTPUT_IMAGE.jpg

    server.jpg cropped to 50x50 on center


  • Trim image from a specific point (x,y)

    To crop the image to the required dimension from a specific point in the image (x,y coordinates) we use the command:

    convert INPUT_IMAGE.jpg -crop WWxHH+XX+YY OUTPUT_IMAGE.jpg

    In this command, as in the one above, we set the size of the crop using WWxHH. In addition, the next parameters are +XX+YY. These are the coordinates from which the image will be cropped — for example: +34+100. The whole to complete in this command looks like this: 100x150+34+100.

    code.jpg cropped to 100x150 x=34 y=100


  • Invert X-axis image

    To invert the image in the X axis, use the command:

    convert INPUT_IMAGE.jpg -flop OUTPUT_IMAGE.jpg

    server.jpg flopped


  • Y-axis image inversion

    To invert the image in the Y axis, use the command:

    convert INPUT_IMAGE.jpg -flip OUTPUT_IMAGE.jpg

    code.jpg flipped


  • Combining multiple images into one horizontal

    Convert also allows us to combine images into one. With this procedure we can merge any number of images into one. This will be an X-axis (horizontal) merge.

    convert INPUT_IMAGE_1.jpg INPUT_IMAGE_2.jpg INPUT_IMAGE_n.jpg +append OUTPUT_IMAGE.jpg

    code.jpg and server.jpg merged (x)


  • Merging multiple images into one vertical

    Merging Y-axis (vertical) images should be done with the command:

    convert INPUT_IMAGE_1.jpg INPUT_IMAGE_2.jpg INPUT_IMAGE_n.jpg -append OUTPUT_IMAGE.jpg

    server.jpg and code.jpg merged (y)


  • Changing image colors to black and white

    Convert also allows us to perform color-based operations. Here we have a command to change the image to black and white colors:

    convert INPUT_IMAGE.jpg -monochrome OUTPUT_IMAGE.jpg

    code.jpg in monochrome


  • Change image colors to shades of gray

    To change the image color palette to shades of gray we will use the command:

    convert INPUT_IMAGE.jpg -colorspace Gray OUTPUT_IMAGE.jpg

    server.jpg in Grays


  • Increase/decrease contrast and brightness

    Increasing or decreasing the contrast and brightness is done with one command. The first value we specify in the command will be brightness, the second will be contrast. I point out that we can specify negative numbers in these values. The command itself looks like this:

    convert INPUT_IMAGE.jpg -brightness-contrast BBxCC OUTPUT_IMAGE.jpg

    With BB we specify the brightness, and with CC we specify the contrast. E.g. 10x12, -5x10 or -10x-10.

    code.jpg +10brightness -10contrast


  • Adding blur

    Adding blur in a photo is possible and is controlled by a single value. The command looks like this:

    convert INPUT_IMAGE.jpg -blur 0xBB OUTPUT_IMAGE.jpg

    In the place where we have 0xBB we specify the blur value. For a strong blur we specify 0x10 and for a light blur we specify 0x2.

    server.jpg with 0x5 blur


  • Rotate the image by a given angle

    To rotate the image by a given angle we will use the command:

    convert INPUT_IMAGE.jpg -rotate XX OUTPUT_IMAGE.jpg

    In the place where there is XX we specify the angle by which we want to rotate the image — for example 45 or 90.

    code.jpg rotated 45


  • Convert image to PNG with transparency settings

    To convert a JPG file to a PNG file with settings to make a given color transparent, we will use the command:

    convert INPUT_IMAGE.jpg -fuzz PP% -transparent CCCC OUTPUT_IMAGE.png

    In the place where we have CCCC we specify what color is to be transparent — for example white, black or red etc. In the place where we have PP% we specify what tolerance we want the given color to change to transparent — for example, 10% or 5%.

    server.jpg with 10% black transparency

Conclusion

As you can see imagemagick is a very powerful library for image processing. There are many more such commands, which allow you to make, for example, a collage or insert text.... but I decided that here I will discuss the basic ones. I hope that now the processing of images on your server will be more pleasant!

License: MIT
Want to contribute?

Get Rewarded: Get up to €50 in credit! Be a part of the community and contribute. Do it for the money. Do it for the bragging rights. And do it to teach others!

Report Issue
Try Hetzner Cloud

Get 20€ free credit!

Valid until: 31 December 2024 Valid for: 3 months and only for new customers
Get started
Want to contribute?

Get Rewarded: Get up to €50 credit on your account for every tutorial you write and we publish!

Find out more