Comparing two images pixel-wise with PIL (Python Imaging Library)

前端 未结 1 1253
暖寄归人
暖寄归人 2021-02-04 12:00

I need a function which compares two PIL images of the same size. Let\'s call them A and B. The result is supposed to be a new image of the same size. If a pixels is the same in

1条回答
  •  忘了有多久
    2021-02-04 12:53

    Not sure about other libraries, but you can do this with PIL, with something like...

    from PIL import Image, ImageChops
    
    point_table = ([0] + ([255] * 255))
    
    def black_or_b(a, b):
        diff = ImageChops.difference(a, b)
        diff = diff.convert('L')
        diff = diff.point(point_table)
        new = diff.convert('RGB')
        new.paste(b, mask=diff)
        return new
    
    a = Image.open('a.png')
    b = Image.open('b.png')
    c = black_or_b(a, b)
    c.save('c.png')
    

    0 讨论(0)
提交回复
热议问题