2023-04-11 02:03:19 +00:00
|
|
|
from io import BytesIO
|
|
|
|
from logging import getLogger
|
2023-07-04 18:29:58 +00:00
|
|
|
from typing import List
|
2023-04-11 02:03:19 +00:00
|
|
|
|
|
|
|
import requests
|
|
|
|
from PIL import Image
|
|
|
|
|
|
|
|
from ..params import ImageParams, StageParams
|
|
|
|
from ..server import ServerContext
|
|
|
|
from ..worker import WorkerContext
|
2023-07-02 23:21:21 +00:00
|
|
|
from .stage import BaseStage
|
2023-04-11 02:03:19 +00:00
|
|
|
|
|
|
|
logger = getLogger(__name__)
|
|
|
|
|
|
|
|
|
2023-07-02 23:21:21 +00:00
|
|
|
class SourceURLStage(BaseStage):
|
2023-07-01 12:10:53 +00:00
|
|
|
def run(
|
|
|
|
self,
|
|
|
|
_job: WorkerContext,
|
|
|
|
_server: ServerContext,
|
|
|
|
_stage: StageParams,
|
|
|
|
_params: ImageParams,
|
2023-07-04 18:29:58 +00:00
|
|
|
sources: List[Image.Image],
|
2023-07-01 12:10:53 +00:00
|
|
|
*,
|
2023-07-04 18:29:58 +00:00
|
|
|
source_urls: List[str],
|
2023-07-01 12:10:53 +00:00
|
|
|
stage_source: Image.Image,
|
|
|
|
**kwargs,
|
2023-07-04 18:29:58 +00:00
|
|
|
) -> List[Image.Image]:
|
2023-07-01 12:10:53 +00:00
|
|
|
logger.info("loading image from URL source")
|
|
|
|
|
2023-07-04 18:29:58 +00:00
|
|
|
if len(sources) > 0:
|
2023-07-01 12:10:53 +00:00
|
|
|
logger.warn(
|
|
|
|
"a source image was passed to a source stage, and will be discarded"
|
|
|
|
)
|
|
|
|
|
2023-07-04 18:29:58 +00:00
|
|
|
outputs = []
|
|
|
|
for url in source_urls:
|
|
|
|
response = requests.get(url)
|
|
|
|
output = Image.open(BytesIO(response.content))
|
2023-07-01 12:10:53 +00:00
|
|
|
|
2023-07-04 18:29:58 +00:00
|
|
|
logger.info("final output image size: %sx%s", output.width, output.height)
|
|
|
|
outputs.append(output)
|
|
|
|
|
|
|
|
return outputs
|