-
Notifications
You must be signed in to change notification settings - Fork 7
/
image_loader.py
72 lines (64 loc) · 2.47 KB
/
image_loader.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
import aiohttp
import asyncio
from PySide6.QtCore import QThread, Signal
class ImageLoader(QThread):
progress_updated = Signal(int, int, dict)
def __init__(
self,
image_urls,
image_manager,
iconified = False,
):
super().__init__()
self.image_urls = image_urls
self.image_manager = image_manager
self.iconified = iconified
async def fetch_image(self, session, image_rank, image_url):
try:
# Use ImageManager to get QIcon or QPixmap
image = await self.image_manager.get_image_from_url(session, image_url, self.iconified)
if image:
if self.iconified:
return {"rank":image_rank, "icon":image}
else:
return {"pixmap":image}
except Exception as e:
print(f"Error fetching image {image_url}: {e}")
raise
return None
async def decode_base64_image(self, image_rank, image_str):
try:
# Use ImageManager to get QIcon or QPixmap
image = await self.image_manager.get_image_from_base64(image_str, self.iconified)
if image:
if self.iconified:
return {"rank":image_rank, "icon":image}
else:
return {"pixmap":image}
except Exception as e:
print(f"Error decoding base64 image : {e}")
raise
return None
async def load_images(self):
async with aiohttp.ClientSession() as session:
tasks = []
for image_rank, url in enumerate(self.image_urls):
if url:
if url.startswith(("http://", "https://")):
tasks.append(self.fetch_image(session, image_rank, url))
elif url.startswith("data:image"):
tasks.append(self.decode_base64_image(image_rank, url))
image_count = len(tasks)
for i, task in enumerate(asyncio.as_completed(tasks), 1):
try:
image_item = await task
except Exception as e:
image_item = None
print(f"Error processing image task: {e}")
finally:
self.progress_updated.emit(i, image_count, image_item)
def run(self):
try:
asyncio.run(self.load_images())
except Exception as e:
print(f"Error in image loading: {e}")