Banner of image magick logo: the magick wizard

How to edit images in the terminal


Category: Linux

Date: March 2023
Views: 469


Simple answer: the convert command from imagemagick package

The convert command is a powerful tool that comes with the ImageMagick library, which can be used to manipulate images from the command line. This tutorial will cover some of the most common use cases of the convert command.

Installing convert

   
# Arch based disro
sudo pacman -S imagemagick
# Debian Based distros
sudo apt install imagemagick
# to check if the install was successfull
convert -version
    

Resizing an image:

To resize an image, you can use the following syntax:

   
convert input_image.jpg -resize 50% output_image.jpg
convert input_image.jpg -resize 500x500 output_image.jpg
    

This will resize the input image to 50% of its original size and save the resized image as "output_image.jpg". The second command will resize it to 500x500, this is my daily command as it helps me create small thumbnails. keep in mind that you should be mindful of the aspect ration of the image I use 500x500 because the input image is square

Cropping an image:

To crop an image, you can use the following syntax:

   
convert input_image.jpg -gravity South -chop x200 output_image.jpg
convert input_image.jpg -crop 500x500+100+100 output_image.jpg
    

The first command will remove 200px from the bottom of the input image.
The second command will crop a 500x500 pixel square from the input image, starting at coordinates (100, 100) and save the cropped image as "output_image.jpg".

Rotating an image:

To rotate an image, you can use the following syntax:

   
convert input_image.jpg -rotate 90 output_image.jpg
    

This will rotate the input image by 90 degrees clockwise and save the rotated image as "output_image.jpg".

Converting image format:

To convert an image from one format to another, you can use the following syntax:

   
convert input_image.png output_image.jpg
    

This will convert the input image from PNG format to JPEG format and save the converted image as "output_image.jpg".

Adding a border to an image:

To add a border to an image, you can use the following syntax:

   
convert input_image.jpg -bordercolor black -border 20 output_image.jpg
    

This will add a black border with a width of 20 pixels to the input image and save the bordered image as "output_image.jpg".

Adding a watermark to an image:

To add a watermark to an image, you can use the following syntax:

   
convert input_image.jpg -gravity center -pointsize 30 -fill white -annotate +0+0 "watermark" output_image.jpg
    

This will add a white watermark with the text "watermark" to the center of the input image and save the watermarked image as "output_image.jpg".

Applying a filter to an image:

To apply a filter to an image, you can use the following syntax:

   
convert input_image.jpg -colorspace Gray output_image.jpg
    

This will apply a grayscale filter to the input image and save the filtered image as "output_image.jpg".

These are just some of the most common use cases of the convert command. The convert command has many other options and features, which can be explored by referring to the ImageMagick documentation.



469 views

Previous Article Next Article

0 Comments, latest

No comments.