How to crop a quadrant out of an 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
olegk
Posts: 37
Joined: 2013-08-16T11:31:10-07:00
Authentication code: 6789

How to crop a quadrant out of an image

Post by olegk »

Hi,
is there an automatic (e.g. without external calculation) way to accomplish the following:

=> crop a quadrant from the centre of an image whose side = 1.1*min(image_width, image_height); with black border needed wherever required.

Semantically it would be equivalent to the following not-so-optimal operations:
(1) add black border on both sides at the smaller dimension; border sickness >= 0.1*min(image_width, image_height)
(2) crop the quadrant from the image with border; size = 1.1*min(image_width, image_height)

But as usual with ImageMagick, there could be an elegant one-line solution that doesn't create huge intermediate images.

Thank you in advance,
Oleg.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: How to crop a quadrant out of an image

Post by snibgo »

What version IM? What platform?

What do you mean by "quadrant"? This normally means a quarter, but you mean something else?
snibgo's IM pages: im.snibgo.com
olegk
Posts: 37
Joined: 2013-08-16T11:31:10-07:00
Authentication code: 6789

Re: How to crop a quadrant out of an image

Post by olegk »

Sorry for the confusion; I meant a square - a sub-image with horizontal and vertical dimensions being equal.
Regarding the IM version, I'd assume the latest one. My platform is MS Windows for now, though portability would be nice.

Thanks again,
Oleg.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: How to crop a quadrant out of an image

Post by snibgo »

This does what I think you want. Windows BAT syntax.

Code: Select all

magick ^
  in.png ^
  -virtual-pixel Black ^
  -set option:distort:viewport ^
    %%[fx:1.1*min(w,h)]x%%[fx:1.1*min(w,h)]+%%[fx:(w-1.1*min(w,h))/2]+%%[fx:(h-1.1*min(w,h))/2] ^
  -filter Point -distort SRT 1,0 ^
  +repage ^
  out.png
Tested in v6.9.5-3 (using "convert" instead of "magick") and v7.0.3-5.
snibgo's IM pages: im.snibgo.com
olegk
Posts: 37
Joined: 2013-08-16T11:31:10-07:00
Authentication code: 6789

Re: How to crop a quadrant out of an image

Post by olegk »

Thanks for your effort Mr snibgo, but ... it doesn't work - the resulting images have the same size as the inputs.

I pasted the command into DOS window:
---->set CONVERT="C:\Program Files (x86)\ImageMagick-7.0.5-5-portable-Q16-x86\convert.exe"

---->for %f in (*.TIF) DO %CONVERT% %f ^
More? -virtual-pixel Black ^
More? -set option:distort:viewport ^
More? %[fx:1.1*min(w,h)]x%%[fx:1.1*min(w,h)]+%[fx:(w-1.1*min(w,h))/2]+%[fx:(h-1.1*min(w,h))/2] ^
More? -filter Point -distort SRT 1,0 ^
More? +repage ^
More? -compress LZW SQUARE\%~nf.TIF


---->"C:\Program Files (x86)\ImageMagick-7.0.5-5-portable-Q16-x86\convert.exe" DSC00003-00009_l.TIF -virtual-pixel Black -set option:distort:viewport %[fx:1.1*min(w,h)]x%%[fx:1.1*min(w,h)]+%[fx:(w-1.1*min(w,h))/2]+%[fx:(h-1.1*min(w,h))/2] -filter Point -distort SRT 1,0 +repage -compress LZW SQUARE\DSC00003-00009_l.TIF

---->"C:\Program Files (x86)\ImageMagick-7.0.5-5-portable-Q16-x86\convert.exe" DSC00004-00010_l.TIF -virtual-pixel Black -set option:distort:viewport %[fx:1.1*min(w,h)]x%%[fx:1.1*min(w,h)]+%[fx:(w-1.1*min(w,h))/2]+%[fx:(h-1.1*min(w,h))/2] -filter Point -distort SRT 1,0 +repage -compress LZW SQUARE\DSC00004-00010_l.TIF
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: How to crop a quadrant out of an image

Post by snibgo »

As I said, my code is Windows BAT syntax. For Windows CMD, change each double %% to single %.
snibgo's IM pages: im.snibgo.com
olegk
Posts: 37
Joined: 2013-08-16T11:31:10-07:00
Authentication code: 6789

Re: How to crop a quadrant out of an image

Post by olegk »

You are right! I forgot to replace one instance of %%.
Now it works for me.
Thanks a lot!

Oleg.
Post Reply