-
Notifications
You must be signed in to change notification settings - Fork 38
Expand file tree
/
Copy pathreal_world_example_async_v1.py
More file actions
145 lines (107 loc) · 4.86 KB
/
real_world_example_async_v1.py
File metadata and controls
145 lines (107 loc) · 4.86 KB
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
import asyncio
import time
from pathlib import Path
import requests
from PIL import Image
IMAGE_URLS = [
"https://images.unsplash.com/photo-1516117172878-fd2c41f4a759?w=1920&h=1080&fit=crop",
"https://images.unsplash.com/photo-1532009324734-20a7a5813719?w=1920&h=1080&fit=crop",
"https://images.unsplash.com/photo-1524429656589-6633a470097c?w=1920&h=1080&fit=crop",
"https://images.unsplash.com/photo-1530224264768-7ff8c1789d79?w=1920&h=1080&fit=crop",
"https://images.unsplash.com/photo-1564135624576-c5c88640f235?w=1920&h=1080&fit=crop",
"https://images.unsplash.com/photo-1541698444083-023c97d3f4b6?w=1920&h=1080&fit=crop",
"https://images.unsplash.com/photo-1522364723953-452d3431c267?w=1920&h=1080&fit=crop",
"https://images.unsplash.com/photo-1493976040374-85c8e12f0c0e?w=1920&h=1080&fit=crop",
"https://images.unsplash.com/photo-1530122037265-a5f1f91d3b99?w=1920&h=1080&fit=crop",
"https://images.unsplash.com/photo-1516972810927-80185027ca84?w=1920&h=1080&fit=crop",
"https://images.unsplash.com/photo-1550439062-609e1531270e?w=1920&h=1080&fit=crop",
"https://images.unsplash.com/photo-1549692520-acc6669e2f0c?w=1920&h=1080&fit=crop",
]
ORIGINAL_DIR = Path("original_images")
PROCESSED_DIR = Path("processed_images")
def download_single_image(url: str, img_num: int) -> Path:
print(f"Downloading {url}...")
ts = int(time.time())
url = f"{url}?ts={ts}" # Add timestamp to avoid caching issues
response = requests.get(url, timeout=10, allow_redirects=True)
response.raise_for_status()
filename = f"image_{img_num}.jpg"
download_path = ORIGINAL_DIR / filename
with download_path.open("wb") as f:
for chunk in response.iter_content(chunk_size=8192):
f.write(chunk)
print(f"Downloaded and saved to: {download_path}")
return download_path
async def download_images(urls: list) -> list[Path]:
async with asyncio.TaskGroup() as tg:
tasks = [
tg.create_task(asyncio.to_thread(download_single_image, url, img_num))
for img_num, url in enumerate(urls, start=1)
]
img_paths = [task.result() for task in tasks]
return img_paths
def process_single_image(orig_path: Path) -> Path:
save_path = PROCESSED_DIR / orig_path.name
with Image.open(orig_path) as img:
data = list(img.getdata())
width, height = img.size
new_data = []
for i in range(len(data)):
current_r, current_g, current_b = data[i]
total_diff = 0
neighbor_count = 0
for dx, dy in [(1, 0), (0, 1)]:
x = (i % width) + dx
y = (i // width) + dy
if 0 <= x < width and 0 <= y < height:
neighbor_r, neighbor_g, neighbor_b = data[y * width + x]
diff = (
abs(current_r - neighbor_r)
+ abs(current_g - neighbor_g)
+ abs(current_b - neighbor_b)
)
total_diff += diff
neighbor_count += 1
if neighbor_count > 0:
edge_strength = total_diff // neighbor_count
if edge_strength > 30:
new_data.append((255, 255, 255))
else:
new_data.append((0, 0, 0))
else:
new_data.append((0, 0, 0))
edge_img = Image.new("RGB", (width, height))
edge_img.putdata(new_data)
edge_img.save(save_path)
print(f"Processed {orig_path} and saved to {save_path}")
return save_path
async def process_images(orig_paths: list[Path]) -> list[Path]:
async with asyncio.TaskGroup() as tg:
tasks = [
tg.create_task(asyncio.to_thread(process_single_image, orig_path))
for orig_path in orig_paths
]
img_paths = [task.result() for task in tasks]
return img_paths
async def main():
ORIGINAL_DIR.mkdir(parents=True, exist_ok=True)
PROCESSED_DIR.mkdir(parents=True, exist_ok=True)
start_time = time.perf_counter()
img_paths = await download_images(IMAGE_URLS)
proc_start_time = time.perf_counter()
processed_paths = await process_images(img_paths)
finished_time = time.perf_counter()
dl_total_time = proc_start_time - start_time
proc_total_time = finished_time - proc_start_time
total_time = finished_time - start_time
print(
f"\nDownloaded {len(img_paths)} images in: {dl_total_time:.2f} seconds. {(dl_total_time / total_time) * 100:.2f}% of total time",
)
print(
f"Processed {len(processed_paths)} images in: {proc_total_time:.2f} seconds. {(proc_total_time / total_time) * 100:.2f}% of total time",
)
print(
f"\nTotal execution time: {total_time:.2f} seconds. {(total_time / total_time) * 100:.2f}% of total time",
)
if __name__ == "__main__":
asyncio.run(main())