
In this modern age, image manipulation is a common task that we perform almost daily. While there are numerous graphical tools available to perform such tasks, sometimes you might need to automate these tasks or use a command-line interface (CLI) due to the lack of a graphical interface. This article will guide you on how to crop images using command-line tools only. We will be using a powerful tool called ImageMagick.
Yes, it is possible to crop images using command line tools only. ImageMagick is a powerful tool that allows you to manipulate images directly from the command line. By using the convert
or mogrify
commands, you can easily crop images by specifying the dimensions and offsets. Additionally, you can use a Python script to automate more complex cropping tasks.
Introduction to ImageMagick
ImageMagick is a robust collection of tools and libraries to read, write, and manipulate an image in any of the more popular image formats. It can crop, resize, rotate, apply various effects, and do much more. In this article, we will focus on how to use ImageMagick to crop images.
Installing ImageMagick
Before we start cropping images, we need to ensure that ImageMagick is installed on our system. You can install it using the package manager of your Linux distribution. For Ubuntu, you can use the following command:
sudo apt-get install imagemagick
For other distributions or operating systems, you can refer to the official ImageMagick installation instructions.
Cropping Images Using ImageMagick
ImageMagick provides two commands for cropping images: convert
and mogrify
.
Using the convert
command
The convert
command allows you to crop an image and save it as a new file. The syntax for the command is as follows:
convert input.jpg -crop WxH+X+Y output.jpg
In this command:
input.jpg
is the original image that you want to crop.W
is the width of the crop rectangle.H
is the height of the crop rectangle.X
is the horizontal offset (distance from the left edge of the image to the left edge of the crop rectangle).Y
is the vertical offset (distance from the top edge of the image to the top edge of the crop rectangle).output.jpg
is the file name of the cropped image.
For example, if you want to crop a 100×100 pixel area from the top left corner of an image, you would use the following command:
convert input.jpg -crop 100x100+0+0 output.jpg
Using the mogrify
command
The mogrify
command is similar to the convert
command, but it modifies the original image file instead of creating a new one. The syntax for the command is as follows:
mogrify -crop WxH+X+Y input.jpg
The parameters are the same as the convert
command.
Using a Python Script
For more complex tasks, you might want to use a script. Here is an example of a Python script that uses ImageMagick to crop an image:
#!/usr/bin/env python3
import subprocess
import sys
img = sys.argv[1]
left = sys.argv[2]
right = sys.argv[3]
top = sys.argv[4]
bottom = sys.argv[5]
img_base = img[:img.rfind(".")]
extension = img[img.rfind("."):]
path = img[:img.rfind("/")]
img_out = img_base + "[cropped]" + extension
data = subprocess.check_output(["identify", img]).decode("utf-8").strip().replace(img, "")
size = [int(n) for n in data.replace(img, "").split()[1].split("x")]
w = str(size[0] - int(left) - int(right))
h = str(size[1] - int(top) - int(bottom))
x = left
y = top
cmd = ["convert", img, "-crop", w + "x" + h + "+" + x + "+" + y, "+repage", img_out]
subprocess.Popen(cmd)
This script takes five arguments: the image file path, and the number of pixels to crop from the left, right, top, and bottom. It then calculates the size of the crop rectangle and its offset, and uses the convert
command to crop the image.
Conclusion
ImageMagick is a powerful tool that allows you to manipulate images directly from the command line. This can be very useful for automating tasks or working on systems without a graphical interface. With the knowledge from this article, you should now be able to crop images using command-line tools only.
Yes, ImageMagick supports cropping images of various formats including JPEG, PNG, GIF, and more.
Yes, you can use a loop or a script to crop multiple images at once using ImageMagick. You can modify the command to include multiple input files and specify different crop parameters for each image.
It depends on the command you use. The convert
command creates a new cropped image without modifying the original image. However, the mogrify
command modifies the original image file itself. Make sure to create a backup of your original image before using the mogrify
command.
Yes, you can specify the crop dimensions and offsets using percentages instead of pixels. For example, you can use convert input.jpg -crop 50%x50%+0+0 output.jpg
to crop an image to 50% of its original width and height, starting from the top left corner.
Yes, ImageMagick is available for Windows. You can download the Windows installer from the official ImageMagick website and follow the installation instructions.
Yes, you can combine cropping and resizing in a single command using ImageMagick. You can specify the desired dimensions for both cropping and resizing, and ImageMagick will perform both operations simultaneously.
Yes, there are other command-line tools available for cropping images, such as GraphicsMagick and FFmpeg. However, ImageMagick is widely used and offers extensive features for image manipulation, making it a popular choice for command-line image cropping.