Crop with negative 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
kasheftin
Posts: 2
Joined: 2016-12-14T09:40:44-07:00
Authentication code: 1151

Crop with negative offsets

Post by kasheftin »

Hi, I'm trying to create backend for https://fengyuanchen.github.io/cropper/ plugin.
I have an image and data with cropping options from this plugin that's like {x:-100,y:200,width:300,height:300} array.
x and y are relative to the top left corner of an image.
The obstacle is that cropping area isn't necessary fit in the image. For example, width or height of the cropping area might be greater than the image's width and height. x and y may be negative. All these cases suppose to extend the cropped image with white color.

Please help to write the correct command for this case.

I tried:
$ convert pic.jpg -crop '300x300-100+200' -extend '300x300' out.jpg
- but this does not work correctly: it gives an image that has white margin from the right side while it should be on the left.
User avatar
GeeMack
Posts: 718
Joined: 2015-12-01T22:09:46-07:00
Authentication code: 1151
Location: Central Illinois, USA

Re: Crop with negative offsets

Post by GeeMack »

kasheftin wrote:I tried:
$ convert pic.jpg -crop '300x300-100+200' -extend '300x300' out.jpg
- but this does not work correctly: it gives an image that has white margin from the right side while it should be on the left.
You might try using your geometry specifiers with the "-extent" operation and don't even use the "-crop". Something like this might do what you want...

Code: Select all

convert pic.jpg -extent '300x300-100+200' out.jpg
Always remember to let us know which version of ImageMagick you're using.
kasheftin
Posts: 2
Joined: 2016-12-14T09:40:44-07:00
Authentication code: 1151

Re: Crop with negative offsets

Post by kasheftin »

Thanks a lot, it works!
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Crop with negative offsets

Post by anthony »

There is a cavat with using -extent, for image with transparency...
But as JPEG images have no transparency that should not be a problem for you.

See IM Exmaples
http://www.imagemagick.org/Usage/crop/#extent
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
Post Reply