How to Resize Images on the Ubuntu Command Line

Sharing graphics and photos has been so popular in the past few years that I am sure you must have also found yourself sharing, and even creating some. While working with graphic files, sometimes we also have to resize them by changing their dimensions. This way, we can make sure that the image fits in the view where we have to eventually display it. There are numerous tools available on Ubuntu that serve your purpose, but why install the complicated ones when you are only concerned with one basic procedure-resizing your graphics. Graphic designers also have to deal with the same issue but they are well-equipped with tools that can resize the images so that they can be easily shared with the stakeholders.

In this article, we will explain how you can resize your graphic files or photos through the Ubuntu command line using the ImageMagick utility.

Why the Command Line?

If you are a Terminal-savvy person, you wouldn't want to leave the comfort of the command line and go somewhere else to do any of your daily technical activities. There is always a way to do almost all of our stuff right inside the Terminal. So, why should gif resizing be any different! Using the Terminal makes certain tasks more efficient and, even faster. The command-line tools do not use too many resources and thus form great alternatives to the widely used graphical applications, especially if you are stuck up with older hardware.

We have run the commands and procedures mentioned in this article on a Ubuntu 18.04 LTS system.

Resize a graphic file on the Linux Command Line

Install ImageMagick

ImageMagick is a free, and open -source image editing suite for viewing, editing and converting image files. ImageMagick can easily be installed through the Ubuntu official repositories using the apt-get command.

Open the Terminal application either through the system Application Launcher search or the Ctrl+Alt+T shortcut. Running the following command before installing a software through the command line helps you in installing its latest available version. You can update the repository index through this command:

$ sudo apt-get update

Then, run the following command as sudo in order to install ImageMagick

$ sudo apt-get install imagemagick

Install Imagemagick

The system might prompt you with a y/n option to confirm installation. Please enter Y and hit Enter after which the software will be installed on your system.

Resize an Image

We will now explain how you can resize an animated gif by specifying the new width and height of the image, in pixels. You can use the same procedure for other image or graphic files like .png ot .jpg as well.

For that, it is important that you know the dimensions of your original gif. The following command helps you in printing the dimensions of an image file:

$ identify -format "%wx%h" imagefile

In this article, I am using a sample gif file named “sample.gif” to explain resizing of a gif file.

To know its exact dimensions, I would run the following command:

$ identify -format "%wx%h" sample.gif

For non-animated images, the command gives a single value for dimensions but for gifs, the output is somewhat like this:

Identify image format

The first value of the dimension, highlighted above, is of importance to you. This is the dimension of your original gif.

Now, run the following command syntax so that your original gif is not lost:

$ convert sample-image.gif -coalesce temporary-image.gif

I will also convert my sample.gif to a temporary one using the following command:

$ convert sample.gif -coalesce temporary-image.gif

Use the convert command to resize an image

Then, it is time to convert the temporary gif to the final resized image through the following command syntax:

$ convert -size [orig-size-of-input-gif] temporary-image.gif -resize [target-size] resized-image.gif

I will bring down the size of my gif from 800x600 pixels to 400x300 by converting the temporary image to the final “resized-image.gif”.

$ convert -size 800x600 temporary-image.gif -resize 400x300 resized-image.gif

resize graphic file

You can verify the change of size by running the following command again on your final image:

$ identify -format "%wx%h" resized-image.gif

Check the new size of the resized image

The above output displays a successful change in the size of my gif.

Remove ImageMagick

Although ImageMagick is a very useful utility, you can uninstall it from your system if you want through the following command:

$ sudo apt-get remove imagemagick

Enter y when the system prompts you with a y/n option for software removal. ImageMagick will then be uninstalled from your system.

This is how you can easily resize a gif, without having to damage its quality; all through the Ubuntu command line.