78

I want to crop an image using command line tools only indicating pixels to crop for the four directions (the same way we can crop in LibreOffice)

For example:

crop image.jpg -top 5px -bottom 7px -right 14px -left 3px

Is there such a tool (not GUI)?

Zanna
  • 72,312
Maythux
  • 87,123

6 Answers6

87

Here is a workaround using convert from image magick pack.

sudo apt-get install imagemagick

For a picture image.jpg

$ identify image.jpg 

image.jpg JPEG 720x482 720x482+0+0 8-bit DirectClass 100KB 0.000u 0:00.009

As shown above, the input image is 720x482px.

Now to do cropping you have to determine two factors:

  1. starting point of the cropping (includes 2 directions)
  2. The cropped rectangle size (Here you can include the other directions)

Now back to the image image.jpg above, I want to crop:

  • top 5px
  • bottom 7px
  • right 14px
  • left 3px

then you could do it with (widthxheight+left+top / wxh+l+t format):

convert image.jpg -crop 703x470+3+5 output.jpg

Now

$ identify output.jpg 

output.jpg JPEG 703x470 703x470+0+0 8-bit DirectClass 102KB 0.000u 0:00.000
el-teedee
  • 157
Maythux
  • 87,123
34

If you want to trim white regions away, imagemagick has a special command for it:

convert -trim input.jpg output.jpg
Martin Thoma
  • 20,535
16

To create a "user friendly" cli- option, the script below can be used. Simply run the command:

<script> <image> <crop_left> <crop_right> <crop_top> <crop_bottom>

It creates a cropped image of image.jpeg, named image[cropped].jpeg in the same directory.

The script

#!/usr/bin/env python3
import subprocess
import sys

# image, crop- dimensions
img = sys.argv[1]; left = sys.argv[2]; right = sys.argv[3]; top = sys.argv[4]; bottom = sys.argv[5]
# arrange the output file's name and path
img_base = img[:img.rfind(".")]; extension = img[img.rfind("."):]; path = img[:img.rfind("/")]
img_out = img_base+"[cropped]"+extension
# get the current img' size
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")]
# calculate the command to resize
w = str(size[0]-int(left)-int(right)); h = str(size[1]-int(top)-int(bottom)); x = left; y = top
# execute the command
cmd = ["convert", img, "-crop", w+"x"+h+"+"+x+"+"+y, "+repage", img_out]
subprocess.Popen(cmd)

How to use

  1. The script uses imagemagick

    sudo apt-get install imagemagick
    
  2. Save the script above as crop_image (no extension) in ~/bin.

  3. Create the directory if necessary. In that case, also run source ~/.profile to make the directory show up in $PATH.
  4. Make the script executable.

Now simply run the script by its name, as mentioned, e.g.:

crop_image /path/to/image.jpg 20 30 40 50

Spaces are no problem, as long as in that case, you use quotes:

crop_image '/path/with spaces in the name/to/image.jpg' 20 30 40 50
Maythux
  • 87,123
Jacob Vlijm
  • 85,475
9

The crop command needs 4 things. To understand it take the image you want to crop. Now, imagine that on the image, you are drawing a rectangle of the size which you want to retain. The area outside this rectangle will be eliminated, cropped. The rectangle must not be tilted i.e. the top side must be horizontal.

Now, note down these 4 things:

  1. the width (W) in pixel of the rectangle
  2. height (H) of the rectangle
  3. distance of the left vertical side of the rectangle from the left margin/end (L) of the image
  4. distance of the top side of the rectangle from the top margin/end of the image (T).

Thus you have now W, H, L and T values. So far so good. To know the pixels, you may install krule tool in Ubuntu. Very useful.

Now, open the terminal and go to the folder where the image is stored. Use the following command and put the values of W, H, L and T properly:

convert input.jpg -crop WxH+L+T output.jpg
Zanna
  • 72,312
Googly Googly
  • 91
  • 1
  • 1
5

Use mogrify -crop <W>x<H>+<X>+<Y> <files>.

Careful: the files are overwritten without notice. Add the -path option to specify an output directory to prevent this if required.

As an example: mogrify -crop 256x256+10+5 images/*.jpg will crop each image in the images folder to a 256x256 image by starting 10 pixels from the side and 5 pixels from the top. It will overwrite the old images.

If you get a Argument list too long error due to trying to convert many images at once, simply wrap your image path in single quotes to prevent bash from expanding it: mogrify -crop 256x256+10+5 'images/*.jpg' (mogrify will do the expansion itself)

Kirill
  • 103
R2-D2
  • 203
  • 2
  • 6
3

You can use convert command in image magick pack.
To install sudo apt-get install imagemagick or sudo yum install ImageMagick.
Then use -crop geometry to crop the image. For more readings read here