2023-02-02 04:20:40 +00:00
|
|
|
from json import dumps
|
|
|
|
from PIL import Image
|
|
|
|
from typing import Any, Optional
|
|
|
|
|
|
|
|
from .params import (
|
|
|
|
Border,
|
|
|
|
ImageParams,
|
|
|
|
Size,
|
|
|
|
UpscaleParams,
|
|
|
|
)
|
|
|
|
from .utils import (
|
|
|
|
base_join,
|
|
|
|
ServerContext,
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
def json_params(
|
|
|
|
output: str,
|
|
|
|
params: ImageParams,
|
|
|
|
size: Size,
|
|
|
|
upscale: Optional[UpscaleParams] = None,
|
|
|
|
border: Optional[Border] = None,
|
|
|
|
) -> Any:
|
2023-02-02 04:31:01 +00:00
|
|
|
json = {
|
|
|
|
'output': output,
|
|
|
|
'params': params.tojson(),
|
|
|
|
}
|
|
|
|
|
2023-02-02 04:25:53 +00:00
|
|
|
if upscale is not None and border is not None:
|
|
|
|
size = upscale.resize(size.add_border(border))
|
|
|
|
|
|
|
|
if upscale is not None:
|
2023-02-02 04:31:01 +00:00
|
|
|
json['upscale'] = upscale.tojson()
|
2023-02-02 04:25:53 +00:00
|
|
|
size = upscale.resize(size)
|
|
|
|
|
|
|
|
if border is not None:
|
2023-02-02 04:31:01 +00:00
|
|
|
json['border'] = border.tojson()
|
2023-02-02 04:25:53 +00:00
|
|
|
size = size.add_border(border)
|
|
|
|
|
2023-02-02 04:37:26 +00:00
|
|
|
json['size'] = size.tojson()
|
2023-02-02 04:31:01 +00:00
|
|
|
|
2023-02-02 04:35:10 +00:00
|
|
|
return json
|
2023-02-02 04:20:40 +00:00
|
|
|
|
|
|
|
|
|
|
|
def save_image(ctx: ServerContext, output: str, image: Image.Image) -> str:
|
|
|
|
path = base_join(ctx.output_path, output)
|
|
|
|
image.save(path)
|
|
|
|
return path
|
|
|
|
|
|
|
|
|
|
|
|
def save_params(
|
|
|
|
ctx: ServerContext,
|
|
|
|
output: str,
|
|
|
|
params: ImageParams,
|
|
|
|
size: Size,
|
|
|
|
upscale: Optional[UpscaleParams] = None,
|
|
|
|
border: Optional[Border] = None,
|
|
|
|
) -> str:
|
|
|
|
path = base_join(ctx.output_path, '%s.json' % (output))
|
|
|
|
json = json_params(output, params, size, upscale=upscale, border=border)
|
|
|
|
with open(path, 'w') as f:
|
|
|
|
f.write(dumps(json))
|