| 100 | |
| 101 | |
| 102 | def grid(x_file: str, y_file: str): |
| 103 | def set_param(line): |
| 104 | param = line.split(':', maxsplit=1) |
| 105 | k = param[0].strip() |
| 106 | v = param[1].strip() if len(param) > 1 else '' |
| 107 | if k == 'prompt': |
| 108 | options.prompt += f'{v} ' # prompt is appended so its not overwritten |
| 109 | elif k == 'lora': |
| 110 | options.prompt += f'<lora:{v}> ' # lora is appended to prompt |
| 111 | else: |
| 112 | setattr(options, k, v) |
| 113 | |
| 114 | log.info(server) |
| 115 | os.makedirs(server.folder, exist_ok=True) |
| 116 | try: |
| 117 | x = open(x_file, encoding='utf8').read().splitlines() if x_file is not None else [] |
| 118 | y = open(y_file, encoding='utf8').read().splitlines() if y_file is not None else [] |
| 119 | except Exception as e: |
| 120 | log.error(f'read file: x={x_file} y={y_file} {e}') |
| 121 | return |
| 122 | x = [line for line in x if ':' in line] |
| 123 | y = [line for line in y if ':' in line] |
| 124 | t0 = time.time() |
| 125 | log.info(f'grid: x={len(x)} y={len(y)} prefix={server.name}') |
| 126 | vertical = [] |
| 127 | Image.MAX_IMAGE_PIXELS = None |
| 128 | for j in range(max(1, len(y))): |
| 129 | horizontal = [] |
| 130 | labels = [] |
| 131 | for i in range(max(1, len(x))): |
| 132 | if len(x) > i: |
| 133 | set_param(x[i]) |
| 134 | if len(y) > j: |
| 135 | set_param(y[j]) |
| 136 | images = generate(i, j) |
| 137 | if images is not None and len(images) > 0: |
| 138 | horizontal.extend(images) |
| 139 | labels.append(f'{x[i] if len(x) > i else ""}\n{y[j] if len(y) > j else ""}') |
| 140 | options.prompt = '' # reset prompt |
| 141 | if server.grids: |
| 142 | if len(horizontal) == 0: |
| 143 | log.warning(f'grid: empty row={j}') |
| 144 | continue |
| 145 | merged = merge(horizontal, horizontal=True, labels=labels if server.labels else None) |
| 146 | vertical.append(merged) |
| 147 | if server.grids: |
| 148 | if len(vertical) == 0: |
| 149 | log.warning('grid: empty grid') |
| 150 | return |
| 151 | merged = merge(vertical, horizontal=False) |
| 152 | fn = os.path.join(server.folder, f'{server.name}.jpg') |
| 153 | merged.save(fn) |
| 154 | log.info(f'grid: size={merged.size} fn="{fn}"') |
| 155 | t1 = time.time() |
| 156 | log.info(f'done: time={t1-t0:.2f}') |
| 157 | |
| 158 | |
| 159 | if __name__ == "__main__": |