Trim left and top based on index image

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
iknowmac
Posts: 3
Joined: 2016-07-18T16:16:25-07:00
Authentication code: 1151

Trim left and top based on index image

Post by iknowmac »

Hi Guys... I'm trying to trim the top and left of a registration based on a barcode and block rectangle. See image...

https://goo.gl/photos/rFDiKzzhUeKoWWiu7

Here is the code I'v tried, but have gotten inconsistent behavior...

Code: Select all

convert input.jpg -gravity NorthWest -crop \
  `convert input.jpg -canny 0x1+10%+30% -virtual-pixel edge -blur 0x5 -fuzz 10% -trim \
  -format '%[fx:w-20]x%[fx:h-20]+%[fx:page.x-10]+%[fx:page.y-10]' \
  info:` +repage output_trimmed.jpg
Here is an image of what I am looking to achieve...

https://goo.gl/photos/zitxKiGLKhNbg76SA

ImageMagick version...
Version: ImageMagick 6.9.5-2 Q16 x86_64 2016-07-13 http://www.imagemagick.org
Copyright: Copyright (C) 1999-2016 ImageMagick Studio LLC
License: http://www.imagemagick.org/script/license.php
Features: Cipher DPC Modules
Delegates (built-in): bzlib freetype jng jpeg ltdl lzma png tiff xml zlib
Any assistance would be appreciated...
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Trim left and top based on index image

Post by snibgo »

You might also show an example where your command doesn't do what you want.

You might find it helpful at the start to turn the most of the paper white, and some ink black:

Code: Select all

convert input.jpg -contrast-stretch 1%x50% -canny 0x1+10%+30% -virtual-pixel edge  {etc...}
And, of course, don't use JPEG unless you really have to.
snibgo's IM pages: im.snibgo.com
iknowmac
Posts: 3
Joined: 2016-07-18T16:16:25-07:00
Authentication code: 1151

Re: Trim left and top based on index image

Post by iknowmac »

This is tremendous snibgo... It's getting closer and closer... Below are three examples of what I mean by "inconsistent behavior" using your suggestion.

https://goo.gl/photos/En9HE9mphZEwbgtL7
https://goo.gl/photos/DhNbMYnsB4trzFa68
https://goo.gl/photos/yTZPVF8YFmZx7XzQA

Sometimes the top and left are right on, and other times is over or under. Would it be possible to use just the black rectangle on the left to get an accurate alignment each time? I really don't need the barcode, I thought it would just be easier to align on.

Thanks for all you advice...
iknowmac
Posts: 3
Joined: 2016-07-18T16:16:25-07:00
Authentication code: 1151

Re: Trim left and top based on index image

Post by iknowmac »

After a lot of tweaking I finally have something usable...

Code: Select all

convert input.png -crop \
  `convert input.png -morphology Smooth Octagon:3 -canny 0x1+10%+30% -trim -format '%[fx:w]x%[fx:h]+%[fx:page.x]+%[fx:page.y]' info:` \
  +repage output_trimmed.png
However is there a way to only apply the trim to the top and left of the image and leave the rest alone?
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Trim left and top based on index image

Post by snibgo »

"-crop" takes parameters WxH+X+Y. If W==0, it will crop from the x-offset to the remainder of the width. Likewise H.

So just use 0x0 instead of %[fx:w]x%[fx:h]
snibgo's IM pages: im.snibgo.com
Post Reply