Centered crop

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
dargaud
Posts: 31
Joined: 2006-08-06T01:58:58-07:00

Centered crop

Post by dargaud »

Hello all,
I've been reading about -crop and image geometry for a few hoursas well as wrestling with the examples, but I can't get this right: I would like to get a fixed-sized crop around a point, even it it goes partially outside the image border. For instance I want a resulting Width and Height of 100, around point (30,60), so a crop that goes from (-20,10) to (80,110). I don't care what it fills the gap with. I've been wrestling with the +/- signs and ! in the geometry expression to no avail.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Centered crop

Post by fmw42 »

try

convert image.png -crop 100x100-20+10\! -background lightblue -flatten image_crop_flat.png

see http://www.imagemagick.org/Usage/crop/#crop_viewport

change the background color to whatever you want
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Centered crop

Post by anthony »

for cropping around a specific center point on alternative is to use a distort with a viewport into the distorted image. distort viewport

for example translate the center point to crop around to origin, then viewport around the origin.

Code: Select all

convert logo: -set option:distort:viewport 101x101-50-50 \
            -distort affine '388,226 0,0' +repage  logo_nose.png
Image

substitute logo: for input image\
388,226 for the center point
show: for the output image
and adjust viewport for size.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
Post Reply