1

For example, I'd like to crop an area in this yellow rectangle in all pictures of same dimensions (same length and width).

Image

One use of this may be to crop a bunch of screenshots taken of a video game or program so that only the relevant parts of the image are there.

1
  • 1
    NetPBM (open source, multi-platform) is another solution. I've used it for this sort of task, multiple times. – TOOGAM Feb 21 '20 at 6:22
4

You can use irfanview to do this. Once you have it installed, open a command prompt, go to irfanview program folder, then convert your images with a command like this:

i_view64.exe c:\images\*.jpg /crop=(0,0,500,500,0) /convert=c:\output\*.jpg 

The 5 numbers in the parenthesis are

  1. starting X position
  2. starting Y position
  3. width
  4. height
  5. which corner to start with.

you'll have to find these numbers yourself. Once you have the numbers figured out, you can crop all your images with the command above in one go.

Or if you prefer graphical user interface instead of command line, you can open up one of your images using irfarview, press b to batch crop/convert your files. you can find crop options by advanced button.

4
  • What's a good way to find the numbers to crop? Is there a software that lines up your picture in a pixel grid? – CreativiTimothy Feb 26 '20 at 23:51
  • 1
    irfanview does that as well. Just open your image, drag your mouse to select an area, then you can see the size and location of your selection in the title. – SparedWhisle Feb 27 '20 at 0:30
  • Great got it. By the way for the corner to start with, are there 4 corners? (So 0 for top left, 1 for top right, 2 for bottom left, 3 for bottom right). I don’t understand that part. I just put 0 like your example and it cropped just fine, but I’d like to understand it. – CreativiTimothy Feb 27 '20 at 1:11
  • 1
    I actually tested this myself. you are right, 0 is for top left, so you usually want that. 1 would be top right, so top right corner would have X index 0, top left corner would have the largest X index. Same rule goes for 2 and 3. – SparedWhisle Feb 27 '20 at 1:42
3

You can use the well known free and open-source ImageMagick

Simple example from the documentation:

convert rose: -crop 40x30+10+10  crop.gif

Where 40x30+10+10 is the geometry, basically meaning width x height + offsetX + offsetY. All in pixels. Offset is measured from top left corner. Note that there should be no spaces between the numbers and the x and + signs. I'm adding spaces here for readability.

To run that for a batch of images, you could use OS completion and expansion commands depending on which system you're on. In windows command prompt for example:

for %f in (*.png) do (
    convert %f -crop 40x30+10+10 cropped_%f
)

In PowerShell:

gci *.png | %{ convert -crop 40x30+10+10 $_.FullName ($_.BaseName+"_crop"+$_.Extension)}

--- edit

And in Bash:

images=$(ls -1 *.png)
for image in $images ; do
    convert -crop 40x30+10+10 cropped_$image
done

If you want to recurse through all sub-directories, use:

images=$(find -type f -name '*.png')

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.