B
B
Bezola2021-05-13 21:50:26
Python
Bezola, 2021-05-13 21:50:26

How to find out by how many pixels photos differ from each other in python?

I want to compare images. If they do not match, output the number of different pixels.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
ScriptKiddo, 2021-05-13
@Bezola

Images

609d7c7f64898136360679.png
609d7c86b39f5189175412.png

import base64
from io import BytesIO

from PIL import Image, ImageChops


a_base_64 = 'iVBORw0KGgoAAAANSUhEUgAAAAIAAAACCAIAAAD91JpzAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsMAAA7DA' \
            'cdvqGQAAAAWSURBVBhXY3BwcGBgYGD8//8/AwMDABgGA7/DZDVsAAAAAElFTkSuQmCC'

b_base_64 = 'iVBORw0KGgoAAAANSUhEUgAAAAIAAAACCAIAAAD91JpzAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsMAAA7DA' \
            'cdvqGQAAAAVSURBVBhXY/z//z8DAwMTEDMwMAAAJAYDAbrboo8AAAAASUVORK5CYII='


a = Image.open(BytesIO(base64.b64decode(a_base_64)))
b = Image.open(BytesIO(base64.b64decode(b_base_64)))

diff = ImageChops.difference(a, b).convert('L')
print(sum(diff.point(bool).getdata()))

OUT
2

Process finished with exit code 0

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question