This project has moved and is read-only. For the latest updates, please go here.

Remove ragged black edge from scanned images

Mar 9, 2016 at 5:19 PM
I have found many approaches/examples with Imagemagick command line to eliminate black edges from scanned images. None seem to drop readily into Magick.Net (at least to a newbie)
These are B/W tiff images of a fixed size and I know that there is no usable data .5 inch all around the image. Is the best way to overlay a white border ? If so how ? Or is there a better way?
Mar 10, 2016 at 6:25 AM
Can you show how you would do it on the command line? I can help you translate that to Magick.NET. Most command line options are methods of the MagickImage class so you could also give it a try yourself first.
Mar 10, 2016 at 9:22 PM

I found a 'script' on Freds ImageMagick Script site that I believe should do what I need.
The arguments are -f 5 -m crop

Perhaps you can give me an idea on how to do that.

Michael Wechter
[email removed]
Mar 12, 2016 at 7:52 AM
As I told you in the e-mail I will add that script as the next script to the FredsImageMagickScripts.NET project ( These script do require a license when you use them commercially. You will need to contact Fred Weinhaus after the script has been created.
Mar 15, 2016 at 7:53 PM

Fred offered the follow UNIX example as a solution to cleaning up my borders.
(Note: My files are B/W Png which will be later converted to tiff)

I have tried using the ImageMagick convert.exe, but it complains about 'head' in the command line.

In unix syntax:

size=`convert test.tiff -format "%wx%h\n" info: | head -n 1`
convert test.tiff -quiet -shave 75x75 -background white -extent $size newtest.tiff

How would we do this using Magick ?

Michael Wechter

Mar 15, 2016 at 8:47 PM
The first line only gets the width and height of the image and stores this in a variable to use it at a later moment. So there is not need to do this. What this command does it shave 75 pixels from the image and then resize it back to the original size. You can translate the command to this:
using (MagickImage image = new MagickImage("test.tiff"))
  image.Shave(75, 75); // -shave 75x75
  image.BackgroundColor = MagickColors.White; // -background white
  image.Extent(image.BaseWidth, image.BaseHeight, Gravity.Center); // -extent $size
The BaseWidth and BaseHeight properties store the Width/Height of the image when it was read. If you do other operations you might not be able to use that value and you will need to store the width and height of the image in a variable right after you read it.