Trim, clone then trim

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
spender
Posts: 4
Joined: 2015-11-16T05:37:14-07:00
Authentication code: 1151

Trim, clone then trim

Post 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?
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Trim, clone then trim

Post 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?
spender
Posts: 4
Joined: 2015-11-16T05:37:14-07:00
Authentication code: 1151

Re: Trim, clone then trim

Post 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.
User avatar
GeeMack
Posts: 718
Joined: 2015-12-01T22:09:46-07:00
Authentication code: 1151
Location: Central Illinois, USA

Re: Trim, clone then trim

Post 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.
spender
Posts: 4
Joined: 2015-11-16T05:37:14-07:00
Authentication code: 1151

Re: Trim, clone then trim

Post by spender »

That looks perfect. I'll give this a shot later and let you know how I got on. Thanks.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Trim, clone then trim

Post 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:
snibgo's IM pages: im.snibgo.com
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Trim, clone then trim

Post 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
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
Post Reply