Page 1 of 1

Smart cropping of a photo

Posted: 2017-01-21T12:59:59-07:00
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?

Re: Smart cropping of a photo

Posted: 2017-01-21T13:33:06-07:00
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.

Re: Smart cropping of a photo

Posted: 2017-01-21T13:51:32-07:00
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?

Re: Smart cropping of a photo

Posted: 2017-01-21T13:57:10-07:00
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.

Re: Smart cropping of a photo

Posted: 2017-01-21T14:13:17-07:00
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.

Re: Smart cropping of a photo

Posted: 2017-01-21T14:25:18-07:00
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),

Re: Smart cropping of a photo

Posted: 2017-01-21T16:18:34-07:00
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.

Re: Smart cropping of a photo

Posted: 2017-01-21T19:00:43-07:00
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

Re: Smart cropping of a photo

Posted: 2017-01-21T20:44:49-07:00
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.