Page 1 of 1
How to crop a quadrant out of an image
Posted: 2017-05-03T07:52:22-07:00
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.
Re: How to crop a quadrant out of an image
Posted: 2017-05-03T07:58:40-07:00
by snibgo
What version IM? What platform?
What do you mean by "quadrant"? This normally means a quarter, but you mean something else?
Re: How to crop a quadrant out of an image
Posted: 2017-05-04T00:54:34-07:00
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.
Re: How to crop a quadrant out of an image
Posted: 2017-05-04T04:34:53-07:00
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.
Re: How to crop a quadrant out of an image
Posted: 2017-05-04T12:21:46-07:00
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
Re: How to crop a quadrant out of an image
Posted: 2017-05-04T12:57:51-07:00
by snibgo
As I said, my code is Windows BAT syntax. For Windows CMD, change each double %% to single %.
Re: How to crop a quadrant out of an image
Posted: 2017-05-06T01:23:16-07:00
by olegk
You are right! I forgot to replace one instance of %%.
Now it works for me.
Thanks a lot!
Oleg.