-
Notifications
You must be signed in to change notification settings - Fork 38
/
Copy pathtest.py
63 lines (44 loc) · 1.85 KB
/
test.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
from io import BytesIO
from os import path
from unittest import TestCase
import wand.image
from PIL import Image as PilImage
from PIL import ImageDraw as PilDraw
import dhash
IMGDIR = path.dirname(__file__)
def pil_to_wand(image, format="png"):
with BytesIO() as fd:
image.save(fd, format=format)
fd.seek(0)
return wand.image.Image(file=fd)
class TestDHash(TestCase):
def test_get_grays_pil(self):
with PilImage.open(path.join(IMGDIR, "dhash-test.jpg")) as image:
self._test_get_grays(image, delta=1)
def test_get_grays_wand(self):
image = wand.image.Image(filename=path.join(IMGDIR, "dhash-test.jpg"))
self._test_get_grays(image, delta=2)
def _test_get_grays(self, image, delta):
result = dhash.get_grays(image, 9, 9)[:18]
expected = [
93, 158, 210, 122, 93, 77, 74, 74, 77,
95, 117, 122, 111, 92, 74, 81, 80, 77,
] # fmt: skip
self.assertEqual(len(result), len(expected))
for r, e in zip(result, expected):
self.assertAlmostEqual(r, e, delta=delta)
def test_fill_transparency(self):
"Ensure transparent colors in PIL Images are ignored in hashes"
# grayscale image, completely white and also completely transparent
im1 = PilImage.new("LA", (100, 100), (0xFF, 0))
im2 = im1.copy()
# replace most of it with "transparent black"
PilDraw.Draw(im2).rectangle((10, 10, 90, 90), (0, 0))
# delta=n means difference <= n
self.assertAlmostEqual(dhash.dhash_row_col(im1), dhash.dhash_row_col(im2), delta=1)
# same with Wand version of images
pm1, pm2 = pil_to_wand(im1), pil_to_wand(im2)
self.assertAlmostEqual(dhash.dhash_row_col(pm1), dhash.dhash_row_col(pm2), delta=1)
if __name__ == "__main__":
import unittest
unittest.main()