Add assert.py

This commit is contained in:
adienakhmad 2025-11-19 07:25:28 +00:00
parent eec18009ef
commit deb8c49354

38
assert.py Normal file
View file

@ -0,0 +1,38 @@
# /// script
# requires-python = ">=3.14"
# dependencies = ["imagehash<5"]
# ///
import json
import imagehash
from PIL import Image
Image.MAX_IMAGE_PIXELS = None
def calculate_hash(filename: str) -> imagehash.ImageHash:
im = Image.open(filename)
return imagehash.phash(im)
def test_hashes(subtests):
with open("answer.json", "r") as file:
answers = json.load(file)
for test in answers.get("tests", []):
name = test.get("name")
hash = imagehash.hex_to_hash(test.get("p_hash"))
with subtests.test(msg=name):
actual = calculate_hash(name)
assert actual - hash < 3
def test_sizes(subtests):
with open("answer.json", "r") as file:
answers = json.load(file)
for test in answers.get("tests", []):
name = test.get("name")
size = test.get("img_size")
with subtests.test(msg=name):
im = Image.open(name)
assert im.size == tuple(size)