Page 1 of 1

Crop a percentage of image

Posted: 2020-02-01T18:44:53-07:00
by Ommadawn
Hello everyone,

I want to crop the footer of a batch of files and I don't know how to proceed. The quantity is not fixed (measured in pixels), but it's always the 3,5% of the image height.

So, if the image is 1800x1000 pixels, I want to crop 35 pixels from the bottom.

I think I have to use:

Code: Select all

convert image.jpg -crop 1800x1000+0-35 image_cropped.jpg
I don't know if its correct, but what I don't know is how to do it using percentages instead of calculating the pixels.

Could anyone help me?

Thanks!

Re: Crop a percentage of image

Posted: 2020-02-01T19:03:58-07:00
by snibgo
What version of IM, on what platform?

I don't know if you want to remove 3.5% of rows from the bottom, or if you want to keep only the bottom 3.5% of rows.

In either case, your "-crop 1800x1000" is wrong because that would give all 1000 rows. If you use IM v7 and "magick", you can use "%[fx:...]" expressions, eg "-crop 1800x%[fx:h*0.965]+0+0" to get the top 96.5% of rows.

Re: Crop a percentage of image

Posted: 2020-02-01T19:20:32-07:00
by Ommadawn
I'm using IM 7 in GNU/Linux (Ubuntu), via im4java (http://im4java.sourceforge.net/)

I want to remove the 3.5% of rows from the bottom.

Re: Crop a percentage of image

Posted: 2020-02-01T20:33:09-07:00
by fmw42
try

Code: Select all

convert image.jpg -gravity south -chop 0x3.5% result.jpg
See

https://imagemagick.org/Usage/crop/#chop
https://imagemagick.org/script/command- ... p#geometry

Re: Crop a percentage of image

Posted: 2020-02-02T02:48:37-07:00
by Ommadawn
Thank you very much! That works like a charm!

I want to do that crop, some resize and optimize file size. Should I do this in 3 separated commands or is it better to execute the 3 tasks in one step (if it's possible)?

Thanks.

PS: I didn't know if I should ask this in this thread or create another one.

Re: Crop a percentage of image

Posted: 2020-02-02T03:17:04-07:00
by snibgo
If you do it in separate commands, you will need to write each result to an intermediate file, then read it in the next command. In general it is best to do everything in one command.

For IM v7, I suggest you use "magick", not "convert".

Re: Crop a percentage of image

Posted: 2020-02-02T03:27:44-07:00
by Ommadawn
Thank you, snibgo!

I'm using the im4java library so, I don't know if it allows to use "magick" command.

Why is it better to use "magick" instead of "convert"? Would they make different results?

Re: Crop a percentage of image

Posted: 2020-02-02T03:39:36-07:00
by snibgo
The difference is mostly at the CLI (command-line interface). V7 has stricter syntax than v6, so some v6 commands won't work in v7. But v7 has some good new features, such as being able to use "%[fx:...]" expressions more widely.

Re: Crop a percentage of image

Posted: 2020-02-02T03:49:26-07:00
by Ommadawn
Thank you very much. Here we learn a lot! :-)