17

I want to write a batch command script for osx that will crop off the bottom of an image. Is that possible using sips?

I have a bunch of images 640 x 1136 and I want crop them (not scale or resize) to 640 x 960. When the image is cropped, I want the bottom of the image removed and the top to stay the same. Basically, I just want to cut the bottom of the image off.

I have this but it's cropping both from the top of the image and bottom.

sips --cropToHeightWidth 640 960
Dev01
  • 13,292
  • 19
  • 70
  • 124
  • 2
    For anyone else working on this... the pesky SIPS program also pads equally top and bottom, so you can't pad the top first and then crop from top and bottom to get what you want :-) – Mark Setchell Nov 18 '13 at 19:11
  • 7
    I believe this is not possible, sips is a bit too simple. I would go with ImageMagick or GraphicsMagick installed from Homebrew, even though it adds an extra dependency. Anyway, props for anyone who can find a solution. :) – juhovh Nov 18 '13 at 20:49
  • 2
    sips is a strange program – wasted an hour as well trying to trick it into cropping non-central part of an image and ye, it's not possible. – Sergey Grinev Sep 08 '14 at 17:07

6 Answers6

8

Doesn't look like it's possible. Thanks guys.

Dev01
  • 13,292
  • 19
  • 70
  • 124
5

It's only taken me 2 years to think this up... but you can do it with the GD library which is actually included in the standard, built-in OSX PHP interpreter (so no need to install any packages):

#!/usr/bin/php -f

<?php
   $im = imagecreatefromjpeg("image.jpg");
   $crop_area = array('x'=>0,'y'=> 0,'width'=>640,'height'=>960);
   $result = imagecrop($im, $crop_area);
   imagejpeg($result,"result.jpg");
?>

To call from Terminal, you would save it in a file, called say chopper and then make the file executable like this:

chmod +x chopper

and then you could run it by typing:

./chopper

or double-clicking it in Finder.

I guess you would want to make it take parameters of the filename to open and the filename to save and the dimensions, but you get the idea.

Mark Setchell
  • 191,897
  • 31
  • 273
  • 432
  • Cool. How do you call this from terminal? – Dev01 Feb 01 '16 at 11:16
  • 1
    As presented in the answer: Type `./chopper` (and press return) – Lars Blumberg Feb 16 '16 at 09:14
  • This is an excellent, simple example. With a little bit of searching I found there are also imagecreatefrompng, imagecreatefromgif, imagecreatefrombmp, imagecreatefromwebp, etc. functions in this PHP library. So if you know the type of image file, it should be possible to modify this example. – system PAUSE Aug 13 '19 at 04:20
1

You could also use the flip mode twice.

sips --flip vertical image.jpg
sips --cropToHeightWidth 640 960
sips --flip vertical image.jpg

This has done it for me.


Update

Like the comments says this solution does not work any longer. Sorry for that.

tammoj
  • 908
  • 10
  • 14
  • sips --cropToHeightWidth 15000 15000 img15000sq.png worked for me – anjchang Sep 12 '18 at 03:15
  • I'm not understanding how vertical flipping makes a difference. The question and the comments on the question point out that sips will crop *equal amounts* from *both* the top and bottom. The original question seeks to crop away just the bottom part, without cropping anything from the top. Are you using a different version of sips? What version of macos do you have? Can you post or link to a sequence of images showing your results? – system PAUSE Aug 13 '19 at 04:25
  • 1
    @systemPAUSE you are right! I just tested it again and now it's cropping both the top and bottom. One and a half year ago (I think I was still running High Sierra) this behaviour was different. The comment of anjchang confirms that my answers was right. But I'm sorry this solution no longer answers the questions. – tammoj Sep 18 '19 at 19:51
1

From the sips man page:

--cropOffset offsetY offsetH
      Crop offset from top left corner.

At least one of those values needs to be higher than 0, or the crop will occur from the centre.

user137369
  • 5,219
  • 5
  • 31
  • 54
0

Mark Setchell's answer does not work on older Apple systems such as Snow Leopard and Lion, where you will receive this error:

Fatal error: Call to undefined function imagecrop() in Command line code on line 1

To check the version of your PHP installion run php -v:

PHP 5.3.15 with Suhosin-Patch (cli) (built: Jul 31 2012 14:49:18) 

My Lion machine has PHP 5.3.15 and its in-built GD (GIF/Graphics Draw) library does not have the imagecrop function, which is only available with PHP 5.5.0 or greater.  I have thus used the imagecopy() function in place of imagecrop().

This solution should work with Linux and Unix machines that have the inbuilt PHP interpreter and GD library.

The key line in my image cropping PHP script is:

imagecopy($dest_image,$src_image,0,0,$new_x,$new_y,$new_width,$new_height);

Pass in the parameters of the cropping task to the script, where (x,y) is the starting point of the cropping rectangle:

php -f ./crop_image.php $infile $outfile $x $y $width $height

Example: Search for all files with the "png" extension in the current directory, crop each image from the top-left point (10,50) to the bottom-right point (730,1030) and then save all output files in the "cropped" folder.

j=1; for i in $(ls *.png); do php crop_image.php $i ./cropped/$j.png 10 50 720 980; ((j++)); done
rwarvi
  • 43
  • 1
  • 6
0

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

sips --cropOffset 1 1 -c 960 640 -o output.png input.png
Tyler2P
  • 2,324
  • 26
  • 22
  • 31
JHime
  • 21
  • 1