Working with and Optimising images on the command line in Linux
Using the command line there are optimisation you can do with images rather than using a photo editing program to perform some basic image editing. The code below is what I tend to use more often. For all the code below its assuming Debian based system like Ubuntu.
Resize an image on the terminal
This may be useful if you in a pinch is resizing images on the command line. Open up the command line and we enter the following command first we start of with the command covert followed by the name of the image then we pass in the -resize flag. Followed by the new width and height. Then finally the name of the new image.
convert originalimage.png -resize 500x500 newimage.png
The convert command can do loads of other useful actions with images such as change the file type, rotate an image, change saturation and blur. Its worth reading up further if you find any of the actions useful – https://imagemagick.org/script/convert.php
Optimise images for Jpg and Png
There are two different tools which I use for optimising images one for Jpg called jpegoptim and the other for png called optipng, Now lets install both packages
sudo apt-get install optipng jpegoptim
Lets start with optimising a JPG in the command line type the following if you take a note of what the size of the image before and after to notice the difference.
Go back the image and check the size and you should see the size reduced. Now lets do the same process using a PNG. On the command line enter the following below.
Once again go back to the command line and you should notice a difference in size.
Convert images from Webp to Png
This one isn’t much of an issue for me since WordPress 5.8 as added native support for Webp. However you may find it useful. We using a package called webp lets start by installing the package.
sudo apt install webp
On the command enter dwebp followed by the filename and then -o flag to where you want the new png file to be saved.
dwebp filename.webp -o newfilename.png