I know I can batch trim images with automator for 50% horizontal, but then it get's trimmed on both sides... I need it to get 50% trimmed on just the left/right side.
-
If not, is there another free way to accomplish this?– mesqueebCommented May 25, 2013 at 11:28
-
So, you mean that you want to cut off the left-hand sides of a batch of images?– evilsoupCommented May 25, 2013 at 11:34
-
Yes, batch cut off 50% from the left side once. And with other images cut off 50% from the right side.– mesqueebCommented May 25, 2013 at 12:32
1 Answer
I don't know about Automator; I'd use bash (on the command-line) for this sort of thing.
If you have ImageMagick installed (you should be able to get it from homebrew), you can use convert to cut off one side or another of an image.
This will give you the left-hand side of the image (so it will cut off the right half):
convert input.png -crop 50%x100%+0+0 output.png
becomes
This will give you the right-hand side (cut off the left half of the image):
convert input.png -gravity east -crop 50%x100%+0+0 output.png
becomes
To act on every file ending with .png
in a given directory, you can put it in a bash for loop:
for f in *.png; do convert "$f" -crop 50%x100%+0+0 "${f%.*}-cropped.png"; done
To do so recursively (every *.png
in the working directory, and every *.png
in any sub-directories), use globstar:
shopt -s globstar; for f in **/*.png; do convert "$f" -crop 50%x100%+0+0 "${f%.*}-cropped.png"; done
All of these will create a separate output file (in these examples, for every file.png
there will be created a file-cropped.png
). To work on files without creating a separate output, use mogrify
(the companion tool of convert
). Of course, this will over-write your existing files, so be careful in its use. Mogrify is able to take multiple inputs from the command-line, so there is no need to use a for loop here:
mogrify -crop 50%x100%+0+0 *.png
## Recursively:
shopt -s globstar; mogrify -crop 50%x100%+0+0 **/*.png
If you are working with a truly huge number of files (thousands), the above globs may choke; in that case, you should use find
:
## For convert:
find . -type f -name '*.png' -execdir bash -c 'convert "$0" -crop 50%x100%+0+0 "${0%.*}-cropped.png"' '{}' \;
## For mogrify:
find . -type f -name '*.png' -execdir mogrify -crop 50%x100%+0+0 '{}' \;
You can put any of this stuff into a bash script like so:
#!/usr/bin/env bash
for f in *.png; do convert "$f" -crop 50%x100%+0+0 "${f%.*}-cropped.png"; done
exit 0
Or, a nicer-looking version:
#!/usr/bin/env bash
for f in *.png; do
convert "$f" -crop 50%x100%+0+0 "${f%.*}-cropped.png"
done
exit 0
...save that in a plaintext file called something like crop-left.sh
and set it as executable:
chmod u+x crop-left.sh
To use it from the command-line, it would be
./crop-left.sh
I don't know how to fit this into Automator, since I've never used that program, but it probably can be done.
-
Great answer. Automator only allows you to specify the crop percentage, but you can't tell it where to crop, so I'm not sure if there's a built in way. Of course you can run these commands from within Automator's Run Shell Script action as well.– slhckCommented May 25, 2013 at 13:48
-
Can I save this as some sort of application, that I just have to click one button? (I can save Automator workflows as applications. Is it possible with command line stuff? ^^)– mesqueebCommented May 25, 2013 at 14:35
-
How to save this as a Shell Script that I can integrate it in automator? And if I do save it as shell script, can I refrain from specifying a folder in the shell script, so I can specify the folder in Automator?– mesqueebCommented May 25, 2013 at 14:39
-
You certainly can save it into a shell script - make sure that the first line of the file is
#! /usr/bin/env bash
, and then just paste whichever line you prefer to use, and set it as executable (from the command-line:chmod u+x scriptname.sh
- replace scriptname.sh with whatever you want to call the script). Unfortunately, I've never used Automator, so I don't know how to integrate shell scripts into it - hopefully @slhck or someone with that knowledge will come along and tell how to do it.– evilsoupCommented May 25, 2013 at 14:45 -
Thanks guys! I figured out you also can use Pixelmator's automater recipes to crop from one side!– mesqueebCommented May 25, 2013 at 15:10