Article: How to crop images manually fast
| Table of Contents |
Abstract
It is often required to crop images, e.g., cropping faces in images, manually or semi-automatically for computer vision researchers to gather training and testing image sets.
This is a short document about how to crop images manually fast with existing free software(s).
Now, I made ImageClipper
. Just use this.
Image Editor
I use IrfanView
to crop images.
IrfanView is a famous image viewer software because of its many supports for image formats such as pgm, ppm, eps in addition to jpg, png, gif, bmp etc and its good usabilities. This can be used to crop images.
IrfanView has a good property, that is, it allows us to move to the next image in a directory by a simple action, pushing space key. Furthermore, it starts up with the rectangle selection tool as a default. Furthermore, we can create a new image of cropped region by a simple action, Ctrl-v. We do not need to do as File > New Image > Ctrl-v as some image software.
Installation is just as usual, let me skip to explain. Run the IrfanView from StartMenu and drag and drop an image to open the image.
You can select a region with mouse by doing drag and drop.
Cut the region by Ctrl-x,
and type Ctrl-v next. This not only pastes the image but also creates a new image.
Use Ctrl-s to save the image, this shows a name like CripboardXX.XXX as a default Type Enter to save. You may choose a file format here.
then use Space key or Right Arrow key to go to the next image.
Repeat this procedure.
Select a region > Ctrl-x > Ctrl-v > Ctrl-s > Enter (Save) > Space
The number of steps of this procedure is probably the least among many image software(s) to crop images.
Keyboard macro
The procedure required by IrfanView was pretty fast, but you may still feel
Ctrl-x > Ctrl-v > Ctrl-s > Enter > Space
is burdensome.
We can use a keyboard macro software to create one short-cut to do the procedure. I use AutoHotkey
.
After installation was done, run AutoHotKey from StatMenu. At the first time to run, you will see a dialog whether you create a sample script file on My Documents folder or not. I assume you chose YES. The AutoHotKey stays at the task tray. The script file should be opened with a notepad, or you can right-click AutoHotKey icon on the task tray and choose 'Edit this script'.
Add this to the script file
#s::
Send, {CTRLDOWN}xvs{CTRLUP}{ENTER}{SPACE}
return
Then, right-click AutoHotKey icon on the task tray and choose 'Reload this script'.
Now, you can do the sequence of hot-keys by Windows key (#) + s. This creates files named as ClipboardXX.XXX.
Now, required things to do is only
Select a region > Win + s
Cool!
Here is a simple list of modifier keys recognized on AutoHotkey
| # | Win (Windows logo key) |
| ! | Alt |
| ^ | Control |
| + | Shift |
See official site
for others.
Let me see....
#s::
SetTitleMatchMode, 2
Send, {CTRLDOWN}xvs{CTRLUP}
WinWait, Save ...
IfWinExist Save ...
{
WinActivate
}
Send, {ENTER}{SPACE}
return
would be better than the previous script. Sometimes ENTER to save an image was too fast to wait to see Save... Dialog window.
Rename
You can rename image files later with a command as
Linux (cygwin): rename Clipboard Foobar *.XXX DOS: ren Clipboard*.XXX Foobar*.XXX
How to crop with the same size
You may want to crop regions with same size always. Use this AutoHotKey script as an example.
#s::
SetTitleMatchMode, 2
Send, {CTRLDOWN}xvs{CTRLUP}
WinWait, Save ...
IfWinExist Save ...
{
WinActivate
}
Send, {ENTER}{SPACE}
WinWait, IrfanView
IfWinExist IrfanView
{
WinActivate
}
MouseClickDrag, LEFT, 300, 200, 326, 233
return
!s::
MouseClickDrag, LEFT, 300, 200, 326, 233
return
Use Alt+s to draw rectangular (select a region) at the first picture. You can move the rectangular by right click without changing the size of the rectangular on IrfanView. After that, use Win+s to do the procedure.
How to get positions
If you want to get positions of cropped regions in the original images later, you can do it simply by a template matching. I wrote a simple c code using OpenCV.
I attached a binary for windows. The usage is as follows:
Usage: templatematch.exe <reference> <template> [-show]
and this outputs the position of a cropped reagion as
upperleft-x upperleft-y width height
I recommend you to install cygwin to get a rich CUI environment on Windows.
You can create a shell script to do batch process as following Linux (cygwin) commands:
$ find [original image dir] -name '*.jpg' > original.txt $ find [cropped image dir] -name '*.png' > crop.txt $ yes ./templatematch | head -`cat original.txt | wc -l` > command.txt $ paste -d' ' command.txt original.txt crop.txt > batch.sh $ sh batch.sh > position.txt # Execute ./templatematch original01.jpg crop01.png ./templatematch original02.jpg crop02.png ./templatematch original03.jpg crop03.png ...
Discussion
That's it. Yes, creating a software specialized to crop is still good and I may create it.
References
AutoHotKey
- http://www.autohotkey.com/docs/
- http://lukewarm.s101.xrea.com/
(Unofficial Japanese site)
