19

I want to crop an image on my CLI only Ubuntu using command line tool only indicating pixels to crop for the four directions. (same as that found in libreoffice)

For example:

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

Is there such a tool (not GUI)?

  • possible duplicate of Is there a lightweight tool to crop images quickly? – Thomas Weller Jun 3 '15 at 9:03
  • @ThomasWeller this is asking for command line and not GUI tool plus the linked question you supplied is about GUI so please just read the question again – Maythux Jun 3 '15 at 9:09
  • 2
    The linked question does not specify the tool type. The second answer is about a command line tool: imagemagick. I know there are discussions on SO whether a question is a duplicate in case the answer is the same. People may agree or disagree. – Thomas Weller Jun 3 '15 at 9:15
  • 1
    @ThomasWeller but the answer is not convenient, moreover it doesn't show how and why he used the options.. Frankly did you understand the options used?! I myself can't get that information, it's just the first answer here, it's with no explanation which makes the answer low quality – Maythux Jun 3 '15 at 9:17
  • @downvoters Should I know what is the matter with you?!! – Maythux Jun 3 '15 at 9:18
30

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 713x470+5+3 output.jpg

Now

$ identify output.jpg 

output.jpg JPEG 713x470 713x470+0+0 8-bit DirectClass 102KB 0.000u 0:00.000
  • I hope this would help – Maythux Jun 3 '15 at 8:56
  • It's said that Imagemagick 7 uses the "magick" command in place of "convert". If anyone run into command not found: convert issue, try magick – iplus26 May 4 '17 at 11:38
  • This was a big help, but took me a while to wrap my head around the "calculations". I had a set of 1280x1024 images I needed to crop top and bottom from, so final image would have the "middle" 718 pixels from the original (taking off a black band top and bottom). I needed to use this: convert in.png -crop 1280x718+0+152 out.png where I understand that line to mean: convert in.png -crop [final-right-x]x[final-right-y]+[crop-left]+[crop-top] out.png, although that doesn't seem to match @Maythux's numbers... FWIW! – Dɑvïd Feb 19 '18 at 10:11
  • 1
    Shouldn't it be 703x470 instead of 713x470? As the left+right cropping = 3+14 = 17px, that when subtracted from 720 is 703, and not 713. – Mooncrater May 31 '18 at 7:05
8

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 Not sure what your latest edit means, you mean the solution above does not work on your system? It is cli only. – Jacob Vlijm Jun 4 '15 at 7:14
  • I'm sorry I was off three days ago, so I don't know why you get a downvote, instead here my +1 since you deserve. – Maythux Jun 8 '15 at 9:11
  • But I think even it works this would be pretty harder for a newbie a bit than using a native crop tool, but for me I love your way, pretty cool, sorry again for delay – Maythux Jun 8 '15 at 9:13
  • Wow, thanks. The downvote I got because of this: meta.askubuntu.com/questions/14082/… – Jacob Vlijm Jun 8 '15 at 9:13
  • I don't know why a good answer/question is downvoted unless a bad use of reputation, this question gots -3!!! hhhh I don't get why – Maythux Jun 8 '15 at 9:16
7

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

convert -trim input.jpg output.jpg
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

  • This crop by ratio and not with different margins – Maythux Jun 3 '15 at 8:43
  • Nope, -crop option gives x and y That are offsets and gravity. so you can use it to time to crop a square – M.Fooladgar Jun 3 '15 at 8:48
  • I posted it as answer, thanks for your help anyway, but you should write that in your answer before4 – Maythux Jun 3 '15 at 8:48
  • @Arronical Thanks I already get it and posted as answer. please check it – Maythux Jun 3 '15 at 8:50
3

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

Careful: the files are overwritten without notice.

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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