How to crop or remove white background from an image

后端 未结 4 832
隐瞒了意图╮
隐瞒了意图╮ 2020-12-08 17:04

I am trying to compare images using OpenCV and Python.

Consider these images:




Both feature an identical pair of shoes, se

4条回答
  •  渐次进展
    2020-12-08 17:43

    I found this on github.

    https://imagemagick.org/script/download.php

    import pgmagick
    
    def remove_background(image, background=None):
        """Returns a copy of `image` that only contains the parts that is distinct
           from the background. If background is None, returns parts that are
           distinct from white."""
        if background is None:
            background = pgmagick.Image(image.size(), 'white')
        elif isinstance(background, pgmagick.Image):
            blob = pgmagick.Blob()
            background.write(blob)
            background = pgmagick.Image(blob, image.size())
        else:
            background = pgmagick.Image(image.size(), background)
        background.composite(image, 0, 0, pgmagick.CompositeOperator.DifferenceCompositeOp)
        background.threshold(25)
        blob = pgmagick.Blob()
        image.write(blob)
        image = pgmagick.Image(blob, image.size())
        image.composite(background, 0, 0, pgmagick.CompositeOperator.CopyOpacityCompositeOp)
        return image
    

提交回复
热议问题