Skip to content

Commit 141af54

Browse files
authored
📢 Command-based structure, support for remix and remake.
1 parent 1b72198 commit 141af54

File tree

9 files changed

+368
-266
lines changed

9 files changed

+368
-266
lines changed

src/texturize/__main__.py

Lines changed: 37 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,15 @@
66
|_| |_|\___|\__,_|_| \__,_|_| \__\___/_/\_\\__|\__,_|_| |_/___\___|
77
88
Usage:
9-
texturize SOURCE... [--size=WxH] [--output=FILE] [--variations=V] [--seed=SEED]
10-
[--mode=MODE] [--octaves=O] [--threshold=H] [--iterations=I]
11-
[--device=DEVICE] [--precision=PRECISION] [--quiet] [--verbose]
9+
texturize remix SOURCE... [options]
10+
texturize remake TARGET like SOURCE [options]
1211
texturize --help
1312
1413
Examples:
15-
texturize samples/grass.webp --size=1440x960 --output=result.png
16-
texturize samples/gravel.png --iterations=200 --precision=1e-5
17-
texturize samples/sand.tiff --output=tmp/{source}-{octave}.webp
18-
texturize samples/brick.jpg --device=cpu
14+
texturize remix samples/grass.webp --size=1440x960 --output=result.png
15+
texturize remix samples/gravel.png --iterations=200 --precision=1e-5
16+
texturize remix samples/sand.tiff --output=tmp/{source}-{octave}.webp
17+
texturize remix samples/brick.jpg --device=cpu
1918
2019
Options:
2120
SOURCE Path to source image to use as texture.
@@ -43,17 +42,18 @@
4342
# warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
4443
#
4544

45+
import os
4646
import glob
4747
import itertools
4848

4949
import docopt
50-
import progressbar
5150
from schema import Schema, Use, And, Or
5251

5352
import torch
5453

5554
from . import __version__
56-
from .api import process_single_file, ansi, ConsoleLog
55+
from . import api, io, commands
56+
from .logger import ansi, ConsoleLog
5757

5858

5959
def validate(config):
@@ -64,6 +64,7 @@ def split_size(size: str):
6464
sch = Schema(
6565
{
6666
"SOURCE": [str],
67+
"TARGET": Or(None, str),
6768
"size": And(Use(split_size), tuple),
6869
"output": str,
6970
"variations": Use(int),
@@ -86,30 +87,51 @@ def split_size(size: str):
8687
def main():
8788
# Parse the command-line options based on the script's documentation.
8889
config = docopt.docopt(__doc__[356:], version=__version__, help=False)
90+
command = [cmd for cmd in ("remix", "remake") if config[cmd]][0]
91+
8992
# Ensure the user-specified values are correct.
9093
config = validate(config)
91-
filenames, seed, quiet, verbose, help = [
92-
config.pop(k) for k in ("SOURCE", "seed", "quiet", "verbose", "help")
94+
sources, target, output, seed, mode = [
95+
config.pop(k) for k in ("SOURCE", "TARGET", "output", "seed", "mode")
9396
]
9497

9598
# Setup the output logging and display the logo!
96-
log = ConsoleLog(quiet, verbose)
99+
log = ConsoleLog(config.pop("quiet"), config.pop("verbose"))
97100
log.notice(ansi.PINK + " " + __doc__[:356] + ansi.ENDC)
98-
if help is True:
101+
if config.pop("help") is True:
99102
log.notice(__doc__[356:])
100103
return
101104

102105
# Scan all the files based on the patterns specified.
103-
files = itertools.chain.from_iterable(glob.glob(s) for s in filenames)
106+
files = itertools.chain.from_iterable(glob.glob(s) for s in sources)
104107
for filename in files:
105108
# If there's a random seed, use the same for all images.
106109
if seed is not None:
107110
torch.manual_seed(seed)
108111
torch.cuda.manual_seed(seed)
109112

113+
source_img = io.load_image_from_file(filename)
114+
target_img = io.load_image_from_file(target) if target else None
115+
116+
if command == "remix":
117+
cmd = commands.Remix(source_img, mode=mode)
118+
if command == "remake":
119+
cmd = commands.Remake(source_img, target_img, mode=mode)
120+
config["octaves"] = 1
121+
config["size"] = target_img.size
122+
110123
# Process the files one by one, each may have multiple variations.
111124
try:
112-
result = process_single_file(filename, log, **config)
125+
config["output"] = output
126+
config["output"] = config["output"].replace(
127+
"{source}", os.path.splitext(os.path.basename(filename))[0]
128+
)
129+
if target:
130+
config["output"] = config["output"].replace(
131+
"{target}", os.path.splitext(os.path.basename(target))[0]
132+
)
133+
134+
result = api.process_single_command(cmd, log, **config)
113135
log.notice(ansi.PINK + "\n=> result:", result, ansi.ENDC)
114136
except KeyboardInterrupt:
115137
print(ansi.PINK + "\nCTRL+C detected, interrupting..." + ansi.ENDC)

0 commit comments

Comments
 (0)