Add assert.py
This commit is contained in:
parent
eec18009ef
commit
deb8c49354
1 changed files with 38 additions and 0 deletions
38
assert.py
Normal file
38
assert.py
Normal 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)
|
||||||
Loading…
Add table
Add a link
Reference in a new issue