Question

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
Was it helpful?

Solution

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

OTHER TIPS

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.

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.

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

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top