Crop a percentage of image

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
Ommadawn
Posts: 5
Joined: 2020-02-01T18:39:02-07:00
Authentication code: 1152

Crop a percentage of image

Post 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!

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

Re: Crop a percentage of image

Post 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.
snibgo's IM pages: im.snibgo.com

Ommadawn
Posts: 5
Joined: 2020-02-01T18:39:02-07:00
Authentication code: 1152

Re: Crop a percentage of image

Post 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.

User avatar
fmw42
Posts: 26249
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Crop a percentage of image

Post 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

Ommadawn
Posts: 5
Joined: 2020-02-01T18:39:02-07:00
Authentication code: 1152

Re: Crop a percentage of image

Post 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.

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

Re: Crop a percentage of image

Post 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".
snibgo's IM pages: im.snibgo.com

Ommadawn
Posts: 5
Joined: 2020-02-01T18:39:02-07:00
Authentication code: 1152

Re: Crop a percentage of image

Post 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?

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

Re: Crop a percentage of image

Post 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.
snibgo's IM pages: im.snibgo.com

Ommadawn
Posts: 5
Joined: 2020-02-01T18:39:02-07:00
Authentication code: 1152

Re: Crop a percentage of image

Post by Ommadawn »

Thank you very much. Here we learn a lot! :-)

Post Reply