2023-09-11 01:59:13 +00:00
|
|
|
from logging import getLogger
|
2023-12-03 18:53:50 +00:00
|
|
|
from typing import List, Optional
|
2023-09-11 01:59:13 +00:00
|
|
|
|
|
|
|
from PIL import Image
|
|
|
|
|
2023-11-26 00:52:47 +00:00
|
|
|
from ..params import ImageParams, Size, SizeChart, StageParams
|
2023-09-11 01:59:13 +00:00
|
|
|
from ..server import ServerContext
|
|
|
|
from ..worker import ProgressCallback, WorkerContext
|
2023-11-18 23:18:23 +00:00
|
|
|
from .base import BaseStage
|
2024-01-06 22:59:02 +00:00
|
|
|
from .result import ImageMetadata, StageResult
|
2023-09-11 01:59:13 +00:00
|
|
|
|
|
|
|
logger = getLogger(__name__)
|
|
|
|
|
|
|
|
|
|
|
|
class BlendGridStage(BaseStage):
|
2023-09-24 15:11:52 +00:00
|
|
|
max_tile = SizeChart.max
|
|
|
|
|
2023-09-11 01:59:13 +00:00
|
|
|
def run(
|
|
|
|
self,
|
|
|
|
_worker: WorkerContext,
|
|
|
|
_server: ServerContext,
|
|
|
|
_stage: StageParams,
|
2024-01-06 22:59:02 +00:00
|
|
|
params: ImageParams,
|
2023-11-19 00:08:38 +00:00
|
|
|
sources: StageResult,
|
2023-09-11 01:59:13 +00:00
|
|
|
*,
|
|
|
|
height: int,
|
|
|
|
width: int,
|
2023-09-16 00:16:47 +00:00
|
|
|
# rows: Optional[List[str]] = None,
|
|
|
|
# columns: Optional[List[str]] = None,
|
|
|
|
# title: Optional[str] = None,
|
2023-12-03 18:53:50 +00:00
|
|
|
order: Optional[List[int]] = None,
|
2023-09-11 01:59:13 +00:00
|
|
|
stage_source: Optional[Image.Image] = None,
|
2023-09-16 00:16:47 +00:00
|
|
|
callback: Optional[ProgressCallback] = None,
|
2023-09-11 01:59:13 +00:00
|
|
|
**kwargs,
|
2023-11-19 00:08:38 +00:00
|
|
|
) -> StageResult:
|
2023-09-11 01:59:13 +00:00
|
|
|
logger.info("combining source images using grid layout")
|
|
|
|
|
2024-01-06 02:11:58 +00:00
|
|
|
images = sources.as_images()
|
2023-11-26 00:52:47 +00:00
|
|
|
ref_image = images[0]
|
|
|
|
size = Size(*ref_image.size)
|
2023-09-11 01:59:13 +00:00
|
|
|
|
2023-11-26 00:52:47 +00:00
|
|
|
output = Image.new(ref_image.mode, (size.width * width, size.height * height))
|
2023-09-11 01:59:13 +00:00
|
|
|
|
|
|
|
# TODO: labels
|
2023-09-11 04:15:01 +00:00
|
|
|
if order is None:
|
2023-11-19 03:35:00 +00:00
|
|
|
order = range(len(images))
|
2023-09-11 04:15:01 +00:00
|
|
|
|
|
|
|
for i in range(len(order)):
|
2023-09-11 01:59:13 +00:00
|
|
|
x = i % width
|
2023-09-11 04:06:00 +00:00
|
|
|
y = i // width
|
2023-09-11 01:59:13 +00:00
|
|
|
|
2023-09-11 04:06:00 +00:00
|
|
|
n = order[i]
|
2023-11-26 00:52:47 +00:00
|
|
|
output.paste(images[n], (x * size.width, y * size.height))
|
2023-09-11 01:59:13 +00:00
|
|
|
|
2024-01-06 02:11:58 +00:00
|
|
|
result = StageResult(source=sources)
|
2024-01-06 22:59:02 +00:00
|
|
|
result.push_image(
|
|
|
|
output,
|
|
|
|
ImageMetadata(params, Size(width, height), ancestors=[sources.metadata]),
|
|
|
|
)
|
2024-01-06 02:11:58 +00:00
|
|
|
return result
|
2023-09-12 23:16:16 +00:00
|
|
|
|
|
|
|
def outputs(
|
2023-09-13 00:17:03 +00:00
|
|
|
self,
|
2023-09-16 00:16:47 +00:00
|
|
|
_params: ImageParams,
|
2023-09-13 00:17:03 +00:00
|
|
|
sources: int,
|
2023-09-12 23:16:16 +00:00
|
|
|
) -> int:
|
2023-09-13 00:17:03 +00:00
|
|
|
return sources + 1
|