need to auto crop

Questions and postings pertaining to the usage of ImageMagick regardless of the interface. This includes the command-line utilities, as well as the C and C++ APIs. Usage questions are like "How do I use ImageMagick to create drop shadows?".
Post Reply
graysky

need to auto crop

Post by graysky »

I have a pretty specific need to mass-process a ton of images in a directory tree. The major problem I'm having is that the source images have, for the most part, different aspect ratios. Why? People did a bunch of silly freehand crops on perfectly good images! Anyway, I would like to batch process as follows:

Crop the source images such that each outputted image will have an aspect ratio of either 4:3 if landscape or 3:4 if portrait losing the minimum number of pixels in the process. I understand that there is no way to accurately know what would make the most pleasing crop from a gravity perspective (starting from the top, left, right, etc.), but I don't care given the large number of photos I'm dealing with in this scenario.

Suggestions are greatly appreciated.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: need to auto crop

Post by fmw42 »

see my scripts, aspect and aspectpad. They may be of help. But you will need to write a wrapper script to traverse your directory tree. My scripts work on one image at a time.
graysky

Re: need to auto crop

Post by graysky »

This is great. Here's what I came up with to do my resizing. Also thank you kindly for the emails with those excellent suggestions, and for your scripts.

Code: Select all

#!/bin/bash

if [ ! -d processed_images ]; then
 mkdir processed_images
fi

for PIC in `ls *.{jpg,JPG,jpeg,JPEG}`; do
NAME=`echo ${PIC} | sed 's/.jpg//' | sed 's/.JPG//' | sed 's/.jpeg//' | sed 's/.JPEG//'`
 test=`convert ${PIC} -format "%[fx:(w/h)>=1?1:0]" info:`

 if [ $test -eq 1 ]; then
  echo ${PIC} "is landscape... making thumbnail and image"
  aspect.sh 150x113 -m crop ${PIC} ./processed_images/${NAME}a.jpg
  aspect.sh 600x450 -m crop ${PIC} ./processed_images/${NAME}b.jpg
 else
  echo ${PIC} "is portraitn... making thumbnail and image"
  aspect.sh 113x150 -m crop ${PIC} ./processed_images/${NAME}a.jpg
  aspect.sh 450x600 -m crop ${PIC} ./processed_images/${NAME}b.jpg
 fi
done
Post Reply