Contents

Trim image borders by imagemagick

When converting image format from png to jpeg, imagemagick (convert command)add margin border with bold black color automatically.

Environment

  • MacOS Monterey 12.0, M1 Apple silicon
  • Version: ImageMagick 7.1.0-12 Q16-HDRI arm

Problem

Getting screenshot by Command + Shift + 5 in OSX, it allows to get a screenshot for a specific window like:

../../../images/imagemagick/IACDriverProperties.png
(Screenshot) IACDriverProperties.png

Above image contains transparent background. Converting .png to .jpg by imagemagick, it generates image border in black color that I do not expect.

1
convert IACDriverProperties.png -quality 95% IACDriverProperties.jpg

../../../images/imagemagick/IACDriverProperties.jpg
IACDriverProperties.jpg


Solution

For removing margins, add options of -trim , -fuzz and -border to convert command

1
2
3
4
5
 convert IACDriverProperties.png \
     -fuzz 30% \
     -trim \
     -border 1x1 \
     -quality 95% IACDriverProperties_trim.jpg
  • -fuzz distance : colors within this distance are considered equal
  • -trim : trim image edges
  • -border geometry : surround image with a border of color

../../../images/sonicpi/IACDriverProperties_trim.jpg
IACDriverProperties_trim.jpg


References