move make_grid into image_utils

This commit is contained in:
Kevin Gibbons 2022-08-30 21:36:38 -07:00
parent b983d61e93
commit d566ee092a
3 changed files with 24 additions and 27 deletions

View File

@ -1,3 +1,4 @@
from math import sqrt, floor, ceil
from PIL import Image
class InitImageResizer():
@ -51,4 +52,22 @@ class InitImageResizer():
return new_image
def make_grid(image_list, rows=None, cols=None):
image_cnt = len(image_list)
if None in (rows, cols):
rows = floor(sqrt(image_cnt)) # try to make it square
cols = ceil(image_cnt / rows)
width = image_list[0].width
height = image_list[0].height
grid_img = Image.new('RGB', (width * cols, height * rows))
i = 0
for r in range(0, rows):
for c in range(0, cols):
if i >= len(image_list):
break
grid_img.paste(image_list[i], (c * width, r * height))
i = i + 1
return grid_img

View File

@ -2,16 +2,13 @@
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.
into the PNG.
PromptFormatter -- Utility for converting a Namespace of prompt parameters
back into a formatted prompt string with command-line switches.
"""
import os
import re
from math import sqrt, floor, ceil
from PIL import Image, PngImagePlugin
from PIL import PngImagePlugin
# -------------------image generation utils-----
@ -42,26 +39,6 @@ class PngWriter:
image.save(path, 'PNG', pnginfo=info)
return path
# TODO move this to its own helper function; it's not really a method of pngwriter
def make_grid(self, image_list, rows=None, cols=None):
image_cnt = len(image_list)
if None in (rows, cols):
rows = floor(sqrt(image_cnt)) # try to make it square
cols = ceil(image_cnt / rows)
width = image_list[0].width
height = image_list[0].height
grid_img = Image.new('RGB', (width * cols, height * rows))
i = 0
for r in range(0, rows):
for c in range(0, cols):
if i>=len(image_list):
break
grid_img.paste(image_list[i], (c * width, r * height))
i = i + 1
return grid_img
class PromptFormatter:
def __init__(self, t2i, opt):

View File

@ -12,6 +12,7 @@ import time
import ldm.dream.readline
from ldm.dream.pngwriter import PngWriter, PromptFormatter
from ldm.dream.server import DreamServer, ThreadingDreamServer
from ldm.dream.image_util import make_grid
def main():
"""Initialize command-line parsers and the diffusion model"""
@ -226,7 +227,7 @@ def main_loop(t2i, outdir, prompt_as_dir, parser, infile):
t2i.prompt2image(image_callback=image_writer, **vars(opt))
if do_grid and len(grid_images) > 0:
grid_img = file_writer.make_grid(list(grid_images.values()))
grid_img = make_grid(list(grid_images.values()))
first_seed = next(iter(seeds))
filename = f'{prefix}.{first_seed}.png'
# TODO better metadata for grid images