#!/usr/bin/env python # -*- coding: utf-8 -*- # This script is (C) Copyright 2023, P. Lutus: https://www.arachnoid.com # and is released under the GPL: https://www.gnu.org/licenses/gpl-3.0.en.html # This is the A1111 version of the matrix script import json import requests import io import base64 import os from dataclasses import dataclass from PIL import Image #, PngImagePlugin class MultiDraw: def __init__(self): self.destpath = 'images' self.server='http://localhost:7860' self.checkpoint = 'sd_xl_base_1.0.safetensors [31e35c80fc]' self.sampler_name = 'Euler' self.steps = 20 self.cfg_scale = 7 self.width = 1024 self.height = 1024 self.seed = 333 self.batch_size = 1 def display_json(self,title,json): print(f'JSON data dump: {title}') for key in json: print(f' {key:12s} : {json[key]}') def draw_image(self,item, draw = True): filepath = f'{item.destdir}/output_row_{item.row:03d}_col_{item.col:03d}.png' if draw: print(f'Drawing "{item.prompt:60s}", row {item.row:03d} col {item.col:03d}') # see localhost:7860/docs for full explanation payload = { "prompt": item.prompt, "negative_prompt": item.negative_prompt, 'seed' : self.seed, "width" : self.width, "height" : self.height, "sampler_name" : self.sampler_name, "cfg_scale": self.cfg_scale, "steps": self.steps, 'batch_size': self.batch_size, } override_settings = { "sd_model_checkpoint": self.checkpoint, } override_payload = { "override_settings": override_settings, } payload.update(override_payload) #print(payload) response = requests.post(url=f'{self.server}/sdapi/v1/txt2img', json=payload) print(f'Response: ${response}') r = response.json() if 'images' in r: base64_image = r['images'][0] print(f'Successful image generation, saving {filepath}...') # image info is automatically saved with the file with open(filepath, 'wb') as fp: fp.write(base64.b64decode(base64_image)) else: if 'info' in r: self.display_json("Error",r['info']) return filepath @dataclass class ImageData: destdir: str prompt: str negative_prompt: str row: int col: int rows = ( 'teenage girl full face', 'still life', 'grumpy old man', 'picnic in the park', 'open-air market', 'children at play', 'sailors, boat, stormy sea', 'post-apocalyptic decaying city', ) # photorealistic and some other arguments must # precede the prompt, so is marked by < # others must follow and have 'by ' prefix columns = ( ' """ html += '\n' for coln, column in enumerate(columns): if column[0] == '<': prompt = f'{column[1:]}' else: prompt = column html += f'\n' html += '\n' for rown,row in enumerate(rows): if rown == 0: negative_prompt = '' else: negative_prompt = """ out of frame, lowres, text, error, cropped, worst quality, low quality, jpeg artifacts, ugly, duplicate, morbid, mutilated, extra fingers, mutated hands, poorly drawn hands, poorly drawn face, mutation, deformed, blurry, dehydrated, bad anatomy, bad proportions, extra limbs, cloned face, disfigured, gross proportions, malformed limbs, missing arms, missing legs, extra arms, extra legs, fused fingers, too many fingers, long neck, username, watermark, signature, """ print('') html += f""" """ for coln,column in enumerate(columns): if column[0] == '<': prompt = f'{column[1:]} {row}' else: prompt = f'{row} by {column}' item = ImageData(md.destpath,prompt,negative_prompt,rown,coln) # note an available draw argument: True/False # this saves time until prompt # debugging is complete filepath = md.draw_image(item, False) html += f""" """ html += '' html += """
{prompt}
‟{row}”
""" with open(f'keyindex.html','w') as f: f.write(html)