-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtest_helper.py
59 lines (43 loc) · 2.03 KB
/
test_helper.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
# a a couple of helper functions for the "front end" notebook for opening
# image files and displaying themm with a green or red tinting depending
# on whether or not the photos match. This code could easily be in the
# notebook, but I think it's cleaner to place it in a separate python
# script
from PIL import Image
import matplotlib.pyplot as plt
class TestFunctions:
# this method opens the images and returns the image data
def open_images(self, first: object, second: object) -> object:
with Image.open(first) as photo1:
photo1.load()
with Image.open(second) as photo2:
photo2.load()
return photo1, photo2
# method to display two photos side by side
def show_images(self, imagea: object, imageb: object,
title="Evaluated Photo"):
# create canvas that photos will be plotted on
figure = plt.figure(figsize=(8, 8))
figure.add_subplot(1, 2, 1)
plt.title("Reference Photo")
plt.imshow(imagea)
figure.add_subplot(1, 2, 2)
plt.title(title)
plt.imshow(imageb)
# this method puts a red or green tint on the evaluated photo depending
# on the match status expected input is two images that have been opened
# with the Python Pillow (PIL) library result is a 1 for matched photos
# 0 for photos that didn't match
def display_results(self, imagea: object, imageb: object, result: int):
# create required tint: red for no match, green for match
if result == 0:
tint = Image.new("RGB", (imageb.size), color=(255, 155, 15))
title = 'No Match!'
else:
tint = Image.new("RGB", (imageb.size), color=(55, 255, 11))
title = 'Photos Match!'
# with the tint determined, blend the photo and the tint together
comparedPhoto = Image.blend(imageb, tint, 0.5)
# push the reference image and the sample photo to the method that
# will display the images
self.show_images(imagea, comparedPhoto, title)