Sometimes you'll want to crop the same area from multiple images (think of taking the contents of the same window from a load of screenshots). Of course, you could fire up your favourite image editor to select and crop over and over, but, as usual, there is a better way. This short tutorial describes an efficient way to do this for a theoretically infinite amount of images. Difficulty: Basic - Medium Note: I strongly recommend making a backup of the images you're using for this tutorial before messing with them. Some actions in this tutorial will overwrite the image files, so if you make a mistake you may lose valuable data. This tutorial assumes basic Linux knowledge, like starting a program, opening a terminal and working with a terminal. The tools we'll be using are GIMP and mogrify (from the ImageMagick suite), so make sure that you have them installed. We'll use GIMP to graphically select the area to be cropped and the mogrify tool to automate the cropping, saving us a lot of work. Let's start with the selecting: Getting the right cropping values using GIMPIn 5 steps: We now have the values we need to tell the mogrify utility what to crop. Let's go on and write a line that'll execute mogrify in such a way that it'll crop all our images! Cropping the imagesNow we'll start working in the terminal. Open up your favourite one and cd to the directory where the images are located. Note that I strongly recommend having only the images that are to be cropped in the directory, nothing more. It saves you a lot of trouble. Well then, let's start with the mogrify command. The syntax for cropping is as follows:
Now don't be scared, the {Width}, {Height} and so on simply are the places where you should put the values you got from GIMP! Note that I use a png file as an example, while mogrify is able to handle over 100 image file types. You don't have to use png files with it. Anyway, if I'd fill in the values from the screenshot, the mogrify command would look like this:
The logic behind this system is the following: crop an area of 643 by 393 pixels, starting at 7 pixels from the left and 83 pixels from the top of the image. Got it? Ok then. The above command would overwrite image.png with a cropped version. But this still manipulates just a single image. The easiest way to make mogrify modify all images is just this:
The asterisk makes bash fill in all png files in the current directory, and mogrify will handle them all happily. After a (hopefully short) wait, all the images will have been cropped. If you want to crop images of an other format, just change "*.png" to, for example, "*.jpg" or "*.gif". You might want to give the cropped images other names, so that the original images will not be overwritten and it will be clear which images have been cropped and which haven't. This is more complex, but hey, we're working on Linux! Everything is possible if you take the time to write it. Renaming the cropped imagesIn order to give the cropped images other names, we'll use a bash loop. This time, we'll use the convert utility. It is from the same family as mogrify, but it makes it easier for us to output to an other filename. I won't explain the whole loop, as most of it is bash knowledge, but I will tell you which things you can/should alter to get the right results. There are loops for two cases, just pick the one of which you like the file naming the most.
Final wordsWell, I hope this small tutorial helped you. If you want some (more) help with any of these things, feel free to place a comment. Thanks for reading and God bless! |
|