Performing a script based on a selection

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
whugemann
Posts: 289
Joined: 2011-03-28T07:11:31-07:00
Authentication code: 8675308
Location: Münster, Germany 52°N,7.6°E

Performing a script based on a selection

Post by whugemann »

Hi,

I would like to manipulate an image like this:
Image

that is I would like to make a selection on a larger photograph, scale it down and insert the original version of some section of the image as a detail enlargement. I am working under Windows and would like to use some standard program to pass the parameters of the selection to a script that adresses convert and performs the manipulation based on the selection I chose.

Any ideas for a suitable approach? I tried IrfanView, but it offers no way to pass parameters to one of the "external editors". IMDisplay also doesn't offer something in this regard. I checked whether I could read the parameters from IrfanView's title bar via a Windows API call. This is possible, but cannot be done via VBScript. This would require VisualBasic or some other real programming language.

I could do it via GIMP, I guess, but this would require me to write something in Script-Fu and learn a scripting language intirely new to me. Moreover, the people intended to work with that script are more familiar with IrfanView and alike.

Does anyone have an idea for a suitable appraoch?

Wolfgang Hugemann
Wolfgang Hugemann
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Performing a script based on a selection

Post by anthony »

Draw your red box on an image.

Then use

Code: Select all

  -fill black +opaque red  -fill while -opaque red
to convert the image into a mask (add -fuzz if you need to). Use -trim on that mask to get the bounds (Virtual Canvas Info after the Trim) of the input red square. Once you have the bounds you can manipulate the image as you like.

A example of using trim bounds is given in IM Examples, under Fuzzy Trim
http://www.imagemagick.org/Usage/crop/#trim_blur

NOTE you can not currently directly use the trim bounds in the same command. Though I have ideas for a change that will allow it. Hold... I think I know of a way to use Distort Viewport with Percent escapes as a -crop alternative.

This code is not tested, but should work!

Code: Select all

convert input_with_red_box.png  \
    \( +clone  -fill black +opaque red  -fill while -opaque red -trim \) \
    -set option:distort:viewport %G%O +delete \
    \( +clone -filter point -distort SRT 0  +repage  +filter +resize 300% \) \
    -gravity NorthEast -composite    result.png
The last line may be adjusted, perhaps using -set repage and some FX escapes for maths and -layers merge for the final placement of the insert image that -distort cropped.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
User avatar
whugemann
Posts: 289
Joined: 2011-03-28T07:11:31-07:00
Authentication code: 8675308
Location: Münster, Germany 52°N,7.6°E

Re: Performing a script based on a selection

Post by whugemann »

Anthony,

I understood your first approach, in which you used -trim to determine the geometry of the red box. I think this is basically what I need, because I then have the geometry of the selection and can extract it from the output of Identify. And I don't mind the script producing some interim results.

I basically understood the code of your more sophisticated script. The code however doesn't work instantly when transferred to Windows: Convert laments about not knowing about an image named '300%'. So could you please comment a little bit more about what you are actually doing? (I think you are overestimating my knowledge about the command line usage ;-)

Anyway, your basic idea for the solution will already do, thanks a lot!

Wolfgang
Wolfgang Hugemann
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Performing a script based on a selection

Post by anthony »

whugemann wrote: Convert laments about not knowing about an image named '300%'. So could you please comment a little bit more about what you are actually doing? (I think you are overestimating my knowledge about the command line usage ;-)
Opps. sorry the +resize should be -resize!

Okay.
make a clone of the input, convert to a white on black mask of 'red'
(You may need to use some morphology at this point to clean up any other 'red' specks, while preserving the red box)
now trim.

Set the distort:viewport expert setting to the trimmed image size and offset (from last image)
See http://www.imagemagick.org/Usage/distor ... t_viewport
And for using -set instead of -define see Using Set to Define an Artifact

Now that we have captured the results of the trim, the mask image is deleted.

The 'viewport' is what is used to 'crop' or limit the output of the distort command, so clone the original (and no only image again), turn off filtering (to provent even the micro-blurring it can cause in a no-op case), do the distort using a no-op. I then turn on filtering again, and resize the image.

At this point you have the original image, and a enlarged crop. The rest is just positioning and overlay... Do what you will!
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
Post Reply