2023-12-15 20:31:28 +00:00
|
|
|
"""Command for upscaling images with AI"""
|
|
|
|
|
2023-02-25 20:32:50 +00:00
|
|
|
import logging
|
|
|
|
|
|
|
|
import click
|
|
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
|
|
|
@click.argument("image_filepaths", nargs=-1)
|
|
|
|
@click.option(
|
|
|
|
"--outdir",
|
|
|
|
default="./outputs/upscaled",
|
|
|
|
show_default=True,
|
|
|
|
type=click.Path(),
|
|
|
|
help="Where to write results to.",
|
|
|
|
)
|
2023-02-25 22:08:34 +00:00
|
|
|
@click.option("--fix-faces", is_flag=True)
|
|
|
|
@click.option(
|
|
|
|
"--fix-faces-fidelity",
|
|
|
|
default=1,
|
|
|
|
type=float,
|
|
|
|
help="How faithful to the original should face enhancement be. 1 = best fidelity, 0 = best looking face.",
|
|
|
|
)
|
2023-02-25 20:32:50 +00:00
|
|
|
@click.command("upscale")
|
2023-02-25 22:08:34 +00:00
|
|
|
def upscale_cmd(image_filepaths, outdir, fix_faces, fix_faces_fidelity):
|
2023-02-25 20:32:50 +00:00
|
|
|
"""
|
|
|
|
Upscale an image 4x using AI.
|
|
|
|
"""
|
|
|
|
import os.path
|
|
|
|
|
|
|
|
from tqdm import tqdm
|
|
|
|
|
2023-02-25 22:08:34 +00:00
|
|
|
from imaginairy.enhancers.face_restoration_codeformer import enhance_faces
|
2023-02-25 20:32:50 +00:00
|
|
|
from imaginairy.enhancers.upscale_realesrgan import upscale_image
|
2023-12-10 00:33:39 +00:00
|
|
|
from imaginairy.schema import LazyLoadingImage
|
2023-03-11 02:55:21 +00:00
|
|
|
from imaginairy.utils import glob_expand_paths
|
2023-02-25 20:32:50 +00:00
|
|
|
|
|
|
|
os.makedirs(outdir, exist_ok=True)
|
2023-03-11 02:55:21 +00:00
|
|
|
image_filepaths = glob_expand_paths(image_filepaths)
|
2023-02-25 20:32:50 +00:00
|
|
|
for p in tqdm(image_filepaths):
|
|
|
|
savepath = os.path.join(outdir, os.path.basename(p))
|
|
|
|
if p.startswith("http"):
|
|
|
|
img = LazyLoadingImage(url=p)
|
|
|
|
else:
|
|
|
|
img = LazyLoadingImage(filepath=p)
|
|
|
|
logger.info(
|
|
|
|
f"Upscaling {p} from {img.width}x{img.height} to {img.width * 4}x{img.height * 4} and saving it to {savepath}"
|
|
|
|
)
|
|
|
|
|
|
|
|
img = upscale_image(img)
|
2023-02-25 22:08:34 +00:00
|
|
|
if fix_faces:
|
|
|
|
img = enhance_faces(img, fidelity=fix_faces_fidelity)
|
2023-02-25 20:32:50 +00:00
|
|
|
|
|
|
|
img.save(os.path.join(outdir, os.path.basename(p)))
|