1
0
Fork 0
mirror of https://github.com/ldericher/fftcgtool synced 2025-01-15 23:03:00 +00:00
fftcgtool/fftcg/imageloader.py

34 lines
886 B
Python
Raw Normal View History

2021-08-04 01:39:19 +00:00
import io
import logging
import multiprocessing
2021-08-04 01:39:19 +00:00
import requests
2021-08-04 01:41:14 +00:00
from PIL import Image
2021-08-04 01:39:19 +00:00
2021-08-23 14:55:41 +00:00
from .utils import RESOLUTION
2021-08-17 15:37:28 +00:00
2021-08-04 01:39:19 +00:00
class ImageLoader:
@classmethod
def _load(cls, url: str) -> Image.Image:
2021-08-04 01:39:19 +00:00
logger = logging.getLogger(__name__)
2021-08-04 16:36:23 +00:00
# fetch image (retry on fail)
while True:
logger.info(f"downloading image {url}")
try:
res = requests.get(url)
image = Image.open(io.BytesIO(res.content))
2021-08-04 01:39:19 +00:00
# unify images
image.convert(mode="RGB")
return image.resize(RESOLUTION, Image.BICUBIC)
2021-08-04 18:15:38 +00:00
except requests.exceptions.RequestException:
pass
2021-08-04 01:39:19 +00:00
@classmethod
2021-08-20 22:16:38 +00:00
def load(cls, urls: list[str], num_threads: int) -> list[Image.Image]:
with multiprocessing.Pool(num_threads) as p:
return p.map(ImageLoader._load, urls)