Cropping an image with %

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
Marko97
Posts: 3
Joined: 2019-11-09T07:20:11-07:00
Authentication code: 1152

Cropping an image with %

Post by Marko97 » 2019-11-09T07:25:54-07:00

Hi!
I need to crop an image with command line but I don't know how to do that. I've tryed a lot of times but any commands work.
This is the image:
Image

I need to crop the image maintaining the area inside the red rectangle using % via command line.
Thanks a lot :)

snibgo
Posts: 12299
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Cropping an image with %

Post by snibgo » 2019-11-09T07:57:47-07:00

What version of IM, on what platform?

For "-crop", see https://www.imagemagick.org/script/comm ... s.php#crop
snibgo's IM pages: im.snibgo.com

Marko97
Posts: 3
Joined: 2019-11-09T07:20:11-07:00
Authentication code: 1152

Re: Cropping an image with %

Post by Marko97 » 2019-11-09T08:08:57-07:00

snibgo wrote:
2019-11-09T07:57:47-07:00
What version of IM, on what platform?

For "-crop", see https://www.imagemagick.org/script/comm ... s.php#crop
I've already readed that but offset does not work.

snibgo
Posts: 12299
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Cropping an image with %

Post by snibgo » 2019-11-09T08:34:28-07:00

What version of ImageMagick do you use? What is your platform: bash, Windows command line, Windows BAT, etc?

Offsets are numbers of pixels, not percentages. In v7 with "magick", they can be %[fx:...] expressions.
snibgo's IM pages: im.snibgo.com

User avatar
GeeMack
Posts: 723
Joined: 2015-12-01T22:09:46-07:00
Authentication code: 1151
Location: Central Illinois, USA

Re: Cropping an image with %

Post by GeeMack » 2019-11-09T08:46:40-07:00

Marko97 wrote:
2019-11-09T07:25:54-07:00
I need to crop the image maintaining the area inside the red rectangle using % via command line.
You can specify the dimensions of a "-crop" as percentages of the input image, but the "-crop" operator won't let you specify the offsets as percentages. You can work around that by using "-distort" which does allow you to specify the dimensions and offsets as FX expressions. Here is an example command...

Code: Select all

convert input.png \
   -set option:distort:viewport %[fx:w*0.6]x%[fx:h*0.22]+%[fx:w*0.23]+%[fx:h*0.05] \
   -distort SRT 0 +repage result.png
That reads the input image, sets the viewport dimensions and offsets, then does a no-op "-distort" to size that viewport and locate the image within it according to the offsets.

That command is in *nix syntax using ImageMagick v6. If you're using IM v7 use "magick" instead of "convert". For Windows you'll need to change the continued-line backslashes "\" to carets "^".

Also, as fmw42 mentioned above, if you're using IM v7 you can use FX expressions to specify the offsets directly within the "-crop" operation.

Marko97
Posts: 3
Joined: 2019-11-09T07:20:11-07:00
Authentication code: 1152

Re: Cropping an image with %

Post by Marko97 » 2019-11-09T09:03:11-07:00

snibgo wrote:
2019-11-09T08:34:28-07:00
What version of ImageMagick do you use? What is your platform: bash, Windows command line, Windows BAT, etc?

Offsets are numbers of pixels, not percentages. In v7 with "magick", they can be %[fx:...] expressions.
I've used this command and now work:

Code: Select all

convert.exe test.jpg -crop 60%x20%+100+50 -threshold 80% thresholded.jpg

Post Reply