ImageMagick can perform quite a few operations. This guide will guide you through the syntax and basic operations of ImageMagick and show you how to combine the various actions and how to batch multiple images.
Installation
in Ubuntu and many Linux distributions, there is no default installation ImageMagick, to install it on Ubuntu, please use the following command:
The code is as follows |
Copy Code |
sudo apt-get install ImageMagick |
Convert the format of an image
The conversion command performs an operation on an image and saves it in the name you specify. One of the most basic things you can do with it is to convert your images to a variety of other formats. The following command converts a PNG file called "Howtogeek.png" in the current directory to a JPEG file.
The code is as follows |
Copy Code |
Convert Howtogeek.png howtogeek.jpg |
You can also specify the compression level for JPEG images:
The code is as follows |
Copy Code |
Convert howtogeek.png-quality howtogeek.jpg |
This number must be between 1 and 100. In the absence of designation, ImageMagick uses the quality level of the original image (quality levels), otherwise ImageMagick takes 92 as its default value.
Scaling Images
The conversion command also makes it easy to resize an image. The following command instructs ImageMagick to resize an image to 200 pixels wide and 100 pixels high.
The code is as follows |
Copy Code |
Convert example.png-resize 200x100 example.png |
In this command, we use the same filename for both the input and the output, so that ImageMagick will overwrite the original file.
When using this command, ImageMagick will try to keep the aspect ratio of the image. It will adjust the image to fit the 200x100 area so that the image is not exactly 200x100. If you want to force the image to a specified size-even if this changes the aspect ratio of the image-then add an exclamation mark after the dimension parameter.
The code is as follows |
Copy Code |
Convert Example.png-resize 200x100! Example.png |
You can also specify a specific width or height, and the ImageMagick will scale when the aspect ratio is maintained. The following command scales the width of an image to 200 pixel wide:
The code is as follows |
Copy Code |
Convert Example.png-resize example.png |
The following command scales a picture to a height of 100 pixels:
The code is as follows |
Copy Code |
Convert Example.png-resize x100 example.png |
Rotate image
ImageMagick can rotate images quickly. The following command rotates an image called "howtogeek.jpg" 90 degrees and saves the rotated image as "howtogeek-rotated.jpg":
The code is as follows |
Copy Code |
Convert Howtogeek.jpg-rotate howtogeek-rotated.jpg |
If you specify the same filename, ImageMagick will overwrite the original image with the rotated image.
Apply Special effects
ImageMagick can make a variety of special effects on an image. For example, the following command applies an effect called a "charcoal stroke" (charcoal) to an image:
The code is as follows |
Copy Code |
Convert Howtogeek.jpg-charcoal 2 howtogeek-charcoal.jpg |
This command will give your image an artistic Shang effect, and 2 of the-CHARCOAL option can control the strength of the effect.
The following command produces an "implosion" (implode) effect with a strength of 1:
The code is as follows |
Copy Code |
Convert Howtogeek.jpg-implode 1 howtogeek-imploded.jpg |
The "implosion" effect makes a picture appear as if there is a black hole in the center.
Combine the various operations!
All of these commands can be combined, so that you can zoom, rotate, add special effects, and format conversions to an image at the same time:
The code is as follows |
Copy Code |
Convert howtogeek.png-resize 400x400-rotate 180-charcoal 4-quality howtogeek.jpg |
With ImageMagick, you can do much more than that, and there are many commands you can combine to use.
Batch Processing
With bash, you can easily batch multiple image files. For example, the following command will rotate all PNG files in the current directory and save them with a new filename consisting of the original file name plus "-rotated."
The code is as follows |
Copy Code |
for file in *.png; Do convert $file-rotate rotated-$file; Done |
By modifying this command a little bit, you can use it to do a lot of other things. In addition, you can automate the process of image processing by integrating batch commands into bash scripts.
Any article about ImageMagick will omit a lot of things-because it has so many commands and options. If you are interested in other features of ImageMagick, please refer to ImageMagick's official documentation for a further understanding of ImageMagick