Smart cropping of a photo

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
User avatar
rossmcm
Posts: 28
Joined: 2012-07-26T06:10:38-07:00
Authentication code: 15
Location: Auckland, NZ

Smart cropping of a photo

Post by rossmcm »

ImageMagick 6.8.8-1 Q16 x64

I have a series of photographs like the one shown.

Image

(is is actually oriented as portrait - the forum seems to want to render it as landscape) The photos were taken with the picture placed on a table. I want to either crop it so that I am left with this:

Image

(i.e. using the table top to simulate a wooden frame), or alternatively this:

Image

(i.e. the minimum crop required to remove the irregular edges of the paper). As a start, I have tried

Code: Select all

convert image1.jpg  -fuzz 10% -trim +repage  image_fuzzy_trim.jpg
with various setting of fuzz, but nothing seeks to happen, i.e. the output image is the same as the input.

The placement of the picture within the frame is not constant.

Can I do this with ImageMagick?
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Smart cropping of a photo

Post by snibgo »

You can use a greater fuzz, eg 50%.

"-format %@ info:" gives the trim that would be applied, so you can catch that in a script and apply a less-aggressive trim.

Applying an inner trim is more complex, because there are many possible outcomes. See my "Inner trim" page.
snibgo's IM pages: im.snibgo.com
User avatar
rossmcm
Posts: 28
Joined: 2012-07-26T06:10:38-07:00
Authentication code: 15
Location: Auckland, NZ

Re: Smart cropping of a photo

Post by rossmcm »

Thanks for replying. First, let me admit that the reason I was getting no trim at all is because I was calling Convert.exe from a batch file, so the '%' was being swallowed. How I ever passed Batch 101 I'll never know.

Anyway

Code: Select all

convert image1.jpg  -fuzz 50% -trim +repage  image_fuzzy_trim.jpg
(with the percent correctly escaped) gave me this:

Image

which is pretty close. It then occurred to me that a more satisfactory result might be to replace any color matching the trim color with transparency, and then trimming. That would eliminate the brown bits on the edges, right?
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Smart cropping of a photo

Post by fmw42 »

If your image has EXIF orientation information, you can use -auto-orient to correct from landscape to it s proper portrait. See http://www.imagemagick.org/script/comma ... uto-orient. Note, that I think you need to put -auto-orient before reading the input, but I may be wrong about that.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Smart cropping of a photo

Post by fmw42 »

You will have a hard time separating the brown border/background with the brown on the right side of your image. Using a more contrasting and constant color background when photographing would make it much easier.
User avatar
rossmcm
Posts: 28
Joined: 2012-07-26T06:10:38-07:00
Authentication code: 15
Location: Auckland, NZ

Re: Smart cropping of a photo

Post by rossmcm »

It shows as portrait on my PC (thumbnail in Explorer, irfan view). It's just strange that it shows as landscape in the forum.

Good point about the brown in the image. Assuming that I can successfully trim as shown in my last image, how can I "grow" the trim border so that I end up with, say, a 64-pixel border of table top (i.e. like the second example in my original post),
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Smart cropping of a photo

Post by fmw42 »

It shows as portrait on my PC (thumbnail in Explorer, irfan view). It's just strange that it shows as landscape in the forum
Many viewers will read the orientation from the EXIF and automatically reorient it. I suppose this forum's image presentation software just ignores the EXIF orientation, so does not properly re-orient it.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Smart cropping of a photo

Post by snibgo »

rossmcm wrote:... how can I "grow" the trim border ...
As I said, by capturing the output of "-format %@" and doing a bit of arithmetic. For example, Windows BAT script:

Code: Select all

set SRC=kidPic.jpg
set EXTRA=30

for /F "usebackq tokens=1-4 delims=x+" %%A in (`%IM%convert ^
  %SRC% ^
  -fuzz 50%% ^
  -format %%@ ^
  info:`) do (
  echo %%A %%B %%C %%D
  set /A W=%%A+2*%EXTRA%
  set /A H=%%B+2*%EXTRA%
  set /A X=%%C-%EXTRA%
  set /A Y=%%D-%EXTRA%
)

echo %W%x%H%+%X%+%Y%

%IM%convert ^
  %SRC% ^
  -crop %W%x%H%+%X%+%Y% ^
  +repage ^
  out.png
snibgo's IM pages: im.snibgo.com
User avatar
rossmcm
Posts: 28
Joined: 2012-07-26T06:10:38-07:00
Authentication code: 15
Location: Auckland, NZ

Re: Smart cropping of a photo

Post by rossmcm »

Brilliant. Worked a treat. Put your code inside of a ForFiles loop and had the whole lot done in a couple of minutes.

Many thanks for your help.
Post Reply