Selecting a rectangular region of an image and doing something with it is an extremely common operation. Even the simplest image viewers (e.g. Microsoft Paint) allow you to do this. While this is easy enough to do by hand, there are times when it’s useful to do it via the command-line, for example:
- The image is too big to load into memory via an image viewer
- You need to extract the same region for many images
In this article, we’ll see how to use ImageMagick to extract a region in an image. Just to give some context, I’m using Linux Kubuntu 19.10 and ImageMagick 7.0.10-23.
Analysing an Image
Before we go ahead and use ImageMagick, we first need to study the image that we want to process. Let’s say, for instance, that we’re going to work with the following screenshot from Warcraft II: Beyond the Dark Portal:
There are a few regions of interest that we could potentially extract: the minimap, the selected unit’s portrait, the main view, etc. Let’s say we want to extract the minimap. First, we need to figure out the details of its rectangular region, namely the coordinates of its top-right corner, its width, and its height.
We can do this using most basic image viewers. For instance, Linux distributions using the K Desktop Environment (KDE) have a simple Paint-like tool called KolourPaint. If you use the rectangular selection tool and use it to draw a boundary around the region of interest, the program should tell you the details of interest in the status bar. This is easier to do accurately if you zoom in enough and turn on the grid lines, allowing you to see exactly where your selection fits within the pixel boundaries.
For this particular example, we can see that the region of interest has its top-left corner at the coordinates (22, 24), and it has a size of 132×132.
Extracting the Minimap
All we need is to pass this information to ImageMagick along with the image file, and use the
-extract switch to extract the region of interest to a separate file, like this:
magick -extract 132x132+22+24 deathwing.png deathwing-minimap.png
That means: extract a region of size 132×132 at (22, 24) from deathwing.png and save it to deathwing-minimap.png.
And it works beautifully:
Analysing and Extracting the Unit’s Portrait
To extract Deathwing’s portrait, we just need to repeat the same process for a different region. First, we identify the coordinates and size of the region of interest:
Then we use ImageMagick to extract the desired region, as before. Obviously, we change the details of the region and the output filename:
magick -extract 52x44+6+166 deathwing.png deathwing-portrait.png
And the result is as we expect:
This is great, especially because you can do the analysis step once and repeat the command on many images that have the same structure. For instance, if I wanted to create a website with information about each unit in Warcraft 2, I would just need a screenshot for each unit. I could then simply run a command on each image, and extract the units’ portraits with ease.