1
votes

So I am trying to trim the borders of an image with imagemagick trimimage function (PHP), based on the background color. Below is a demonstration of what I am trying to do, and what the problem is.

When I upload this image (this image has thick white borders on the top and the right): enter image description here

And run the following code:

$canvas->setBackgroundColor('#ffffff');
$quantumRange = $canvas->getQuantumRange();
$canvas->trimImage(0.3 * $quantumRange['quantumRangeLong']);
$canvas->setImagePage(0, 0, 0, 0);

The result is exactly what I expect: The white borders are being trimmed out. enter image description here

Using the below image however (no outside borders): enter image description here

and running the same code, the image gets trimmed from the inside(?) with some weird effects: enter image description here

Now, form what I understood, trimimage should take the background color and trim it from the outside in, and only if the outer margins of the image (borders) do match this color.

So, either I understood trimimage wrong, or I am missing something in the implementation. Also, playing around with the fuzz value is not an option. At some point it will stop trimming the blue border, but at this point it will also NOT trim enough if there are outer borders since I need to have a value high enough to also remove light shadows or compression artifacts.

So the actual questions are: 1. Is there a way to crop the image only from the outside in when the outside border color matches the background color? Maybe I missed something. 2. Is there another method you could recommend?

EDIT: After @fmw42 answered below, I took a closer look at what exactly edge detection is and how it works. My assumption was that imagemagick looks at the borders of the image and cuts them based on the given background color , which is wrong. To better understand, check how edge detection works: https://en.wikipedia.org/wiki/Edge_detection

Thank you.

1
@mspir wrote EDIT: After @fmw42 answered below, I took a closer look at what exactly edge detection is and how it works. My assumption was that imagemagick looks at the borders of the image and cuts them based on the given background color , which is wrong. Why do you say it is wrong? It is correct for doing trimming. If you want automatic trimming based upon edge detection, see my scripts: smarttrim and smartcrop at my web site: fmwconcepts.com/imagemagick/index.phpfmw42

1 Answers

2
votes

Imagemagick -trim will trim from each sides only if the side color is within the fuzz value. Your white background does nothing. The -trim function looks at the colors of the edges and trims inward until the rows and columns of the edges exceed the fuzz value for the color on the edges. It looks at the corner colors to determine whether to trim or not. In your first case, you have 3 white corners. So it will trim the white on the top and right. In your second case, you now have 3 blue corners. So, it will trim inward on the top and right sides to remove some of the blue.

I think what you want to do is put a 1 pixel border around the image of the background color. In this case you want white.

Input1:

enter image description here

convert image1.jpg -bordercolor white -border 1 -fuzz 30% -trim +repage image_trim.jpg


Note that 30% is equivalent to 0.3*quantumrange

enter image description here

Image2:

enter image description here

convert image2.jpg -bordercolor white -border 1 -fuzz 30% -trim +repage image2a_trim.jpg


enter image description here

Without adding the white border, the -trim sees the blue as the border from 3 of the corners. So it will trim blue.

convert image2.jpg -fuzz 30% -trim +repage image2b_trim.jpg


enter image description here

If you increase the fuzz value to 50%, it will be more obvious and trim to the penguin.

convert image2.jpg -fuzz 30% -trim +repage image2c_trim.jpg


enter image description here