How to manipulate images from the terminal

ImageMagick is an application that allows us to manipulate images through the command line and that is used for practically all formats. With it, it is also possible to automate the actions on the images contained in a folder (conversion, resizing, etc ...)
Let's see a list of useful ImageMagick commands:

Get information from an image.

identify -ping image.png

Get even more information.

identify -verbose image.png

Know the list of colors used in an image.

identify -list color image.png

Convert a PNG image to JPG

convert image.png image.jpg

Convert a PNG image to JPG indicating the conversion quality.

convert -quality 96 image.png image.jpg

Convert all PNG images to JPG contained in a folder

mogrify -format png *.jpg

Convert all images (* .jpg, * .png) to PDF

convert images*.* archivo.pdf

Resize an image

convert -resize 48×48 image.png image-mini.png

Resize all images in a folder

mogrify -resize 48×48 *.png

Resize an image by specifying the width

convert -resize 620x image.png image-620.png

Resize an image by specifying its height

convert -resize x100 image.png image-100.png

Create a Favicon

convert -colors 256 -resize 16×16 image.jpg favicon.ico

Convert a color image to black and white

convert -type image.jpg image-noir-blanc.jpg

Add a 1 pixel transparent border around an image

convert -bordercolor Transparent -border 1×1 image.png image-borde.png

Add a 10 pixel black border around an image

convert -bordercolor #000000 -border 10×10 image.png image-borde.png

Create a negative of an image

convert -negate image.png image-negate.png

Rotate an image vertically

convert -flip image.png image-inversee.png

Rotate an image from left to right

convert -flop image.png image-inversee.png

Use the ImageMagick GUI

display image.png


Leave a Comment

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

*

*

  1. Responsible for the data: Miguel Ángel Gatón
  2. Purpose of the data: Control SPAM, comment management.
  3. Legitimation: Your consent
  4. Communication of the data: The data will not be communicated to third parties except by legal obligation.
  5. Data storage: Database hosted by Occentus Networks (EU)
  6. Rights: At any time you can limit, recover and delete your information.

  1.   glorfindel said

    are you using mogrify ?? look at the example in the post.
    Cheers! Paul.

  2.   RubenGnu said

    Did you copy and paste?

    It puts a letter 'x' instead of the sign for '×' that the text editor puts on the page.

    regards

    Ruben

  3.   ainhoaredam said

    Hello
    I've been testing it and it's fine, but the command to create a favicon doesn't work for me, this is the error it gives me:
    convert: invalid argument for option `-resize ': 16 × 16 @ error / convert.c / ConvertImageCommand / 2343.
    regards

  4.   katherine said

    what is a terminal

  5.   jathan said

    Very good selection of commands and clarity in their syntax. Previously I had a lot of trouble trying to use convert to resize a batch of images and now that I use mogrify for the same purpose everything worked out fine. Thank you very much Pablo.

  6.   Enrique said

    A small contribution to this brilliant post!

    To convert to B&W:

    convert -monochrome image.png image-bw.png

    regards

    1.    let's use linux said

      Thanks for the contribution!
      Hug! Paul

  7.   Moises Garnica Radilla said

    Hello, I have seen this information on many websites!
    Question: How can I scale to another directory?
    Example: convert -scale 50% -quality 80% * .jpg> scaling /
    The example does not work, how should it be from a folder x to a subfolder x / scaled?
    Beforehand thank you very much!

    1.    Sixteen said

      I made a script for it and the modified ones are saved in another folder.
      I place the script inside the folder where I have all the images that I want to modify, and I execute it from the console (the file must have execution permission). The code that I am going to put next, you paste it in a file and give it the name you want (I put .sh at the end to know that it is a script just by reading the name of the file).

      I copy and paste the code in case it helps you:

      #! / Bin / bash
      ### Change the size of an image indicating the width and quality
      # convert -quality 86 -resize 620x image.png image-620.png
      ### Change the size of an image indicating the height and quality
      # convert -quality 86 -resize x100 image.png image-100.png

      + https://blog.desdelinux.net/como-manipular-imagenes-desde-el-terminal/

      #
      # =============================================== ==============
      DIRECTORY =pwd
      cd $ DIRECTORY
      resized_directory = »resized_img»
      $ (mkdir "$ resized_directory" 2> / dev / null)
      TEMP = »img_list» # internal temporary file
      #
      #list of directory images in a temporary file
      ls * .png 2> / dev / null >> $ resized_directory / $ TEMP; ls * .PNG 2> / dev / null >> $ resized_directory / $ TEMP;
      ls * .jpg 2> / dev / null >> $ resized_directory / $ TEMP; ls * .JPG 2> / dev / null >> $ resized_directory / $ TEMP;
      ls * .jpeg 2> / dev / null >> $ resized_directory / $ TEMP; ls * .JPEG 2> / dev / null >> $ resized_directory / $ TEMP;
      ls * .gif 2> / dev / null >> $ resized_directory / $ TEMP; ls * .GIF 2> / dev / null >> $ resized_directory / $ TEMP
      #
      #changing images within the directory
      echo -n "Processing, please wait"
      while read image
      do
      echo -n "."
      convert -quality 90 -resize 1000x $ image $ resized_directory / $ image
      done <$ resized_directory / $ TEMP
      threw out ""
      #
      # delete the temporary file
      rm $ resized_directory / $ TEMP
      echo "completed successfully"

  8.   linux pro said

    very good thank you