Page 1 of 1

Trim, clone then trim

Posted: 2016-12-08T12:26:42-07:00
by spender
Say I do some processing on an image in order to make -trim more accurate... the details are unimportant, but it all ends with a trim

Code: Select all

magick convert someImage.jpg -colorspace Gray -negate -trim
I can extract information about the trimmed area to stdout with

Code: Select all

magick convert someImage.jpg -colorspace Gray -negate -trim -format "%wx%h%O" info:
And I can use this information in a new invocation of magick to crop the original image.

Is it possible to do this in a single invocation via the use of parentheses and +clone?

Re: Trim, clone then trim

Posted: 2016-12-08T12:29:45-07:00
by fmw42
I think you really want -format "%@" w and h are probably the size of the input.

Code: Select all

magick convert someImage.jpg -colorspace Gray -negate -format "%@" info:
I do not quite understand. You can trim without saving the crop arguments.

Code: Select all

magick convert someImage.jpg -colorspace Gray -negate -trim +repage croppedimage.jpg
What exactly do you want to do with the crop arguments in the second command besides just cropping?

Re: Trim, clone then trim

Posted: 2016-12-08T12:35:12-07:00
by spender
Hi, thanks for the speedy reply. I want to apply the trim that happened to the processed image to original image. I'm using a morphology and edge detection to make the image more friendly to trimming, but I effectively want to record the trim on the processed image and apply it to the unprocessed image.

To be clear, I'd rather not have to extract any crop parameters and instead (if possible) use the output of the trim of the processed image as a means of cropping the original.

Re: Trim, clone then trim

Posted: 2016-12-08T12:43:16-07:00
by GeeMack
spender wrote:Hi, thanks for the speedy reply. I want to apply the trim that happened to the processed image to original image. I'm using a morphology and edge detection to make the image more friendly to trimming, but I effectively want to record the trim on the processed image and apply it to the unprocessed image.
As fmw42 mentioned, you can access the calculated trim bounding box with the FX escape "%@". A command using that technique might look something like this...

Code: Select all

magick input.png ( +clone -do-some-stuff -set option:trimmer "%@" +delete ) -crop %[trimmer] output.png
That gets the specs for the trim bounding box from your processed clone, then holds that value in a variable named "trimmer", then deletes the clone to remove it from further processing. After that you use that variable to crop your original image before the output. You can learn more about how to access all sorts of information about the image at this page about FX Escapes.

Re: Trim, clone then trim

Posted: 2016-12-08T12:48:13-07:00
by spender
That looks perfect. I'll give this a shot later and let you know how I got on. Thanks.

Re: Trim, clone then trim

Posted: 2016-12-08T12:51:09-07:00
by snibgo
I don't understand what you want, so this may not help, but you can write an image and info about that image, eg:

Code: Select all

magick convert someImage.jpg -colorspace Gray -negate -trim -format "%wx%h%O" +write out.png info:

Re: Trim, clone then trim

Posted: 2016-12-08T22:05:37-07:00
by anthony
This is basically what is discussed in
Trimming Noisy images...
http://www.imagemagick.org/Usage/#trim_noise

NOTE: the techniques goes on to with an improvement by 'expanding' the resulting trim bounds using some FX maths.
That is why separate percent escapes for each number may be preferable.

At the time the example was generated two separate commands was needed, but I have added the IMv7 version of the command to the examples, for a one command version. The examples should update in a few hours.

Code: Select all

  convert logo:   -resize 30%   noisy.jpg
Image

Code: Select all

  magick noisy.jpg \
         \( +clone -virtual-pixel edge -blur 0x15 -fuzz 15% -trim \
            -set option:fuzzy_trim \
                     '%[fx:w+20]x%[fx:h+20]+%[fx:page.x-10]+%[fx:page.y-10]'\
            +delete \) \
         -crop %[fuzzy_trim] noisy_trimmed_4.jpg
Image