-crop by percent size and offsets

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
GreenKoopa
Posts: 457
Joined: 2010-11-04T17:24:08-07:00
Authentication code: 8675308

-crop by percent size and offsets

Post by GreenKoopa »

How to I achieve the intent of

Code: Select all

convert in.png -crop 40x40%+10%+10% out.png
since offsets must be specified in pixels?

My first thought was

Code: Select all

convert in.png -chop 10x10% -crop 40x40%+0+0 out.png
but the initial chop then alters the percentage for the crop.

Obviously something like (40%/(1-10%)=44.4%)

Code: Select all

convert in.png -chop 10x10% -crop 44.4x44.4%+0+0 out.png
works, but that gets a bit mathy to change the 10%.

If all my images happened to be the same size, I could just convert the offsets to pixels. But it was not meant to be.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: -crop by percent size and offsets

Post by fmw42 »

get the size of each image and put that into a variable

width=`convert image -format "%w" info:`
height=convert image -format "%w" info:`

Now convert your percentages to pixels as variables, for example

xoff=`convert xc: -format "%[fx:$width*10/100]" info:
yoff=`convert xc: -format "%[fx:$height*10/100]" info:
ww=`convert xc: -format "%[fx:$width*40/100]" info:`
hh=`convert xc: -format "%[fx:$height*40/100]" info:`

then put that into your crop

convert image -crop ${ww}x${hh}+${xoff}+${yoff} +repage result

Note the above are unix commands. For windows, see http://www.imagemagick.org/Usage/windows/
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: -crop by percent size and offsets

Post by anthony »

THE '%' symbol in any geometry style argument in IM is an all or nothing switch.

When given any argument using percent will use percent. You can not turn on percent for one part of the argument and not another. That is just the way the percentage flag works in ImageMagick core library.

As such 50%x50% %50x50 50x%50 50x50% all mean exactly the same thing. The location of the percent does not matter.

As for crop. The percent is specified it is applied to both width and height only. It does not effect offset, which remains as pixels.

This is only with geometry style arguments. Other argument formats, may or may not use percent. Typically they does use them at all except as a method of 'image property escape substitution' (format escapes)

This duality of percent being used for 'percent' and for 'escapes' depending on the type of argument is one of the existing problems faced with a future unification of argument handling, and eventual 'scripted' converts.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
User avatar
GreenKoopa
Posts: 457
Joined: 2010-11-04T17:24:08-07:00
Authentication code: 8675308

Re: -crop by percent size and offsets

Post by GreenKoopa »

Yuck! I guess not every simple need can be anticipated by an elegant solution. Thanks for the responses all the same.
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: -crop by percent size and offsets

Post by anthony »

A geometry argument in IM is probably one of the hardest arguments IM parses.
It has a weird syntax, and has many options and tweeks needed to get it to work
in the desired fashion.

At this point to allow separate % handling of arguments would require a major re-think
of a huge number of options, and all the backward compatibility issues involved.

The comma separated argument format (as used by distort) is much much simpler by contrast. But then it does not handle percentages or other special options.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
User avatar
GreenKoopa
Posts: 457
Joined: 2010-11-04T17:24:08-07:00
Authentication code: 8675308

Re: -crop by percent size and offsets

Post by GreenKoopa »

Despite all the small quirks and sometimes (to me) unexpected behaviors, I have usually figured something out. I am absolutely impressed with the power packed into the command-line interface. And the learning curve was easier than I had anticipated. In only one week I can usually find the documentation I need or at least form an intelligent question. I thank the design, documentation, and examples.
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: -crop by percent size and offsets

Post by anthony »

That was the goal of IM Examples.

Before this IM was often only used for relativeally simple operations, unless you write scripts or program, and even then it was difficult. The examples, and the option handling change from version 5 to 6 changed all this.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
Post Reply