Search code examples
pythonimage-processingpython-imaging-libraryscikit-image

How can I crop black corners from a scanned image?


Scans/photocopies often miss the corner which comes out black. How can these black corners be made white using python with numpy, pillow or skimage?


Solution

    1. Create a mask with all black objects (mask = image < threshold)
    2. Remove objects touching the border (new_mask = segmentation.clear_border(mask))
    3. Which objects were removed? objs = (new_mask != mask)
    4. Fill those objects with white: image[objs] = 1 (or 255, if dtype int).

    If you need to make sure that the objects replaced by white are triangles, you can use skimage.measure.regionprops to further examine each one.