2022-08-26 07:15:42 +00:00
|
|
|
"""
|
2022-08-26 02:49:15 +00:00
|
|
|
Two helper classes for dealing with PNG images and their path names.
|
|
|
|
PngWriter -- Converts Images generated by T2I into PNGs, finds
|
|
|
|
appropriate names for them, and writes prompt metadata
|
|
|
|
into the PNG. Intended to be subclassable in order to
|
|
|
|
create more complex naming schemes, including using the
|
|
|
|
prompt for file/directory names.
|
|
|
|
PromptFormatter -- Utility for converting a Namespace of prompt parameters
|
|
|
|
back into a formatted prompt string with command-line switches.
|
2022-08-26 07:15:42 +00:00
|
|
|
"""
|
2022-08-24 21:52:34 +00:00
|
|
|
import os
|
2022-08-24 23:47:59 +00:00
|
|
|
import re
|
2022-08-26 07:15:42 +00:00
|
|
|
from math import sqrt, floor, ceil
|
|
|
|
from PIL import Image, PngImagePlugin
|
2022-08-24 21:52:34 +00:00
|
|
|
|
2022-08-25 04:42:37 +00:00
|
|
|
# -------------------image generation utils-----
|
|
|
|
class PngWriter:
|
2022-08-26 07:15:42 +00:00
|
|
|
def __init__(self, outdir, prompt=None, batch_size=1):
|
|
|
|
self.outdir = outdir
|
|
|
|
self.batch_size = batch_size
|
|
|
|
self.prompt = prompt
|
|
|
|
self.filepath = None
|
|
|
|
self.files_written = []
|
2022-08-25 04:42:37 +00:00
|
|
|
os.makedirs(outdir, exist_ok=True)
|
|
|
|
|
2022-08-26 07:15:42 +00:00
|
|
|
def write_image(self, image, seed):
|
|
|
|
self.filepath = self.unique_filename(
|
|
|
|
seed, self.filepath
|
|
|
|
) # will increment name in some sensible way
|
2022-08-25 04:42:37 +00:00
|
|
|
try:
|
|
|
|
prompt = f'{self.prompt} -S{seed}'
|
2022-08-26 07:15:42 +00:00
|
|
|
self.save_image_and_prompt_to_png(image, prompt, self.filepath)
|
2022-08-25 04:42:37 +00:00
|
|
|
except IOError as e:
|
|
|
|
print(e)
|
2022-08-26 07:15:42 +00:00
|
|
|
self.files_written.append([self.filepath, seed])
|
2022-08-25 04:42:37 +00:00
|
|
|
|
2022-08-26 07:15:42 +00:00
|
|
|
def unique_filename(self, seed, previouspath=None):
|
2022-08-25 04:42:37 +00:00
|
|
|
revision = 1
|
|
|
|
|
|
|
|
if previouspath is None:
|
|
|
|
# sort reverse alphabetically until we find max+1
|
2022-08-26 07:15:42 +00:00
|
|
|
dirlist = sorted(os.listdir(self.outdir), reverse=True)
|
2022-08-25 04:42:37 +00:00
|
|
|
# find the first filename that matches our pattern or return 000000.0.png
|
2022-08-26 07:15:42 +00:00
|
|
|
filename = next(
|
|
|
|
(f for f in dirlist if re.match('^(\d+)\..*\.png', f)),
|
|
|
|
'0000000.0.png',
|
|
|
|
)
|
|
|
|
basecount = int(filename.split('.', 1)[0])
|
2022-08-25 04:42:37 +00:00
|
|
|
basecount += 1
|
|
|
|
if self.batch_size > 1:
|
|
|
|
filename = f'{basecount:06}.{seed}.01.png'
|
|
|
|
else:
|
|
|
|
filename = f'{basecount:06}.{seed}.png'
|
2022-08-26 07:15:42 +00:00
|
|
|
return os.path.join(self.outdir, filename)
|
2022-08-25 04:42:37 +00:00
|
|
|
|
|
|
|
else:
|
|
|
|
basename = os.path.basename(previouspath)
|
2022-08-26 07:15:42 +00:00
|
|
|
x = re.match('^(\d+)\..*\.png', basename)
|
2022-08-25 04:42:37 +00:00
|
|
|
if not x:
|
2022-08-26 07:15:42 +00:00
|
|
|
return self.unique_filename(seed, previouspath)
|
2022-08-25 04:42:37 +00:00
|
|
|
|
|
|
|
basecount = int(x.groups()[0])
|
2022-08-26 07:15:42 +00:00
|
|
|
series = 0
|
2022-08-25 04:42:37 +00:00
|
|
|
finished = False
|
|
|
|
while not finished:
|
|
|
|
series += 1
|
|
|
|
filename = f'{basecount:06}.{seed}.png'
|
2022-08-26 07:15:42 +00:00
|
|
|
if self.batch_size > 1 or os.path.exists(
|
|
|
|
os.path.join(self.outdir, filename)
|
|
|
|
):
|
2022-08-25 04:42:37 +00:00
|
|
|
filename = f'{basecount:06}.{seed}.{series:02}.png'
|
2022-08-26 07:15:42 +00:00
|
|
|
finished = not os.path.exists(
|
|
|
|
os.path.join(self.outdir, filename)
|
|
|
|
)
|
|
|
|
return os.path.join(self.outdir, filename)
|
2022-08-25 04:42:37 +00:00
|
|
|
|
2022-08-26 07:15:42 +00:00
|
|
|
def save_image_and_prompt_to_png(self, image, prompt, path):
|
2022-08-25 04:42:37 +00:00
|
|
|
info = PngImagePlugin.PngInfo()
|
2022-08-26 07:15:42 +00:00
|
|
|
info.add_text('Dream', prompt)
|
|
|
|
image.save(path, 'PNG', pnginfo=info)
|
2022-08-25 21:26:48 +00:00
|
|
|
|
2022-08-26 07:15:42 +00:00
|
|
|
def make_grid(self, image_list, rows=None, cols=None):
|
2022-08-25 21:26:48 +00:00
|
|
|
image_cnt = len(image_list)
|
2022-08-26 07:15:42 +00:00
|
|
|
if None in (rows, cols):
|
2022-08-25 21:26:48 +00:00
|
|
|
rows = floor(sqrt(image_cnt)) # try to make it square
|
2022-08-26 07:15:42 +00:00
|
|
|
cols = ceil(image_cnt / rows)
|
|
|
|
width = image_list[0].width
|
2022-08-25 21:26:48 +00:00
|
|
|
height = image_list[0].height
|
|
|
|
|
2022-08-26 07:15:42 +00:00
|
|
|
grid_img = Image.new('RGB', (width * cols, height * rows))
|
|
|
|
for r in range(0, rows):
|
|
|
|
for c in range(0, cols):
|
|
|
|
i = r * rows + c
|
|
|
|
grid_img.paste(image_list[i], (c * width, r * height))
|
2022-08-25 21:26:48 +00:00
|
|
|
|
|
|
|
return grid_img
|
2022-08-26 07:15:42 +00:00
|
|
|
|
|
|
|
|
|
|
|
class PromptFormatter:
|
|
|
|
def __init__(self, t2i, opt):
|
2022-08-25 04:42:37 +00:00
|
|
|
self.t2i = t2i
|
|
|
|
self.opt = opt
|
|
|
|
|
|
|
|
def normalize_prompt(self):
|
2022-08-26 07:15:42 +00:00
|
|
|
"""Normalize the prompt and switches"""
|
|
|
|
t2i = self.t2i
|
|
|
|
opt = self.opt
|
2022-08-25 04:42:37 +00:00
|
|
|
|
|
|
|
switches = list()
|
|
|
|
switches.append(f'"{opt.prompt}"')
|
|
|
|
switches.append(f'-s{opt.steps or t2i.steps}')
|
|
|
|
switches.append(f'-b{opt.batch_size or t2i.batch_size}')
|
|
|
|
switches.append(f'-W{opt.width or t2i.width}')
|
|
|
|
switches.append(f'-H{opt.height or t2i.height}')
|
|
|
|
switches.append(f'-C{opt.cfg_scale or t2i.cfg_scale}')
|
|
|
|
switches.append(f'-m{t2i.sampler_name}')
|
|
|
|
if opt.init_img:
|
|
|
|
switches.append(f'-I{opt.init_img}')
|
|
|
|
if opt.strength and opt.init_img is not None:
|
|
|
|
switches.append(f'-f{opt.strength or t2i.strength}')
|
2022-08-26 06:17:14 +00:00
|
|
|
if opt.gfpgan_strength:
|
|
|
|
switches.append(f'-G{opt.gfpgan_strength}')
|
2022-08-25 04:42:37 +00:00
|
|
|
if t2i.full_precision:
|
|
|
|
switches.append('-F')
|
|
|
|
return ' '.join(switches)
|