-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_image_processing.py
130 lines (103 loc) · 4.07 KB
/
test_image_processing.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
import pytest
from pathlib import Path
from PIL import Image
import os
import shutil
import tempfile
import random
import numpy as np
from datetime import datetime
# Assume we have implemented these functions in our script
from image_processing import process_color_bands, process_inverted, process_no_gps, process_timestamp, process_noise
# Fixtures for setting up temporary directories and files
@pytest.fixture
def setup_temp_dir():
temp_dir = tempfile.mkdtemp()
yield temp_dir
shutil.rmtree(temp_dir)
@pytest.fixture
def setup_sample_images(setup_temp_dir):
image_paths = []
for i in range(5): # Create 5 sample images
img = Image.new('RGB', (100, 100), color=(i * 50, i * 50, i * 50))
path = Path(setup_temp_dir) / f"image_{i}.jpg"
img.save(path)
image_paths.append(path)
return image_paths
@pytest.mark.parametrize("bands_to_keep, expected_color", [
("r", (50, 0, 0)),
("g", (0, 50, 0)),
("b", (0, 0, 50)),
("rg", (50, 50, 0)),
("rgb", (50, 50, 50)),
])
def test_process_monochrome(setup_sample_images, setup_temp_dir, bands_to_keep, expected_color):
output_dir = Path(setup_temp_dir) / "output"
os.makedirs(output_dir, exist_ok=True)
process_color_bands(setup_sample_images, output_dir, bands_to_keep)
for i, image_path in enumerate(Path(output_dir).glob("*.jpg")):
img = Image.open(image_path)
pixels = np.array(img)
assert np.all(pixels == expected_color)
@pytest.mark.parametrize("flip_percentage, mirror_percentage", [
(0, 0),
(100, 0),
(0, 100),
(50, 50),
])
def test_process_inverted(setup_sample_images, setup_temp_dir, flip_percentage, mirror_percentage):
output_dir = Path(setup_temp_dir) / "output"
os.makedirs(output_dir, exist_ok=True)
process_inverted(setup_sample_images, output_dir, flip_percentage, mirror_percentage)
flipped = sum(1 for _ in Path(output_dir).glob("flipped*.jpg"))
mirrored = sum(1 for _ in Path(output_dir).glob("mirrored*.jpg"))
assert flipped == (flip_percentage / 100) * len(setup_sample_images)
assert mirrored == (mirror_percentage / 100) * len(setup_sample_images)
@pytest.mark.parametrize("gps_removal_percentage", [
(0),
(100),
(50),
])
def test_process_no_gps(setup_sample_images, setup_temp_dir, gps_removal_percentage):
output_dir = Path(setup_temp_dir) / "output"
os.makedirs(output_dir, exist_ok=True)
process_no_gps(setup_sample_images, output_dir, gps_removal_percentage)
for image_path in Path(output_dir).glob("*.jpg"):
exif_data = {} # Fetch EXIF data here
has_gps = 'GPSInfo' in exif_data
if random.random() < gps_removal_percentage / 100:
assert not has_gps
else:
assert has_gps
@pytest.mark.parametrize("start_date, end_date", [
("2024-01-01", "2024-01-31"),
("2024-06-01", "2024-06-30"),
])
def test_process_timestamp(setup_sample_images, setup_temp_dir, start_date, end_date):
output_dir = Path(setup_temp_dir) / "output"
os.makedirs(output_dir, exist_ok=True)
process_timestamp(setup_sample_images, output_dir, start_date, end_date)
start_dt = datetime.fromisoformat(start_date)
end_dt = datetime.fromisoformat(end_date)
for image_path in Path(output_dir).glob("*.jpg"):
exif_data = {} # Fetch EXIF data here
timestamp = exif_data.get('DateTime')
timestamp_dt = datetime.strptime(timestamp, "%Y:%m:%d %H:%M:%S")
assert start_dt <= timestamp_dt <= end_dt
@pytest.mark.parametrize("noise_level", [
(0),
(50),
(100),
])
def test_process_noise(setup_sample_images, setup_temp_dir, noise_level):
output_dir = Path(setup_temp_dir) / "output"
os.makedirs(output_dir, exist_ok=True)
process_noise(setup_sample_images, output_dir, noise_level)
for image_path in Path(output_dir).glob("*.jpg"):
img = Image.open(image_path)
pixels = np.array(img)
# Assuming the noise is added and we check for variability
if noise_level > 0:
assert np.std(pixels) > 0
else:
assert np.std(pixels) == 0