This repository has been archived by the owner on Jul 26, 2023. It is now read-only.
forked from rdeepak2002/reddit-place-script-2022
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathtest_image_correction.py
65 lines (51 loc) · 1.96 KB
/
test_image_correction.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
64
65
import sys
import json
import requests
from PIL import Image
from io import BytesIO
import numpy as np
import time
from src.mappings import ColorMapper
from test.mappings import closest_color
if len(sys.argv) != 2:
print("Usage: python correct_image.py <config.json>")
exit(1)
config = json.load(open(sys.argv[1]))
urls = config['template_urls']
templates = []
for url in urls:
sources = requests.get(url).json()
templates += sources['templates']
total_time = 0
total_time_old = 0
for template in templates:
name = template['name']
url = template['sources'][0]
response = requests.get(url)
image = Image.open(BytesIO(response.content))
image = image.convert('RGBA')
image.save(f"images/{name}.png")
image = np.array(image)
current_time = time.time()
corrected_image_numpy = ColorMapper.correct_image(image, ColorMapper.FULL_COLOR_MAP)
total_time += time.time() - current_time
current_time = time.time()
corrected_image_base = np.empty_like(image)
for i in range(image.shape[0]):
for j in range(image.shape[1]):
corrected_image_base[i][j][:3] = closest_color(image[i][j],
ColorMapper.palette_to_rgb(ColorMapper.FULL_COLOR_MAP))
corrected_image_base[i][j][3] = image[i][j][3]
total_time_old += time.time() - current_time
equal = np.array_equal(corrected_image_numpy, corrected_image_base)
print("Corrected {} image. Numpy {} Old".format(
name, '==' if equal else '!='
))
if not equal:
diff = np.argwhere((corrected_image_numpy - corrected_image_base).any(axis=-1))
print(diff)
Image.fromarray(corrected_image_numpy, 'RGBA').save(f"images/{name}_numpy.png")
Image.fromarray(corrected_image_base, 'RGBA').save(f"images/{name}_base.png")
print(f"Average image correction time")
print(f"numpy: {total_time / len(templates)}")
print(f"base: {total_time_old / len(templates)}")