« Charts with more than… | Home | FileMaker Konferenz 2… »

Auto crop a signature picture

Recently we got a client with a special problem. A scanned paper with a signature has a lot of unused space around. The scan shows a bit of dust and dirt. People have greasy fingers, the flatbed scanner is not clean and the paper not perfect white. We have to clean it up and got a problem: The normal trim method in GMImageMBS class would remove some of the space, but a dust piece on the scan may prevent it from removing a bigger part.

We found a relative easy fix. We scale down the image to a much smaller size and all the dust points disappear. Now we can check for bounding box of the content and apply the found rectangle to the original picture.

Here is a simple code to show this:

// load a picture file into GraphicsMagick Dim file As FolderItem = SpecialFolder.Desktop.Child("signature.jpg") Dim g As New GMImageMBS(file) // now make a scaled down copy Dim c As New GMImageMBS(g) c.scale New GMGeometryMBS(200, 200) // we query bounding box for trim // allowing up to 50 color shades difference c.colorFuzz = 50 Dim b As GMGeometryMBS = c.boundingBox // now scale the bounding box back to the unscaled image Dim f As Double = g.width / c.width // we build new geometry and add an extra margin Const margin = 5 Dim x As Integer = b.width * f + margin * 2 Dim y As Integer = b.height * f + margin * 2 Dim w As Integer = b.xOff * f - margin Dim h As Integer = b.yOff * f - margin // now crop to the bounding box Dim r As New GMGeometryMBS( x, y, w, h ) g.crop(r) // and write final application g.write(SpecialFolder.Desktop.Child("output.jpg"))

Please try this. Trim and BoundingBox basically looks on the color in the edges and how far it reaches into the image. This way it can remove the whitespace. Don't forget to set colorfuzz to make sure it doesn't look for exact white, but allows some grayish pixels.

30 01 24 - 06:57