Beispiel #1
0
def chenschmidt(alg, content, style, outfile, size, stylescale, algparams):
    """Runs Chen and Schmidt fast style-transfer algorithm

    References:
        * https://arxiv.org/pdf/1612.04337.pdf
        * https://github.com/rtqichen/style-swap
    """
    if alg not in ["chen-schmidt", "chen-schmidt-inverse"]:
        raise ValueError(
            "Unnaceptable subalgorithm %s for Chen-Schmidt family")

    # Rescale style as requested
    instyle = NamedTemporaryFile()
    copyfile(style, instyle.name)
    resize(instyle.name, int(stylescale * shape(style)[0]))
    # Run algorithm
    outdir = TemporaryDirectory()
    runalgorithm(alg, [
        "--save", outdir.name, "--content", content, "--style", instyle.name,
        "--maxContentSize", size if size is not None else shape(content)[0],
        "--maxStyleSize", size if size is not None else shape(content)[0],
        *algparams
    ])
    # Gather output results
    output = outdir.name + "/" + filename(content) + "_stylized" + fileext(
        content)
    convert(output, outfile)
    instyle.close()
def test_resize_changeproportions():
    """Resizing an image changing proportions works correctly"""
    tmpdir = TemporaryDirectory()
    fname = tmpdir.name + "/docker.png"
    copyfile(CONTENTS + "docker.png", fname)
    resize(fname, [700, 300])
    assert shape(fname) == [700, 300]
def test_resize_keepproportions():
    """Resizing an image without changing proportions works correctly"""
    tmpdir = TemporaryDirectory()
    fname = tmpdir.name + "/docker.png"
    copyfile(CONTENTS + "docker.png", fname)
    resize(fname, 1016)
    assert shape(fname) == [1016, 886]
Beispiel #4
0
def neuraltile(content,
               style,
               outfile,
               size=None,
               overlap=100,
               alg="gatys",
               weight=5.0,
               stylescale=1.0,
               algparams=None):
    """Strategy to generate a high resolution image by running style transfer on overlapping image tiles"""
    LOGGER.info("Starting tiling strategy")
    if algparams is None:
        algparams = []
    workdir = TemporaryDirectory()

    # Gather size info from original image
    fullshape = targetshape(content, size)

    # Compute number of tiles required to map all the image
    xtiles, ytiles = tilegeometry(fullshape, alg, overlap)

    # First scale image to target resolution
    firstpass = workdir.name + "/" + "lowres.png"
    convert(content, firstpass)
    resize(firstpass, fullshape)

    # Chop the styled image into tiles with the specified overlap value.
    lowrestiles = choptiles(firstpass,
                            xtiles=xtiles,
                            ytiles=ytiles,
                            overlap=overlap,
                            outname=workdir.name + "/" + "lowres_tiles")

    # High resolution pass over each tile
    highrestiles = []
    for i, tile in enumerate(lowrestiles):
        name = workdir.name + "/" + "highres_tiles_" + str(i) + ".png"
        styletransfer_single(tile,
                             style,
                             name,
                             size=None,
                             alg=alg,
                             weight=weight,
                             stylescale=stylescale,
                             algparams=algparams)
        highrestiles.append(name)

    # Feather tiles
    featheredtiles = []
    for i, tile in enumerate(highrestiles):
        name = workdir.name + "/" + "feathered_tiles_" + str(i) + ".png"
        feather(tile, name)
        featheredtiles.append(name)

    # Smush the feathered tiles together
    smushedfeathered = workdir.name + "/" + "feathered_smushed.png"
    smush(featheredtiles, xtiles, ytiles, overlap, overlap, smushedfeathered)

    # Smush also the non-feathered tiles
    smushedhighres = workdir.name + "/" + "highres_smushed.png"
    smush(highrestiles, xtiles, ytiles, overlap, overlap, smushedhighres)

    # Combine feathered and un-feathered output images to disguise feathering
    composite([smushedfeathered, smushedhighres], outfile)

    # Adjust back to desired size
    assertshape(outfile, fullshape)