Command line batch image cropping tool
Asked Answered
G

8

59

is there any lightweight command line batch image cropping tool(Linux or Windows) which can handle a variety of the formats ?

Glossotomy answered 12/12, 2009 at 12:16 Comment(2)
Is there anything else about your system you'd like to tell us? for example, which OS?Gertrudis
Either Linux or Windows.I have both on my little netbookGlossotomy
R
33

Imagemagick is what you want -- tried and true.

Roseannaroseanne answered 12/12, 2009 at 12:19 Comment(4)
Thought I would share this, I tried pasting the link in MSN messenger to a friend and no matter what it seems it's on a blacklist of MSN. I had to make tinyurl in order to share it. Strange.Stesha
That is very weird. Does MSN Messenger still exist anyway? ;)Roseannaroseanne
Haha, well, I use Pidgin and it still uses the protocol that MSN had, I believe. Strange the domain would be blocked though.Stesha
This answer doesn't mention any concrete reason for why they would want it nor how to specifically use it for cropping.Kristof
S
61

In Linux you can use

mogrify -crop {Width}x{Height}+{X}+{Y} +repage image.png

for CLI image manipulation

Subtlety answered 10/2, 2012 at 22:44 Comment(6)
homebrew makes short work of this in Mac OS X also. brew install imagemagick Then to see your new goodies... ls -lrt /usr/local/bin/Leroi
Note that this doesn't actually crop the image data, it only blanks the area sorrounding the cropping rectangle and writes the new dimensions+offsets into image metadata, but the 'physical' dimensions of the image will not be changed. Because of this, this method doesn't work well with GIFs for example.Guillotine
P.S. to actually crop the image in the sense in which this operation is commonly understood, use the +repage argument along with the crop operator.Guillotine
Additionally: trying this I got very confused, until I figured out that the image was actually rotated and the image viewer autorotated it upright. (The right orientation is contained in metadata/exif data I think.) To fix this, I used this: mogrify -alpha on -auto-orient *.jpgAlpheus
@ReactiveRaven Link deadPice
NOTE that mogrify will overwrite the image, while convert will write to a new oneMorphia
C
59

Imagemagick's convert does the trick for me (and much more than cropping):

convert -crop +100+10 in.jpg out.jpg

crops 100 pixels off the left border, 10 pixels from the top.

convert -crop -100+0 in.jpg out.jpg

crops 100 pixels off the right, and so on. The Imagemagick website knows more:

http://www.imagemagick.org/Usage/crop/#crop

Cyrie answered 12/2, 2013 at 4:40 Comment(2)
Worth mentioning that ImageMagick's convert also offers autocrop/autocropping, throug the -trim option.Guadiana
-trim is terrific! It cuts out all the white margins automatically.Cavalry
R
33

Imagemagick is what you want -- tried and true.

Roseannaroseanne answered 12/12, 2009 at 12:19 Comment(4)
Thought I would share this, I tried pasting the link in MSN messenger to a friend and no matter what it seems it's on a blacklist of MSN. I had to make tinyurl in order to share it. Strange.Stesha
That is very weird. Does MSN Messenger still exist anyway? ;)Roseannaroseanne
Haha, well, I use Pidgin and it still uses the protocol that MSN had, I believe. Strange the domain would be blocked though.Stesha
This answer doesn't mention any concrete reason for why they would want it nor how to specifically use it for cropping.Kristof
T
7

I found nconvert pretty handy so far.

Trunnel answered 12/12, 2009 at 12:21 Comment(0)
K
6
for f in final/**/*;
do
   convert -crop 950x654+0+660 "$f" "${f%.jpg}".jpg
done

This script loops through all the sub-folders and crops the .jpg files.

Kenaz answered 20/7, 2017 at 7:30 Comment(0)
C
5

macOS has sips image processing tool integrated. Cropping functions available are:

    -c, --cropToHeightWidth pixelsH pixelsW 
        --cropOffset offsetY offsetH 
Caresa answered 26/3, 2021 at 12:10 Comment(0)
C
3

Easy with sips: just set the offset to start the cropping:

sips --cropOffset 1 1 -c <height> <width> -o output.png input.png
Carnap answered 16/8, 2022 at 22:38 Comment(0)
R
0

I have scanned some pages and all ~130 pages needs the lower ~1/8 of the page cut off.

Using mogrify didn't work for me,

a@a-NC210-NC110:/media/a/LG/AC/Learn/Math/Calculus/Workshop/clockwise/aa$ mogrify -quality 100 -crop 2592×1850+0+0 *.jpg  
mogrify.im6: invalid argument for option `2592×1850+0+0': -crop @ error/mogrify.c/MogrifyImageCommand/4232.

However convert did:

a@a-NC210-NC110:~/Pictures/aa$ convert '*.jpg[2596x1825+0+0]' letter%01d.jpg  
a@a-NC210-NC110:~/Pictures/aa$

I learnt this here under the Inline Image Crop section.

Notice my syntax: I had to put my geometry in brackets: [].

Using the successful syntax above but with mogrify simply didn't work, producing:

a@a-NC210-NC110:~/Pictures/aa$ mogrify '*.jpg[2596x1825+0+0]' letter%01d.jpg
mogrify.im6: unable to open image `letter%01d.jpg': No such file or directory @ error/blob.c/OpenBlob/2638.

Linux a-NC210-NC110 3.13.0-32-generic #57-Ubuntu SMP Tue Jul 15 03:51:12 UTC 2014 i686 i686 i686 GNU/Linux
Lubuntu 14.04 LTS
Rigdon answered 10/8, 2014 at 10:27 Comment(1)
This was because you used the character '×' instead of a 'x'Diadelphous

© 2022 - 2024 — McMap. All rights reserved.