AGamePlayer
AGamePlayer

Reputation: 7736

How to use imagemagick to bulk crop images' whitespace just like the "trim()" of a string?

I have many PNGs uploaded by users.

However, they have white spaces around, which should be removed.

For instance:

An image like this:

enter image description here

Should be cropped to an image like this:

enter image description here

What should I do if I want to do this to all files matching *_spec.PNG files in a directory and all its subdirectories?

Example files:

Folder
 |
 Subfolder1 - file1_spec.png
            - file2_spec.png
            - file3.png
 |
 Subfolder2 - filea.png
            - fileb1_spec.png

I need to do this to these files:

Update:

  1. OS: MacOSX Mojave 10.14.6
  2. Imagemagick version: 7.0.10-22 Q16 x86_64 2020-06-27

Upvotes: 2

Views: 1029

Answers (1)

Mark Setchell
Mark Setchell

Reputation: 207465

I would make a COPY of your files in a spare directory before you do anything. Then go to the top directory and run:

find . -iname "*_spec.png" -exec mogrify -trim {} \;

Note that there are more efficient ways of doing this, but they are less readable and only really worth it if you have tens of thousands of files, or more. For anyone who's interested, that means using GNU Parallel and/or trimming more than one file per invocation of ImageMagick to better amortize the process creation time over multiple files.

Upvotes: 4

Related Questions