Repository: mcmonkeyprojects/sd-infinity-grid-generator-script
Branch: master
Commit: 0082d79eb9a4
Files: 16
Total size: 190.6 KB
Directory structure:
gitextract_jp1goxq0/
├── .github/
│ └── FUNDING.yml
├── .gitignore
├── LICENSE.txt
├── README.md
├── assets/
│ ├── a1111webui.js
│ ├── grid.schema.json
│ ├── images/
│ │ └── put-usable-images-here.txt
│ ├── jsgif.js
│ ├── page.html
│ ├── proc.js
│ └── styles.css
├── gridgencore.py
├── install.py
├── javascript/
│ └── infinity_grid.js
├── scripts/
│ └── infinity_grid.py
└── style.css
================================================
FILE CONTENTS
================================================
================================================
FILE: .github/FUNDING.yml
================================================
github: mcmonkey4eva
================================================
FILE: .gitignore
================================================
.vscode/
assets/*.yml
assets/styles-user.css
__pycache__/
assets/images/**/*.png
assets/images/**/*.jpg
assets/images/**/*.webp
================================================
FILE: LICENSE.txt
================================================
The MIT License (MIT)
Copyright (c) 2022-2023 Alex "mcmonkey" Goodwin
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
================================================
FILE: README.md
================================================
# Stable Diffusion Infinity Grid Generator

### Concept
Extension for the [AUTOMATIC1111 Stable Diffusion WebUI](https://github.com/AUTOMATIC1111/stable-diffusion-webui) that generates infinite-dimensional grids.
An "infinite axis grid" is like an X/Y plot grid, but with, well, more axes on it. Of course, monitors are 2D, so this is implemented in practice by generating a webpage that lets you select which two primary axes to display, and then choose the current value for each of the other axes.
### Goals and Use Cases
The primary goal is to let people generate their own fancy grids to explore how different settings affect their renders in a convenient form.
Another goal of this system is to develop educational charts, to provide a universal answer to the classic question of "what does (X) setting do? But what about with (Y)?" - [The MegaGrid](https://sd.mcmonkey.org/megagrid/). There is a built in ability to add description text to fields, for the specific purpose of enhancing educational page output.
### Pros/Cons
The advantage of this design is it allows you to rapidly compare the results of different combinations of settings, without having to wait to for generation times for each specific sub-grid as-you-go - you just run it all once in advance (perhaps overnight for a large run), and then after that browse through it in realtime.
The disadvantage is that time to generate a grid is exponential - if you have 5 samplers, 5 seeds, 5 step counts, 5 CFG scales... that's 5^4, or 625 images. Add another variable with 5 options and now it's 3125 images. You can see how this quickly jumps from a two minute render to a two hour render.
--------------
### Table of Contents
- [Examples](#Examples)
- [Status](#Status)
- [Installation](#Installation)
- [Basic Usage](#Basic-Usage)
- [Advanced Usage](#Advanced-Usage)
- [1: Grid Definition File](#1-grid-definition-file)
- [Supported Extensions](#supported-extensions)
- [Dynamic Thresholding (CFG Scale Fix)](#dynamic-thresholding-cfg-scale-fix)
- [ControlNet](#controlnet)
- [2: Grid Content Generation via WebUI](#2-grid-content-generation-via-webui)
- [3: Using The Output](#3-using-the-output)
- [4: Expanding Later](#4-expanding-later)
- [Credits](#credits)
- [Common Issues](#common-issues)
- [License](#License)
--------------
### Examples
Here's a big MegaGrid using almost every mode option in one, with detailed educational descriptions on every part: https://sd.mcmonkey.org/megagrid/
Here's a very small web demo you can try to test how the output looks and works: https://mcmonkeyprojects.github.io/short_example and you can view the generated asset files for that demo [here](https://github.com/mcmonkeyprojects/mcmonkeyprojects.github.io/tree/master/short_example).

--------------
### Status
Current overall project status (as of December 2023): **Works well, actively maintained**. Has been generally well tested. The core has been ported to other environments and stress-tested for multiple real projects that depend on large grids and rapid grid analysis.
A version of this project is also available for [StableSwarmUI, here](https://github.com/Stability-AI/StableSwarmUI/blob/master/src/BuiltinExtensions/GridGenerator/README.md) which also lets you use it with Comfy and other backends
--------------
### Installation
- You must have the [AUTOMATIC1111 Stable Diffusion WebUI](https://github.com/AUTOMATIC1111/stable-diffusion-webui) already installed and working. Refer to that project's readme for help with that.
- Open the WebUI, go to to the `Extensions` tab
- -EITHER- Option **A**:
- go to the `Available` tab with
- click `Load from` (with the default list)
- Scroll down to find `Infinity Grid Generator`, or use `CTRL+F` to find it
- -OR- Option **B**:
- Click on `Install from URL`
- Copy/paste this project's URL into the `URL for extension's git repository` textbox: `https://github.com/mcmonkeyprojects/sd-infinity-grid-generator-script`
- Click `Install`
- Restart or reload the WebUI
--------------
### Basic Usage

- Basic usage is very simple!
- select the script under `Scripts`
- Leave file as `Create in UI`
- Select `Axis 1 Mode` as whatever mode you want, like `Seed` or `Sampler`
- In the `Axis 1 Value` box, you can type a comma-separated list like `1, 2, 3`, or a double-pipe separated list like `1girl, booru style, prompt || 1boy, has commas, so pipes avoid problems`
- Repeat for as many axes as you want. When you configure axis #4, it will automatically add 4 more rows of axes to fill in. Leave any extras empty.
- Click your main `Generate` button and wait for it to finish.
- When it's done, click the button labeled `Page will be at (Click me) outputs/...` to view the full page.
- Note that a `config.yml` is saved in the output directory, which you can copy to your `assets` folder if you want to reuse it.
--------------
### Advanced Usage
Usage comes in three main steps:
- [1: Build a grid definition](#1-grid-definition-file)
- [2: Generate its contents](#2-grid-content-generation-via-webui)
- [3: View/use the generated output page](#3-using-the-output)
- You can also [expand your grid later](#4-expanding-later)
--------------
### 1: Grid Definition File

- Grid information is defined by **YAML files**, in the extension folder under `assets`. Find the `assets/short_example.yml` file to see an example of the full format.
**If you do not want to follow an example file:**
- You can **create new files** in the assets directory (as long as the `.yml` extension stays), or copy/paste an example file and edit it. I recommend you do not edit the actual example file directly to avoid git issues.
- I recommend editing with a **good text editor**, such as *VS Code* or *Notepad++*. Don't use *MS Word* or *Windows Notepad* as those might cause trouble.
- All text inputs allow for **raw HTML**, so, be careful. You can use `<` for `<`, and `>` for `>`, `:` for `:`, and `&` for `&`.
- The file must have key `grid`, with subkey `title` and `description` to define the file data.
- It must also have `format` as `jpg` or `png`
- It can optionally also have `params` to specify any default parameters.
- It can optionally define `show descriptions`, `autoscale`, and `sticky` as `true` or `false` to change default web-viewer settings.
- It can optionally define `x axis`, `y axis`, `x super axis`, and `y super axis` as axis IDs to change the default web-viewer axes.
- The file can optionally have key `variables` with subkey/value pairs as replacements, for example `(type): waffle` - then later in a param value you can use `a picture of a (type)` to automatically fill the variable. These can be in any format you desire, as they are simple text replacements. They apply to all values, including titles, descriptions, and params (this was added for [valconius in issue #16](https://github.com/mcmonkeyprojects/sd-infinity-grid-generator-script/issues/16)).
- The file must have key `axes` to define the list of axes. This is a map-list key - meaning, add subkeys to create a list of each axis.
- The simplest format for an axis is a setting name as the key and a comma-separated list of values inside.
- For example, `seed: 1, 2, 3` is a valid and complete axis.
- If commas may be problematic, you many instead use two pipes `||` to separate values - for example `prompt: 1girl, booru style, commas || 1boy, more commas, etc`. If `||` is used, commas will be ignored for splitting
- For numeric inputs, you can use ellipses notation to quickly shorthand long lists. For example: `seed: 1, 2, ..., 10` automatically fills to `1, 2, 3, 4, 5, 6, 7, 8, 9, 10`. Or, `cfg scale: 7.0, 7.1, ..., 7.8` automatically fills to `7.0, 7.1, 7.2, 7.3, 7.4, 7.5, 7.6, 7.7, 7.8`.
- Note that you must have two entries before and one after - the step rate is defined as the space between the two prior and the total range is defined as between the one prior and the one after.
- You can use ellipses multiple times in one set, for example `1, 2, ..., 6, 8, ... 14` will fill to `1, 2, 3, 4, 5, 6, 8, 10, 12, 14` (note how 1-6 are spaced 1 apart, but 6-14 are spaced two apart).
- Each axis must have a `title`, and `values`. It can optionally have a `description`.
- You can also optionally have `default: (value_id)` to set the default selected tab.
- There are two ways to do a value in the value list:
- Option 1: just do like `steps=10` ... this will set title to `10`, and param `steps` to value `10`, with no description.
- Option 2: Add a submapping with key `title`, and optional `description`, and then `params` as a sub map of parameters like `steps: 10`
- A value with a full format can be set to skip rendering via adding `skip: true`. This is useful for eg if some values are only valid in certain combinations, you can do runs with some skipped. Skipped values will be kept in the HTML output.
- A value may specify `show: false` to default uncheck the 'Show value' advanced option.
Micro example:
```yml
grid:
title: Tiny example
author: someone
description: This is just to show core format. View the example `.yml` files in assets for better examples.
format: jpg
axes:
sampler: Euler, DDIM
seed: 1, 2, 3
```
- Names and descriptions can always be whatever you want, as HTML text.
#### Settings supported for parameters
| Name | Type | Example | Notes |
| --- | --- | --- | ----------- |
| `Sampler` | Named | `DDIM`, `euler`, ... | |
| `Model` | Filename | `sd-v1-5` | Note that `Model` and `VAE` are **global settings**, and as such you should not have an axis where some values specify one of those params but others don't, as this will cause an unpredictable model selection for the values that lack specificity. |
| `VAE` | Filename | `kl-f8-anime2` | See note on `Model` above |
| `Prompt` | Text | `a cat` | |
| `Negative Prompt` | Text | `gross, weird, bad` | |
| `Prompt Replace` | Text-Pair | `some_tag = new text here` | Note the `=` symbol to separate the original text with the new text. That will change a prompt of for example `my prompt with some_tag stuff` to `my prompt with new text here stuff`. Unlike other modes, the PromptReplace is case-sensitive - if you use capitals in your prompt, you need capitals in your replace matcher. If you want multiple replacements in one value, you can number them, like `Prompt Replace 1` and `Prompt Replace 2` and etc. When building a simple list in UI, you can just do eg `cat, dog, waffle` and it will understand to replace `cat` in the base prompt first with `cat`, then `dog`, then `waffle` (ie you can skip the `=` for simple usages). |
| `Styles` | Text | `photo`, `cartoon`, ... | Automatically includes UI styles to your prompt. Can use comma-separated list to have multiple. Note this means in the UI you can do for example `photo, cinematic ||| anime, cartoon` to compare multiple combos easily. |
| `Seed` | Integer | `1`, `2`, `3`, ... | |
| `Steps` | Integer | `20`, `50`, ... | |
| `CFG Scale` | Decimal | `5`, `7.5`, `12`, ... | |
| `Width` | Integer | `512`, `768`, ... | Initial generation width. |
| `Height` | Integer | `512`, `768`, ... | Initial generation height. |
| `Out Width` | Integer | `512`, `768`, ... | What resolution to save the image as (if unspecified, uses `Width`). Useful to save filespace. |
| `Out Height` | Integer | `512`, `768`, ... | Refer to `Out Width`. |
| `Clip Skip` | Integer | `1`, `2` | Use `2` for NAI-like models, `1` for the rest. |
| `Var Seed` | Integer | `0`, `1`, ... | Variation seed, use with `Var Strength`. |
| `Var Strength` | Decimal | `0`, `0.5`, ..., `1` | Variation seed strength. |
| `Restore Faces` | Named | `true`, `false`, `GFPGan`, `CodeFormer` | Limited to the given example inputs only. |
| `CodeFormer Weight` | Decimal | `0`, `0.5`, ..., `1` | Only applicable if `Restore Faces` is set to `CodeFormer`. |
| `Denoising` | Decimal | `0`, `0.5`, ..., `1` | Denoising strength for img2img or HR fix. |
| `ETA` | Decimal | `0`, `0.5`, ..., `1` | ? |
| `ETA Noise Seed Delta` | Integer | `0`, `31337` | use `31337` to replicate NovelAI results, use `0` for anything else. Not very useful. |
| `Sigma Churn` | Decimal | `0`, `0.5`, ..., `1` | Sampler parameter, rarely used. |
| `Sigma Tmin` | Decimal | `0`, `0.5`, ..., `1` | Sampler parameter, rarely used. |
| `Sigma Tmax` | Decimal | `0`, `0.5`, ..., `1` | Sampler parameter, rarely used. |
| `Sigma Noise` | Decimal | `0`, `0.5`, ..., `1` | Sampler parameter, rarely used. |
| `Tiling` | Boolean | `true`, `false` | Useful for textures. |
| `Image Mask Weight` | Decimal | `0`, `0.5`, ..., `1` | Conditional image mask weight. Only applies to img2img or HR fix. |
| `Enable Highres Fix` | Boolean | `true`, `false` | Required for other HR settings to work. Defaults denoising strength to `0.75` if not specified. Only valid in txt2img. |
| `Highres Scale` | Decimal | `2`, `2.5`, ..., `16` | How much to scale by for HR fix. |
| `Highres Steps` | Integer | `20`, `50`, ... | Secondary steps for HR fix. |
| `Highres Upscaler` | Named | `None`, `Latent`, ... | Upscaler mode to use prior to running Highres Fix. |
| `Highres Resize Width` | Integer | `512`, `768`, ... | Resolution to target as final output size for Highres Fix, overrides `Highres Scale`. |
| `Highres Resize Height` | Integer | `512`, `768`, ... | See `Highres Resize Width` above. |
| `Highres Upscale To Width` | Integer | `512`, `768`, ... | Resolution to upscale to prior to running Highres Fix. |
| `Highres Upscale To Height` | Integer | `512`, `768`, ... | See `Highres Upscale To Height` above. |
| `Image CFG Scale` | Decimal | `5`, `7.5`, `12`, ... | Image CFG Scale, for Instruct pix2pix usage. |
| `Use Result Index` | Integer | `0`, `1`, ... | Special trick to get a non-zero result image index, eg for ControlNet secondary output image. |
- All setting names are **case insensitive and spacing insensitive**. That means `CFG scale`, `cfgscale`, `CFGSCALE`, etc. are all read as the same.
- Inputs where possible also similarly insensitive, including model names.
- Inputs have error checking at the start, to avoid the risk of it working fine until 3 hours into a very big grid run.
- Note that it will be processed from bottom to top
- so if you have first `samplers` DDIM and Euler, then `steps` 20 and 10, then `seeds` 1 and 2, it will go in this order:
- Sampler=DDIM, Steps=20, Seed=1
- Sampler=DDIM, Steps=20, Seed=**2**
- Sampler=DDIM, Steps=**10**, Seed=1
- Sampler=DDIM, Steps=10, Seed=2
- Sampler=**Euler**, Steps=20, Seed=1
- Sampler=Euler, Steps=20, Seed=2
- Sampler=Euler, Steps=10, Seed=1
- Sampler=Euler, Steps=10, Seed=2
- So, things that take time to load, like `Model`, should be put near the top, so they don't have to be loaded repeatedly.
--------------
### Supported Extensions
#### Dynamic Thresholding (CFG Scale Fix)
Extension docs:
| Name | Type | Example | Notes |
| --- | --- | --- | ----------- |
| `DynamicThreshold Enable` | Boolean | `true`, `false` | |
| `DynamicThreshold Mimic Scale` | Decimal | `5`, `7.5`, `12`, ... | |
| `DynamicThreshold Threshold Percentile` | Decimal | `0`, `0.5`, ..., `1.0` | |
| `DynamicThreshold Mimic Mode` | Named | `Constant`, `Linear Down`, `Cosine Down`, `Half Cosine Down`, `Linear Up`, `Cosine Up`, `Half Cosine Up`, `Power Up` | |
| `DynamicThreshold CFG Mode` | Named | `Constant`, `Linear Down`, `Cosine Down`, `Half Cosine Down`, `Linear Up`, `Cosine Up`, `Half Cosine Up`, `Power Up` | |
| `DynamicThreshold Mimic Scale Minimum` | Decimal | `5`, `7.5`, `12`, ... | |
| `DynamicThreshold CFG Scale Minimum` | Decimal | `5`, `7.5`, `12`, ... | |
| `DynamicThreshold Power Value` | Decimal | `2`, `4`, ... | For `Power Up` mode only. |
| `DynamicThreshold Scaling Startpoint` | Named | `ZERO`, `MEAN` | |
| `DynamicThreshold Variability Measure` | Named | `STD`, `AD` | |
| `DynamicThreshold Interpolate Phi` | Decimal | `0`, `0.5`, ..., `1.0` | |
| `DynamicThreshold Separate Feature Channels` | Boolean | `true`, `false` | |
#### ControlNet
Extension docs:
Note: must enable `Allow other script to control this extension` in `Settings` -> `ControlNet`
| Name | Type | Example | Notes |
| --- | --- | --- | ----------- |
| `ControlNet Enable` | Boolean | `true`, `false` | |
| `ControlNet Preprocessor` | Named | `none`, `canny`, `depth`, `hed`, `mlsd`, `normal_map`, `openpose`, `openpose_hand`, `pidinet`, `scribble`, `fake_scribble`, `segmentation` | |
| `ControlNet Model` | Named | `diff_control_sd15_canny_fp16`, ... | |
| `ControlNet Weight` | Decimal | `0.0`, `0.5`, ..., `2.0` | |
| `ControlNet Guidance Strength` | Decimal | `0.0`, `0.5`, ..., `1.0` | |
| `ControlNet Annotator Resolution` | Integer | `64`, `512`, ..., `2048` | |
| `ControlNet Threshold A` | Integer | `64`, `512`, ..., `256` | |
| `ControlNet Threshold B` | Integer | `64`, `512`, ..., `256` | |
| `ControlNet Image` | Text | `pose.png`, `otherpose.jpg`, ... | Put image files in `(EXTENSION FOLDER)/assets/images/`, as `png`, `jpg`, or `webp`. Subfolders allowed. |
#### Other Extensions
- Any extension has the ability to add its own modes with the following code:
```py
# Verify grid extension is present
import importlib
if importlib.util.find_spec("gridgencore") is not None:
import gridgencore
from gridgencore import GridSettingMode
# p is the SD processing object, v is the value
def apply(p, v):
p.some_setting_here = v
# dry: bool, type: str, apply: callable, min: float = None, max: float = None, clean: callable = None
gridgencore.registerMode("mySettingNameHere", GridSettingMode(True, "text", apply))
# for apply if the param is a 'p' field, you can use gridgencore.apply_field("fieldname")
```
--------------
### 2: Grid Content Generation via WebUI

- Open the WebUI
- Go to the `txt2img` or `img2img` tab
- At the bottom of the page, find the `Script` selection box, and select `Generate Infinite-Axis Grid`
- Select options at will. You can hover your mouse over each option for extra usage information.
- Select your grid definition file from earlier.
- If it's not there, you might just need to hit the Refresh button on the right side.
- If it's still not, there double check that your file is in the `assets/` folder of the extension, and that it has a proper `.yml` extension.
- Hit your `Generate` button (the usual big orange one at the top), and wait.
- The output folder will be named based on your `.yml` file's name.
--------------
### 3: Using The Output

- Find the `index.html` file
- It's normally in `(your grid output directory)/(filename)/index.html`
- The example file might output to `outputs/grids/short_example/index.html`
- Open the HTML file in a browser. Enjoy.
- If you want to share the content, just copy/paste the whole folder into a webserver somewhere.
- Or upload to github and make GitHub.io pages host it for you. [See example here](https://github.com/mcmonkeyprojects/mcmonkeyprojects.github.io)
- You have a few different clickable options:
- `Show descriptions of axes and values`: if you used descriptions, you can uncheck this box to hide them. Helps save space for direct viewing.
- `Auto-scale images to viewport width`: this is handy for a few different scenarios
- A: if your images are small and your screen is big, checking this option makes them bigger
- B: if your images are so big they're going off the edge, checking this option makes them smaller
- C: if checked, you can zoom in/out of the page using your browser zoom (CTRL + Mouse Wheel) to change the UI size without affecting the image size
- if unchecked, you can zoom in/out to change the size of the images.
- `Sticky navigation`: when checked, the navigation will stick to the top of the screen as you scroll down. Helps you quickly change axes while scrolling around without losing your place.
- `Advanced Settings`: when clicked, it will open a dropdown with more advanced settings
- 
- `Auto cycle every (x) seconds`: you can set these to non-zero values to the grid automatically change settings over time. For example, if you set your "Seed" option to "3 seconds", then every 3 seconds the seed will change (cycling between the options you have in order). This was suggested by [itswhateverman in issue #2](https://github.com/mcmonkeyprojects/sd-infinity-grid-generator-script/issues/2).
- `Show value`: you can uncheck any box to hide a value from the grid. Helps if you want to ignore some of the values and get a clean grid of just the ones you care about. This was suggested by [piyarsquare in issue #4](https://github.com/mcmonkeyprojects/sd-infinity-grid-generator-script/issues/4).
- You can also click on any image to view it fullscreen and see its metadata (if included in output). While in this mode, you can use arrow keys to quick navigate between images, or press Escape to close (This suggested by [itswhateverman in issue #5](https://github.com/mcmonkeyprojects/sd-infinity-grid-generator-script/issues/5) and [issue #17](https://github.com/mcmonkeyprojects/sd-infinity-grid-generator-script/issues/17)).
- You can also set the `X Super-axis` and `Y Super-axis` to unique axes to get a grid-of-grids!
- 
- You can also quicksave preview images of the grid, or even generate GIFs that autoscroll through an axis, all with a few easy buttons on the viewer page!
--------------
### 4: Expanding Later
If you want to add more content to a grid you already made, you can do that:
- Use the same `yml` file.
- You can add new values to an axis freely.
- If you remove values, they will be excluded from the output but pre-existing generated images won't be removed.
- You can add axes, but you'll have to regenerate all images if so.
- Probably save as a new filename in that case.
- If you're just adding a new value, make sure to leave `overwriting existing images` off.
----------------------
### Credits
- This design was partially inspired by the "XYZ Plot" script by "xrypgame" (not to be confused with the "XYZ Plot" script in Auto WebUI which is actually just the "X/Y Plot" script but they added a "Z" lol)
- Sections of code are referenced from the WebUI itself, and its default "X/Y Plot" script (since renamed to labeled "XYZ Plot").
- Some sections of code were referenced from various other relevant sources, for example the Dreambooth extension by d8ahazard was referenced for a JavaScript code trick (titles override).
- Some sections of code were referenced from, well, random StackOverflow answers and a variety of other googled up documentation and answer sites. I haven't kept track of them, but I'm glad to live in a world where so many developers are happy and eager to help each other learn and grow. So, thank you to all members of the FOSS community!
- Thanks to the authors of all [merged PRs](https://github.com/mcmonkeyprojects/sd-infinity-grid-generator-script/pulls?q=is%3Apr+is%3Aclosed+is%3Amerged).
- Thanks to the authors of all issues labeled as [Completed](https://github.com/mcmonkeyprojects/sd-infinity-grid-generator-script/issues?q=label%3ACompleted).
- Thanks to StabilityAI, RunwayML, CompVis for Stable Diffusion, and the researchers whose work was incorporated.
- Thanks to AUTOMATIC1111 and the long list of contributors for the WebUI.
----------------------
### Common Issues
```
File "stable-diffusion-webui\modules\images.py", line 508, in _atomically_save_image
image_format = Image.registered_extensions()[extension]
KeyError: '.jpg'
```
If you have this error, just hit generate again. I'm not sure why it happens, it just does at random sometimes on the first time the WebUI starts up. It seems to happen when you use `OutWidth`/`OutHeight` settings and is prevented by running any generation without a custom out-resolution. Might be some required initialization is getting skipped when an image is rescaled?
----------------------
### Licensing pre-note:
This is an open source project, provided entirely freely, for everyone to use and contribute to.
If you make any changes that could benefit the community as a whole, please contribute upstream.
### The short of the license is:
You can do basically whatever you want, except you may not hold any developer liable for what you do with the software.
### The long version of the license follows:
The MIT License (MIT)
Copyright (c) 2022-2023 Alex "mcmonkey" Goodwin
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
================================================
FILE: assets/a1111webui.js
================================================
/**
* This file is part of Infinity Grid Generator, view the README.md at https://github.com/mcmonkeyprojects/sd-infinity-grid-generator-script for more information.
*/
function genParamQuote(text) {
// Referenced to match generation_parameters_copypaste.py - quote(text)
if (!text.includes(',')) {
return text;
}
return '"' + text.toString().replaceAll('\\', '\\\\').replaceAll('"', '\\"') + '"';
}
function formatMet(name, val, bad) {
if (val == null) {
return '';
}
val = val.toString();
if (bad != undefined && val == bad) {
return '';
}
return name + ': ' + genParamQuote(val) + ', ';
}
function formatMetadata(valSet) {
var count = Object.keys(valSet).length;
if (count == 0) {
return '';
}
else if (count == 1) {
return valSet['error'];
}
// Referenced to match processing.py - create_infotext(p)
var negative = valSet['negativeprompt'];
if (negative.length > 0) {
negative = '\nNegative prompt: ' + negative;
}
const handled = ['steps', 'sampler', 'cfgscale', 'seed', 'restorefaces', 'width', 'height', 'model', 'varseed', 'varstrength', 'denoising', 'eta', 'clipskip', 'vae', 'sigmachurn', 'sigmatmin', 'sigmatmax', 'sigmanoise', 'prompt', 'negativeprompt', 'codeformerweight'];
var keyData = formatMet('Steps', valSet['steps'])
+ formatMet('Sampler', valSet['sampler'])
+ formatMet('CFG scale', valSet['cfgscale'])
+ formatMet('Seed', valSet['seed'])
+ formatMet('Face restoration', valSet['restorefaces'], 'false')
+ formatMet('Size', valSet['width'] + 'x' + valSet['height'])
// model hash
+ formatMet('Model', valSet['model'])
// Batch size, batch pos
+ formatMet('Variation seed', valSet['varseed'], '0')
+ formatMet('Variation seed strength', valSet['varstrength'], '0')
// Seed resize from
+ formatMet('Denoising strength', valSet['denoising'])
// Conditional mask weight
+ formatMet('Eta', valSet['eta'])
+ formatMet('Clip skip', valSet['clipskip'], '1')
// ENSD
;
// Not part of normal gen-params
var extraData = formatMet('VAE', valSet['vae'])
+ formatMet('Sigma Churn', valSet['sigmachurn'], '0')
+ formatMet('Sigma T-Min', valSet['sigmatmin'], '0')
+ formatMet('Sigma T-Max', valSet['sigmatmax'], '1')
+ formatMet('Sigma Noise', valSet['sigmanoise'], '1')
+ (valSet['restorefaces'] == 'CodeFormer' ? formatMet('CodeFormer Weight', valSet['codeformerweight']) : '');
var lastData = '';
for (const [key, value] of Object.entries(valSet)) {
if (!handled.includes(key) && value != null) {
lastData += `${key}: ${value}, `;
}
}
if (lastData.length > 2) {
lastData = '\n(Other): ' + lastData.substring(0, lastData.length - 2);
}
keyData = keyData.substring(0, keyData.length - 2);
if (extraData.length > 2) {
extraData = extraData.substring(0, extraData.length - 2);
}
return valSet['prompt'] + negative + '\n' + keyData + '\n' + extraData + lastData;
}
function crunchParamHook(data, key, value) {
if (key == 'promptreplace') {
var replacers = value.split('=', 2);
var match = replacers[0].trim();
var replace = replacers[1].trim();
data['prompt'] = data['prompt'].replaceAll(match, replace);
data['negativeprompt'] = data['negativeprompt'].replaceAll(match, replace);
return true;
}
return false;
}
================================================
FILE: assets/grid.schema.json
================================================
{
"$schema": "http://json-schema.org/draft-06/schema#",
"type": "object",
"properties": {
"grid": {
"$ref": "#/definitions/Grid"
},
"axes": {
"$ref": "#/definitions/Axes"
},
"variables": {
"$ref": "#/definitions/Variables"
}
},
"required": [
"axes",
"grid"
],
"title": "InfinityGrid",
"definitions": {
"Variables": {
"type": "object",
"patternProperties": {
"\\(.*\\)": {
"oneOf": [
{"type": "string"},
{"type": "number"}
]
}
}
},
"Axes": {
"type": "object",
"patternProperties": {
".*": {
"$ref": "#/definitions/Axis"
}
},
"title": "Axes"
},
"Axis": {
"oneOf": [{
"type": "string"
},
{
"type": "object",
"properties": {
"title": {
"oneOf": [
{"type": "string"},
{"type": "number"}
]
},
"default": {
"oneOf": [{
"type": "string"
},
{
"type": "number"
}
]
},
"description": {
"type": "string"
},
"values": {
"patternProperties": {
".*": {
"$ref": "#/definitions/Value"
}
}
}
},
"required": ["title", "values"]
}]
},
"Value": {
"oneOf": [{
"type": "string",
"pattern": "(Sampler|sampler|Model|model|VAE|vae|Prompt|prompt|NegativePrompt|negativeprompt|Negative Prompt|negative prompt|PromptReplace|promptreplace|Prompt Replace|prompt replace|Prompt Replace|prompt replace|Seed|seed|Steps|steps|CFGScale|cfgscale|CFG Scale|cfg scale|CFGscale|CFG scale|Width|width|Height|height|OutWidth|outwidth|Out Width|out width|OutHeight|outheight|Out Height|out height|ClipSkip|clipskip|Clip Skip|clip skip|VarSeed|varseed|Var Seed|var seed|VarStrength|varstrength|Var Strength|var strength|CodeFormerWeight|codeformerweight|CodeFormer Weight|codeformer weight|Denoising|denoising|ETA|eta|ETANoiseSeedDelta|etanoiseseeddelta|ETA Noise Seed Delta|eta noise seed delta|SigmaChurn|sigmachurn|Sigma Churn|sigma churn|SigmaTmin|sigmatmin|Sigma Tmin|sigma tmin|SigmaTmax|sigmatmax|Sigma Tmax|sigma tmax|SigmaNoise|sigmanoise|Sigma Noise|sigma noise|Tiling|tiling|ImageMaskWeight|imagemaskweight|Image Mask Weight|image mask weight|EnableHighresFix|enablehighresfix|Enable Highres Fix|enable highres fix|HighresScale|highresscale|Highres Scale|highres scale|HighresSteps|highressteps|Highres Steps|highres steps|HighresUpscaler|highresupscaler|Highres Upscaler|highres upscaler|HighresResizeWidth|highresresizewidth|Highres Resize Width|highres resize width|HighresResizeHeight|highresresizeheight|Highres Resize Height|highres resize height|HighresUpscaleToWidth|highresupscaletowidth|Highres Upscale To Width|highres upscale to width|HighresUpscaleToHeight|highresupscaletoheight|Highres Upscale To Height|highres upscale to height|RestoreFaces|Restore Faces|restorefaces|restore faces|([dD]ynamic\\s?[Tt]hreshold.*)|)\\=.*"
},
{
"type": "object",
"properties": {
"title": {
"oneOf": [
{"type": "string"},
{"type": "number"}
]
},
"skip": {
"type": "boolean"
},
"show": {
"type": "boolean"
},
"description": {
"type": "string"
},
"params": {
"$ref": "#/definitions/GridParams"
}
},
"required": ["title", "params"]
}
]
},
"Grid": {
"type": "object",
"additionalProperties": false,
"properties": {
"title": {
"type": "string"
},
"author": {
"type": "string"
},
"description": {
"type": "string"
},
"params": {
"$ref": "#/definitions/GridParams"
},
"format": {
"type": "string",
"enum": ["png", "jpg", "webp"]
}
},
"required": [
"author",
"description",
"format",
"title"
],
"title": "Grid"
},
"GridParams": {
"type": "object",
"additionalProperties": false,
"properties": {
"Sampler": { "type": "string" }, "sampler": { "type": "string" },
"Model": { "type": "string" }, "model": { "type": "string" },
"VAE": { "type": "string" }, "vae": { "type": "string" },
"Prompt": { "type": "string" }, "prompt": { "type": "string" },
"NegativePrompt": { "type": "string" }, "negativeprompt": { "type": "string" }, "Negative Prompt": { "type": "string" }, "negative prompt": { "type": "string" },
"PromptReplace": { "type": "string" }, "promptreplace": { "type": "string" }, "Prompt Replace": { "type": "string" }, "prompt replace": { "type": "string" }, "Prompt Replace": { "type": "string" }, "prompt replace": { "type": "string" },
"Seed": { "type": "integer" }, "seed": { "type": "integer" },
"Steps": { "type": "integer" }, "steps": { "type": "integer" },
"CFGScale": { "type": "number" }, "cfgscale": { "type": "number" }, "CFG Scale": { "type": "number" }, "cfg scale": { "type": "number" }, "CFGscale": { "type": "number" }, "CFG scale": { "type": "number" },
"Width": { "type": "integer" }, "width": { "type": "integer" },
"Height": { "type": "integer" }, "height": { "type": "integer" },
"OutWidth": { "type": "integer" }, "outwidth": { "type": "integer" }, "Out Width": { "type": "integer" }, "out width": { "type": "integer" },
"OutHeight": { "type": "integer" }, "outheight": { "type": "integer" }, "Out Height": { "type": "integer" }, "out height": { "type": "integer" },
"ClipSkip": { "type": "integer" }, "clipskip": { "type": "integer" }, "Clip Skip": { "type": "integer" }, "clip skip": { "type": "integer" },
"VarSeed": { "type": "integer" }, "varseed": { "type": "integer" }, "Var Seed": { "type": "integer" }, "var seed": { "type": "integer" },
"VarStrength": { "type": "number" }, "varstrength": { "type": "number" }, "Var Strength": { "type": "number" }, "var strength": { "type": "number" },
"CodeFormerWeight": { "type": "number" }, "codeformerweight": { "type": "number" }, "CodeFormer Weight": { "type": "number" }, "codeformer weight": { "type": "number" },
"Denoising": { "type": "number" }, "denoising": { "type": "number" },
"ETA": { "type": "number" }, "eta": { "type": "number" },
"ETANoiseSeedDelta": { "type": "integer" }, "etanoiseseeddelta": { "type": "integer" }, "ETA Noise Seed Delta": { "type": "integer" }, "eta noise seed delta": { "type": "integer" },
"SigmaChurn": { "type": "number" }, "sigmachurn": { "type": "number" }, "Sigma Churn": { "type": "number" }, "sigma churn": { "type": "number" },
"SigmaTmin": { "type": "number" }, "sigmatmin": { "type": "number" }, "Sigma Tmin": { "type": "number" }, "sigma tmin": { "type": "number" },
"SigmaTmax": { "type": "number" }, "sigmatmax": { "type": "number" }, "Sigma Tmax": { "type": "number" }, "sigma tmax": { "type": "number" },
"SigmaNoise": { "type": "number" }, "sigmanoise": { "type": "number" }, "Sigma Noise": { "type": "number" }, "sigma noise": { "type": "number" },
"Tiling": { "type": "boolean" }, "tiling": { "type": "boolean" },
"ImageMaskWeight": { "type": "number" }, "imagemaskweight": { "type": "number" }, "Image Mask Weight": { "type": "number" }, "image mask weight": { "type": "number" },
"EnableHighresFix": { "type": "boolean" }, "enablehighresfix": { "type": "boolean" }, "Enable Highres Fix": { "type": "boolean" }, "enable highres fix": { "type": "boolean" },
"HighresScale": { "type": "number" }, "highresscale": { "type": "number" }, "Highres Scale": { "type": "number" }, "highres scale": { "type": "number" },
"HighresSteps": { "type": "integer" }, "highressteps": { "type": "integer" }, "Highres Steps": { "type": "integer" }, "highres steps": { "type": "integer" },
"HighresUpscaler": { "type": "string" }, "highresupscaler": { "type": "string" }, "Highres Upscaler": { "type": "string" }, "highres upscaler": { "type": "string" },
"HighresResizeWidth": { "type": "integer" }, "highresresizewidth": { "type": "integer" }, "Highres Resize Width": { "type": "integer" }, "highres resize width": { "type": "integer" },
"HighresResizeHeight": { "type": "integer" }, "highresresizeheight": { "type": "integer" }, "Highres Resize Height": { "type": "integer" }, "highres resize height": { "type": "integer" },
"HighresUpscaleToWidth": { "type": "integer" }, "highresupscaletowidth": { "type": "integer" }, "Highres Upscale To Width": { "type": "integer" }, "highres upscale to width": { "type": "integer" },
"HighresUpscaleToHeight": { "type": "integer" }, "highresupscaletoheight": { "type": "integer" }, "Highres Upscale To Height": { "type": "integer" }, "highres upscale to height": { "type": "integer" },
"RestoreFaces": { "oneOf": [{ "type" : "boolean"}, { "type": "string", "enum": ["GFPGan", "CodeFormer", "true", "false", "gfpgan", "codeformer"] }]},
"Restore Faces": { "oneOf": [{ "type" : "boolean"}, { "type": "string", "enum": ["GFPGan", "CodeFormer", "true", "false", "gfpgan", "codeformer"] }]},
"restorefaces": { "oneOf": [{ "type" : "boolean"}, { "type": "string", "enum": ["GFPGan", "CodeFormer", "true", "false", "gfpgan", "codeformer"] }]},
"restore faces": { "oneOf": [{ "type" : "boolean"}, { "type": "string", "enum": ["GFPGan", "CodeFormer", "true", "false", "gfpgan", "codeformer"] }]}
},
"title": "GridParams"
}
}
}
================================================
FILE: assets/images/put-usable-images-here.txt
================================================
================================================
FILE: assets/jsgif.js
================================================
/**
* This class lets you encode animated GIF files
* Base class : http://www.java2s.com/Code/Java/2D-Graphics-GUI/AnimatedGifEncoder.htm
* @author Kevin Weiner (original Java version - kweiner@fmsware.com)
* @author Thibault Imbert (AS3 version - bytearray.org)
* @author Kevin Kwok (JavaScript version - https://github.com/antimatter15/jsgif)
* @version 0.1 AS3 implementation
*/
GIFEncoder = function() {
for (var i = 0, chr = {}; i < 256; i++)
chr[i] = String.fromCharCode(i);
function ByteArray() {
this.bin = [];
}
ByteArray.prototype.getData = function() {
for (var v = '', l = this.bin.length, i = 0; i < l; i++)
v += chr[this.bin[i]];
return v;
};
ByteArray.prototype.writeByte = function(val) {
this.bin.push(val);
};
ByteArray.prototype.writeUTFBytes = function(string) {
for (var l = string.length, i = 0; i < l; i++)
this.writeByte(string.charCodeAt(i));
};
ByteArray.prototype.writeBytes = function(array, offset, length) {
for (var l = length || array.length, i = offset || 0; i < l; i++)
this.writeByte(array[i]);
};
var exports = {};
var width; // image size
var height;
var transparent = null; // transparent color if given
var transIndex; // transparent index in color table
var repeat = -1; // no repeat
var delay = 0; // frame delay (hundredths)
var started = false; // ready to output frames
var out;
var image; // current frame
var pixels; // BGR byte array from frame
var indexedPixels; // converted frame indexed to palette
var colorDepth; // number of bit planes
var colorTab; // RGB palette
var usedEntry = []; // active palette entries
var palSize = 7; // color table size (bits-1)
var dispose = -1; // disposal code (-1 = use default)
var closeStream = false; // close stream when finished
var firstFrame = true;
var sizeSet = false; // if false, get size from first frame
var sample = 10; // default sample interval for quantizer
var comment = "Generated by jsgif (https://github.com/antimatter15/jsgif/)"; // default comment for generated gif
/**
* Sets the delay time between each frame, or changes it for subsequent frames
* (applies to last frame added)
* int delay time in milliseconds
* @param ms
*/
var setDelay = exports.setDelay = function setDelay(ms) {
delay = Math.round(ms / 10);
};
/**
* Sets the GIF frame disposal code for the last added frame and any
*
* subsequent frames. Default is 0 if no transparent color has been set,
* otherwise 2.
* @param code
* int disposal code.
*/
var setDispose = exports.setDispose = function setDispose(code) {
if (code >= 0) dispose = code;
};
/**
* Sets the number of times the set of GIF frames should be played. Default is
* 1; 0 means play indefinitely. Must be invoked before the first image is
* added.
*
* @param iter
* int number of iterations.
* @return
*/
var setRepeat = exports.setRepeat = function setRepeat(iter) {
if (iter >= 0) repeat = iter;
};
/**
* Sets the transparent color for the last added frame and any subsequent
* frames. Since all colors are subject to modification in the quantization
* process, the color in the final palette for each frame closest to the given
* color becomes the transparent color for that frame. May be set to null to
* indicate no transparent color.
* @param
* Color to be treated as transparent on display.
*/
var setTransparent = exports.setTransparent = function setTransparent(c) {
transparent = c;
};
/**
* Sets the comment for the block comment
* @param
* string to be insterted as comment
*/
var setComment = exports.setComment = function setComment(c) {
comment = c;
};
/**
* The addFrame method takes an incoming BitmapData object to create each frames
* @param
* BitmapData object to be treated as a GIF's frame
*/
var addFrame = exports.addFrame = function addFrame(im, is_imageData) {
if ((im === null) || !started || out === null) {
throw new Error("Please call start method before calling addFrame");
}
var ok = true;
try {
if (!is_imageData) {
image = im.getImageData(0, 0, im.canvas.width, im.canvas.height).data;
if (!sizeSet) setSize(im.canvas.width, im.canvas.height);
} else {
if(im instanceof ImageData) {
image = im.data;
if(!sizeset || width!=im.width || height!=im.height) {
setSize(im.width,im.height);
} else {
}
} else if(im instanceof Uint8ClampedArray) {
if(im.length==(width*height*4)) {
image=im;
} else {
console.log("Please set the correct size: ImageData length mismatch");
ok=false;
}
} else {
console.log("Please provide correct input");
ok=false;
}
}
getImagePixels(); // convert to correct format if necessary
analyzePixels(); // build color table & map pixels
if (firstFrame) {
writeLSD(); // logical screen descriptior
writePalette(); // global color table
if (repeat >= 0) {
// use NS app extension to indicate reps
writeNetscapeExt();
}
}
writeGraphicCtrlExt(); // write graphic control extension
if (comment !== '') {
writeCommentExt(); // write comment extension
}
writeImageDesc(); // image descriptor
if (!firstFrame) writePalette(); // local color table
writePixels(); // encode and write pixel data
firstFrame = false;
} catch (e) {
ok = false;
}
return ok;
};
/**
* @description: Downloads the encoded gif with the given name
* No need of any conversion from the stream data (out) to base64
* Solves the issue of large file sizes when there are more frames
* and does not involve in creation of any temporary data in the process
* so no wastage of memory, and speeds up the process of downloading
* to just calling this function.
* @parameter {String} filename filename used for downloading the gif
*/
var download = exports.download = function download(filename) {
if(out===null || closeStream==false) {
console.log("Please call start method and add frames and call finish method before calling download");
} else {
filename= filename !== undefined ? ( filename.endsWith(".gif")? filename: filename+".gif" ): "download.gif";
var templink = document.createElement("a");
templink.download=filename;
templink.href= URL.createObjectURL(new Blob([new Uint8Array(out.bin)], {type : "image/gif" } ));
templink.click();
}
}
/**
* Adds final trailer to the GIF stream, if you don't call the finish method
* the GIF stream will not be valid.
*/
var finish = exports.finish = function finish() {
if (!started) return false;
var ok = true;
started = false;
try {
out.writeByte(0x3b); // gif trailer
closeStream=true;
} catch (e) {
ok = false;
}
return ok;
};
/**
* Resets some members so that a new stream can be started.
* This method is actually called by the start method
*/
var reset = function reset() {
// reset for subsequent use
transIndex = 0;
image = null;
pixels = null;
indexedPixels = null;
colorTab = null;
closeStream = false;
firstFrame = true;
};
/**
* * Sets frame rate in frames per second. Equivalent to
* setDelay(1000/fps).
* @param fps
* float frame rate (frames per second)
*/
var setFrameRate = exports.setFrameRate = function setFrameRate(fps) {
if (fps != 0xf) delay = Math.round(100 / fps);
};
/**
* Sets quality of color quantization (conversion of images to the maximum 256
* colors allowed by the GIF specification). Lower values (minimum = 1)
* produce better colors, but slow processing significantly. 10 is the
* default, and produces good color mapping at reasonable speeds. Values
* greater than 20 do not yield significant improvements in speed.
* @param quality
* int greater than 0.
* @return
*/
var setQuality = exports.setQuality = function setQuality(quality) {
if (quality < 1) quality = 1;
sample = quality;
};
/**
* Sets the GIF frame size. The default size is the size of the first frame
* added if this method is not invoked.
* @param w
* int frame width.
* @param h
* int frame width.
*/
var setSize = exports.setSize = function setSize(w, h) {
if (started && !firstFrame) return;
width = w;
height = h;
if (width < 1) width = 320;
if (height < 1) height = 240;
sizeSet = true;
};
/**
* Initiates GIF file creation on the given stream.
* @param os
* OutputStream on which GIF images are written.
* @return false if initial write failed.
*/
var start = exports.start = function start() {
reset();
var ok = true;
closeStream = false;
out = new ByteArray();
try {
out.writeUTFBytes("GIF89a"); // header
} catch (e) {
ok = false;
}
return started = ok;
};
var cont = exports.cont = function cont() {
reset();
var ok = true;
closeStream = false;
out = new ByteArray();
return started = ok;
};
/**
* Analyzes image colors and creates color map.
*/
var analyzePixels = function analyzePixels() {
var len = pixels.length;
var nPix = len / 3;
indexedPixels = [];
var nq = new NeuQuant(pixels, len, sample);
// initialize quantizer
colorTab = nq.process(); // create reduced palette
// map image pixels to new palette
var k = 0;
for (var j = 0; j < nPix; j++) {
var index = nq.map(pixels[k++] & 0xff, pixels[k++] & 0xff, pixels[k++] & 0xff);
usedEntry[index] = true;
indexedPixels[j] = index;
}
pixels = null;
colorDepth = 8;
palSize = 7;
// get closest match to transparent color if specified
if (transparent !== null) {
transIndex = findClosest(transparent);
}
};
/**
* Returns index of palette color closest to c
*/
var findClosest = function findClosest(c) {
if (colorTab === null) return -1;
var r = (c & 0xFF0000) >> 16;
var g = (c & 0x00FF00) >> 8;
var b = (c & 0x0000FF);
var minpos = 0;
var dmin = 256 * 256 * 256;
var len = colorTab.length;
for (var i = 0; i < len;) {
var dr = r - (colorTab[i++] & 0xff);
var dg = g - (colorTab[i++] & 0xff);
var db = b - (colorTab[i] & 0xff);
var d = dr * dr + dg * dg + db * db;
var index = i / 3;
if (usedEntry[index] && (d < dmin)) {
dmin = d;
minpos = index;
}
i++;
}
return minpos;
};
/**
* Extracts image pixels into byte array "pixels
*/
var getImagePixels = function getImagePixels() {
var w = width;
var h = height;
pixels = [];
var data = image;
var count = 0;
for (var i = 0; i < h; i++) {
for (var j = 0; j < w; j++) {
var b = (i * w * 4) + j * 4;
pixels[count++] = data[b];
pixels[count++] = data[b + 1];
pixels[count++] = data[b + 2];
}
}
};
/**
* Writes Graphic Control Extension
*/
var writeGraphicCtrlExt = function writeGraphicCtrlExt() {
out.writeByte(0x21); // extension introducer
out.writeByte(0xf9); // GCE label
out.writeByte(4); // data block size
var transp;
var disp;
if (transparent === null) {
transp = 0;
disp = 0; // dispose = no action
} else {
transp = 1;
disp = 2; // force clear if using transparent color
}
if (dispose >= 0) {
disp = dispose & 7; // user override
}
disp <<= 2;
// packed fields
out.writeByte(0 | // 1:3 reserved
disp | // 4:6 disposal
0 | // 7 user input - 0 = none
transp); // 8 transparency flag
WriteShort(delay); // delay x 1/100 sec
out.writeByte(transIndex); // transparent color index
out.writeByte(0); // block terminator
};
/**
* Writes Comment Extention
*/
var writeCommentExt = function writeCommentExt() {
out.writeByte(0x21); // extension introducer
out.writeByte(0xfe); // comment label
out.writeByte(comment.length); // Block Size (s)
out.writeUTFBytes(comment);
out.writeByte(0); // block terminator
};
/**
* Writes Image Descriptor
*/
var writeImageDesc = function writeImageDesc() {
out.writeByte(0x2c); // image separator
WriteShort(0); // image position x,y = 0,0
WriteShort(0);
WriteShort(width); // image size
WriteShort(height);
// packed fields
if (firstFrame) {
// no LCT - GCT is used for first (or only) frame
out.writeByte(0);
} else {
// specify normal LCT
out.writeByte(0x80 | // 1 local color table 1=yes
0 | // 2 interlace - 0=no
0 | // 3 sorted - 0=no
0 | // 4-5 reserved
palSize); // 6-8 size of color table
}
};
/**
* Writes Logical Screen Descriptor
*/
var writeLSD = function writeLSD() {
// logical screen size
WriteShort(width);
WriteShort(height);
// packed fields
out.writeByte((0x80 | // 1 : global color table flag = 1 (gct used)
0x70 | // 2-4 : color resolution = 7
0x00 | // 5 : gct sort flag = 0
palSize)); // 6-8 : gct size
out.writeByte(0); // background color index
out.writeByte(0); // pixel aspect ratio - assume 1:1
};
/**
* Writes Netscape application extension to define repeat count.
*/
var writeNetscapeExt = function writeNetscapeExt() {
out.writeByte(0x21); // extension introducer
out.writeByte(0xff); // app extension label
out.writeByte(11); // block size
out.writeUTFBytes("NETSCAPE" + "2.0"); // app id + auth code
out.writeByte(3); // sub-block size
out.writeByte(1); // loop sub-block id
WriteShort(repeat); // loop count (extra iterations, 0=repeat forever)
out.writeByte(0); // block terminator
};
/**
* Writes color table
*/
var writePalette = function writePalette() {
out.writeBytes(colorTab);
var n = (3 * 256) - colorTab.length;
for (var i = 0; i < n; i++) out.writeByte(0);
};
var WriteShort = function WriteShort(pValue) {
out.writeByte(pValue & 0xFF);
out.writeByte((pValue >> 8) & 0xFF);
};
/**
* Encodes and writes pixel data
*/
var writePixels = function writePixels() {
var myencoder = new LZWEncoder(width, height, indexedPixels, colorDepth);
myencoder.encode(out);
};
/**
* Retrieves the GIF stream
*/
var stream = exports.stream = function stream() {
return out;
};
var setProperties = exports.setProperties = function setProperties(has_start, is_first) {
started = has_start;
firstFrame = is_first;
};
return exports;
};
/**
* This class handles LZW encoding
* Adapted from Jef Poskanzer's Java port by way of J. M. G. Elliott.
* @author Kevin Weiner (original Java version - kweiner@fmsware.com)
* @author Thibault Imbert (AS3 version - bytearray.org)
* @author Kevin Kwok (JavaScript version - https://github.com/antimatter15/jsgif)
* @version 0.1 AS3 implementation
*/
LZWEncoder = function() {
var exports = {};
var EOF = -1;
var imgW;
var imgH;
var pixAry;
var initCodeSize;
var remaining;
var curPixel;
// GIFCOMPR.C - GIF Image compression routines
// Lempel-Ziv compression based on 'compress'. GIF modifications by
// David Rowley (mgardi@watdcsu.waterloo.edu)
// General DEFINEs
var BITS = 12;
var HSIZE = 5003; // 80% occupancy
// GIF Image compression - modified 'compress'
// Based on: compress.c - File compression ala IEEE Computer, June 1984.
// By Authors: Spencer W. Thomas (decvax!harpo!utah-cs!utah-gr!thomas)
// Jim McKie (decvax!mcvax!jim)
// Steve Davies (decvax!vax135!petsd!peora!srd)
// Ken Turkowski (decvax!decwrl!turtlevax!ken)
// James A. Woods (decvax!ihnp4!ames!jaw)
// Joe Orost (decvax!vax135!petsd!joe)
var n_bits; // number of bits/code
var maxbits = BITS; // user settable max # bits/code
var maxcode; // maximum code, given n_bits
var maxmaxcode = 1 << BITS; // should NEVER generate this code
var htab = [];
var codetab = [];
var hsize = HSIZE; // for dynamic table sizing
var free_ent = 0; // first unused entry
// block compression parameters -- after all codes are used up,
// and compression rate changes, start over.
var clear_flg = false;
// Algorithm: use open addressing double hashing (no chaining) on the
// prefix code / next character combination. We do a variant of Knuth's
// algorithm D (vol. 3, sec. 6.4) along with G. Knott's relatively-prime
// secondary probe. Here, the modular division first probe is gives way
// to a faster exclusive-or manipulation. Also do block compression with
// an adaptive reset, whereby the code table is cleared when the compression
// ratio decreases, but after the table fills. The variable-length output
// codes are re-sized at this point, and a special CLEAR code is generated
// for the decompressor. Late addition: construct the table according to
// file size for noticeable speed improvement on small files. Please direct
// questions about this implementation to ames!jaw.
var g_init_bits;
var ClearCode;
var EOFCode;
// output
// Output the given code.
// Inputs:
// code: A n_bits-bit integer. If == -1, then EOF. This assumes
// that n_bits =< wordsize - 1.
// Outputs:
// Outputs code to the file.
// Assumptions:
// Chars are 8 bits long.
// Algorithm:
// Maintain a BITS character long buffer (so that 8 codes will
// fit in it exactly). Use the VAX insv instruction to insert each
// code in turn. When the buffer fills up empty it and start over.
var cur_accum = 0;
var cur_bits = 0;
var masks = [0x0000, 0x0001, 0x0003, 0x0007, 0x000F, 0x001F, 0x003F, 0x007F, 0x00FF, 0x01FF, 0x03FF, 0x07FF, 0x0FFF, 0x1FFF, 0x3FFF, 0x7FFF, 0xFFFF];
// Number of characters so far in this 'packet'
var a_count;
// Define the storage for the packet accumulator
var accum = [];
var LZWEncoder = exports.LZWEncoder = function LZWEncoder(width, height, pixels, color_depth) {
imgW = width;
imgH = height;
pixAry = pixels;
initCodeSize = Math.max(2, color_depth);
};
// Add a character to the end of the current packet, and if it is 254
// characters, flush the packet to disk.
var char_out = function char_out(c, outs) {
accum[a_count++] = c;
if (a_count >= 254) flush_char(outs);
};
// Clear out the hash table
// table clear for block compress
var cl_block = function cl_block(outs) {
cl_hash(hsize);
free_ent = ClearCode + 2;
clear_flg = true;
output(ClearCode, outs);
};
// reset code table
var cl_hash = function cl_hash(hsize) {
for (var i = 0; i < hsize; ++i) htab[i] = -1;
};
var compress = exports.compress = function compress(init_bits, outs) {
var fcode;
var i; /* = 0 */
var c;
var ent;
var disp;
var hsize_reg;
var hshift;
// Set up the globals: g_init_bits - initial number of bits
g_init_bits = init_bits;
// Set up the necessary values
clear_flg = false;
n_bits = g_init_bits;
maxcode = MAXCODE(n_bits);
ClearCode = 1 << (init_bits - 1);
EOFCode = ClearCode + 1;
free_ent = ClearCode + 2;
a_count = 0; // clear packet
ent = nextPixel();
hshift = 0;
for (fcode = hsize; fcode < 65536; fcode *= 2)
++hshift;
hshift = 8 - hshift; // set hash code range bound
hsize_reg = hsize;
cl_hash(hsize_reg); // clear hash table
output(ClearCode, outs);
outer_loop: while ((c = nextPixel()) != EOF) {
fcode = (c << maxbits) + ent;
i = (c << hshift) ^ ent; // xor hashing
if (htab[i] == fcode) {
ent = codetab[i];
continue;
}
else if (htab[i] >= 0) { // non-empty slot
disp = hsize_reg - i; // secondary hash (after G. Knott)
if (i === 0) disp = 1;
do {
if ((i -= disp) < 0)
i += hsize_reg;
if (htab[i] == fcode) {
ent = codetab[i];
continue outer_loop;
}
} while (htab[i] >= 0);
}
output(ent, outs);
ent = c;
if (free_ent < maxmaxcode) {
codetab[i] = free_ent++; // code -> hashtable
htab[i] = fcode;
}
else cl_block(outs);
}
// Put out the final code.
output(ent, outs);
output(EOFCode, outs);
};
// ----------------------------------------------------------------------------
var encode = exports.encode = function encode(os) {
os.writeByte(initCodeSize); // write "initial code size" byte
remaining = imgW * imgH; // reset navigation variables
curPixel = 0;
compress(initCodeSize + 1, os); // compress and write the pixel data
os.writeByte(0); // write block terminator
};
// Flush the packet to disk, and reset the accumulator
var flush_char = function flush_char(outs) {
if (a_count > 0) {
outs.writeByte(a_count);
outs.writeBytes(accum, 0, a_count);
a_count = 0;
}
};
var MAXCODE = function MAXCODE(n_bits) {
return (1 << n_bits) - 1;
};
// ----------------------------------------------------------------------------
// Return the next pixel from the image
// ----------------------------------------------------------------------------
var nextPixel = function nextPixel() {
if (remaining === 0) return EOF;
--remaining;
var pix = pixAry[curPixel++];
return pix & 0xff;
};
var output = function output(code, outs) {
cur_accum &= masks[cur_bits];
if (cur_bits > 0) cur_accum |= (code << cur_bits);
else cur_accum = code;
cur_bits += n_bits;
while (cur_bits >= 8) {
char_out((cur_accum & 0xff), outs);
cur_accum >>= 8;
cur_bits -= 8;
}
// If the next entry is going to be too big for the code size,
// then increase it, if possible.
if (free_ent > maxcode || clear_flg) {
if (clear_flg) {
maxcode = MAXCODE(n_bits = g_init_bits);
clear_flg = false;
} else {
++n_bits;
if (n_bits == maxbits) maxcode = maxmaxcode;
else maxcode = MAXCODE(n_bits);
}
}
if (code == EOFCode) {
// At EOF, write the rest of the buffer.
while (cur_bits > 0) {
char_out((cur_accum & 0xff), outs);
cur_accum >>= 8;
cur_bits -= 8;
}
flush_char(outs);
}
};
LZWEncoder.apply(this, arguments);
return exports;
};
/*
* NeuQuant Neural-Net Quantization Algorithm
* ------------------------------------------
*
* Copyright (c) 1994 Anthony Dekker
*
* NEUQUANT Neural-Net quantization algorithm by Anthony Dekker, 1994. See
* "Kohonen neural networks for optimal colour quantization" in "Network:
* Computation in Neural Systems" Vol. 5 (1994) pp 351-367. for a discussion of
* the algorithm.
*
* Any party obtaining a copy of these files from the author, directly or
* indirectly, is granted, free of charge, a full and unrestricted irrevocable,
* world-wide, paid up, royalty-free, nonexclusive right and license to deal in
* this software and documentation files (the "Software"), including without
* limitation the rights to use, copy, modify, merge, publish, distribute,
* sublicense, and/or sell copies of the Software, and to permit persons who
* receive copies from any such party to do so, with the only requirement being
* that this copyright notice remain intact.
*/
/*
* This class handles Neural-Net quantization algorithm
* @author Kevin Weiner (original Java version - kweiner@fmsware.com)
* @author Thibault Imbert (AS3 version - bytearray.org)
* @author Kevin Kwok (JavaScript version - https://github.com/antimatter15/jsgif)
* @version 0.1 AS3 implementation
*/
NeuQuant = function() {
var exports = {};
var netsize = 256; /* number of colours used */
/* four primes near 500 - assume no image has a length so large */
/* that it is divisible by all four primes */
var prime1 = 499;
var prime2 = 491;
var prime3 = 487;
var prime4 = 503;
var minpicturebytes = (3 * prime4); /* minimum size for input image */
/*
* Program Skeleton ---------------- [select samplefac in range 1..30] [read
* image from input file] pic = (unsigned char*) malloc(3*width*height);
* initnet(pic,3*width*height,samplefac); learn(); unbiasnet(); [write output
* image header, using writecolourmap(f)] inxbuild(); write output image using
* inxsearch(b,g,r)
*/
/*
* Network Definitions -------------------
*/
var maxnetpos = (netsize - 1);
var netbiasshift = 4; /* bias for colour values */
var ncycles = 100; /* no. of learning cycles */
/* defs for freq and bias */
var intbiasshift = 16; /* bias for fractions */
var intbias = (1 << intbiasshift);
var gammashift = 10; /* gamma = 1024 */
var gamma = (1 << gammashift);
var betashift = 10;
var beta = (intbias >> betashift); /* beta = 1/1024 */
var betagamma = (intbias << (gammashift - betashift));
/* defs for decreasing radius factor */
var initrad = (netsize >> 3); /* for 256 cols, radius starts */
var radiusbiasshift = 6; /* at 32.0 biased by 6 bits */
var radiusbias = (1 << radiusbiasshift);
var initradius = (initrad * radiusbias); /* and decreases by a */
var radiusdec = 30; /* factor of 1/30 each cycle */
/* defs for decreasing alpha factor */
var alphabiasshift = 10; /* alpha starts at 1.0 */
var initalpha = (1 << alphabiasshift);
var alphadec; /* biased by 10 bits */
/* radbias and alpharadbias used for radpower calculation */
var radbiasshift = 8;
var radbias = (1 << radbiasshift);
var alpharadbshift = (alphabiasshift + radbiasshift);
var alpharadbias = (1 << alpharadbshift);
/*
* Types and Global Variables --------------------------
*/
var thepicture; /* the input image itself */
var lengthcount; /* lengthcount = H*W*3 */
var samplefac; /* sampling factor 1..30 */
// typedef int pixel[4]; /* BGRc */
var network; /* the network itself - [netsize][4] */
var netindex = [];
/* for network lookup - really 256 */
var bias = [];
/* bias and freq arrays for learning */
var freq = [];
var radpower = [];
var NeuQuant = exports.NeuQuant = function NeuQuant(thepic, len, sample) {
var i;
var p;
thepicture = thepic;
lengthcount = len;
samplefac = sample;
network = new Array(netsize);
for (i = 0; i < netsize; i++) {
network[i] = new Array(4);
p = network[i];
p[0] = p[1] = p[2] = (i << (netbiasshift + 8)) / netsize;
freq[i] = intbias / netsize; /* 1/netsize */
bias[i] = 0;
}
};
var colorMap = function colorMap() {
var map = [];
var index = new Array(netsize);
for (var i = 0; i < netsize; i++)
index[network[i][3]] = i;
var k = 0;
for (var l = 0; l < netsize; l++) {
var j = index[l];
map[k++] = (network[j][0]);
map[k++] = (network[j][1]);
map[k++] = (network[j][2]);
}
return map;
};
/*
* Insertion sort of network and building of netindex[0..255] (to do after
* unbias)
* -------------------------------------------------------------------------------
*/
var inxbuild = function inxbuild() {
var i;
var j;
var smallpos;
var smallval;
var p;
var q;
var previouscol;
var startpos;
previouscol = 0;
startpos = 0;
for (i = 0; i < netsize; i++) {
p = network[i];
smallpos = i;
smallval = p[1]; /* index on g */
/* find smallest in i..netsize-1 */
for (j = i + 1; j < netsize; j++) {
q = network[j];
if (q[1] < smallval) { /* index on g */
smallpos = j;
smallval = q[1]; /* index on g */
}
}
q = network[smallpos];
/* swap p (i) and q (smallpos) entries */
if (i != smallpos) {
j = q[0];
q[0] = p[0];
p[0] = j;
j = q[1];
q[1] = p[1];
p[1] = j;
j = q[2];
q[2] = p[2];
p[2] = j;
j = q[3];
q[3] = p[3];
p[3] = j;
}
/* smallval entry is now in position i */
if (smallval != previouscol) {
netindex[previouscol] = (startpos + i) >> 1;
for (j = previouscol + 1; j < smallval; j++) netindex[j] = i;
previouscol = smallval;
startpos = i;
}
}
netindex[previouscol] = (startpos + maxnetpos) >> 1;
for (j = previouscol + 1; j < 256; j++) netindex[j] = maxnetpos; /* really 256 */
};
/*
* Main Learning Loop ------------------
*/
var learn = function learn() {
var i;
var j;
var b;
var g;
var r;
var radius;
var rad;
var alpha;
var step;
var delta;
var samplepixels;
var p;
var pix;
var lim;
if (lengthcount < minpicturebytes) samplefac = 1;
alphadec = 30 + ((samplefac - 1) / 3);
p = thepicture;
pix = 0;
lim = lengthcount;
samplepixels = lengthcount / (3 * samplefac);
delta = (samplepixels / ncycles) | 0;
alpha = initalpha;
radius = initradius;
rad = radius >> radiusbiasshift;
if (rad <= 1) rad = 0;
for (i = 0; i < rad; i++) radpower[i] = alpha * (((rad * rad - i * i) * radbias) / (rad * rad));
if (lengthcount < minpicturebytes) step = 3;
else if ((lengthcount % prime1) !== 0) step = 3 * prime1;
else {
if ((lengthcount % prime2) !== 0) step = 3 * prime2;
else {
if ((lengthcount % prime3) !== 0) step = 3 * prime3;
else step = 3 * prime4;
}
}
i = 0;
while (i < samplepixels) {
b = (p[pix + 0] & 0xff) << netbiasshift;
g = (p[pix + 1] & 0xff) << netbiasshift;
r = (p[pix + 2] & 0xff) << netbiasshift;
j = contest(b, g, r);
altersingle(alpha, j, b, g, r);
if (rad !== 0) alterneigh(rad, j, b, g, r); /* alter neighbours */
pix += step;
if (pix >= lim) pix -= lengthcount;
i++;
if (delta === 0) delta = 1;
if (i % delta === 0) {
alpha -= alpha / alphadec;
radius -= radius / radiusdec;
rad = radius >> radiusbiasshift;
if (rad <= 1) rad = 0;
for (j = 0; j < rad; j++) radpower[j] = alpha * (((rad * rad - j * j) * radbias) / (rad * rad));
}
}
};
/*
** Search for BGR values 0..255 (after net is unbiased) and return colour
* index
* ----------------------------------------------------------------------------
*/
var map = exports.map = function map(b, g, r) {
var i;
var j;
var dist;
var a;
var bestd;
var p;
var best;
bestd = 1000; /* biggest possible dist is 256*3 */
best = -1;
i = netindex[g]; /* index on g */
j = i - 1; /* start at netindex[g] and work outwards */
while ((i < netsize) || (j >= 0)) {
if (i < netsize) {
p = network[i];
dist = p[1] - g; /* inx key */
if (dist >= bestd) i = netsize; /* stop iter */
else {
i++;
if (dist < 0) dist = -dist;
a = p[0] - b;
if (a < 0) a = -a;
dist += a;
if (dist < bestd) {
a = p[2] - r;
if (a < 0) a = -a;
dist += a;
if (dist < bestd) {
bestd = dist;
best = p[3];
}
}
}
}
if (j >= 0) {
p = network[j];
dist = g - p[1]; /* inx key - reverse dif */
if (dist >= bestd) j = -1; /* stop iter */
else {
j--;
if (dist < 0) dist = -dist;
a = p[0] - b;
if (a < 0) a = -a;
dist += a;
if (dist < bestd) {
a = p[2] - r;
if (a < 0) a = -a;
dist += a;
if (dist < bestd) {
bestd = dist;
best = p[3];
}
}
}
}
}
return (best);
};
var process = exports.process = function process() {
learn();
unbiasnet();
inxbuild();
return colorMap();
};
/*
* Unbias network to give byte values 0..255 and record position i to prepare
* for sort
* -----------------------------------------------------------------------------------
*/
var unbiasnet = function unbiasnet() {
var i;
var j;
for (i = 0; i < netsize; i++) {
network[i][0] >>= netbiasshift;
network[i][1] >>= netbiasshift;
network[i][2] >>= netbiasshift;
network[i][3] = i; /* record colour no */
}
};
/*
* Move adjacent neurons by precomputed alpha*(1-((i-j)^2/[r]^2)) in
* radpower[|i-j|]
* ---------------------------------------------------------------------------------
*/
var alterneigh = function alterneigh(rad, i, b, g, r) {
var j;
var k;
var lo;
var hi;
var a;
var m;
var p;
lo = i - rad;
if (lo < -1) lo = -1;
hi = i + rad;
if (hi > netsize) hi = netsize;
j = i + 1;
k = i - 1;
m = 1;
while ((j < hi) || (k > lo)) {
a = radpower[m++];
if (j < hi) {
p = network[j++];
try {
p[0] -= (a * (p[0] - b)) / alpharadbias;
p[1] -= (a * (p[1] - g)) / alpharadbias;
p[2] -= (a * (p[2] - r)) / alpharadbias;
} catch (e) {} // prevents 1.3 miscompilation
}
if (k > lo) {
p = network[k--];
try {
p[0] -= (a * (p[0] - b)) / alpharadbias;
p[1] -= (a * (p[1] - g)) / alpharadbias;
p[2] -= (a * (p[2] - r)) / alpharadbias;
} catch (e) {}
}
}
};
/*
* Move neuron i towards biased (b,g,r) by factor alpha
* ----------------------------------------------------
*/
var altersingle = function altersingle(alpha, i, b, g, r) {
/* alter hit neuron */
var n = network[i];
n[0] -= (alpha * (n[0] - b)) / initalpha;
n[1] -= (alpha * (n[1] - g)) / initalpha;
n[2] -= (alpha * (n[2] - r)) / initalpha;
};
/*
* Search for biased BGR values ----------------------------
*/
var contest = function contest(b, g, r) {
/* finds closest neuron (min dist) and updates freq */
/* finds best neuron (min dist-bias) and returns position */
/* for frequently chosen neurons, freq[i] is high and bias[i] is negative */
/* bias[i] = gamma*((1/netsize)-freq[i]) */
var i;
var dist;
var a;
var biasdist;
var betafreq;
var bestpos;
var bestbiaspos;
var bestd;
var bestbiasd;
var n;
bestd = ~ (1 << 31);
bestbiasd = bestd;
bestpos = -1;
bestbiaspos = bestpos;
for (i = 0; i < netsize; i++) {
n = network[i];
dist = n[0] - b;
if (dist < 0) dist = -dist;
a = n[1] - g;
if (a < 0) a = -a;
dist += a;
a = n[2] - r;
if (a < 0) a = -a;
dist += a;
if (dist < bestd) {
bestd = dist;
bestpos = i;
}
biasdist = dist - ((bias[i]) >> (intbiasshift - netbiasshift));
if (biasdist < bestbiasd) {
bestbiasd = biasdist;
bestbiaspos = i;
}
betafreq = (freq[i] >> betashift);
freq[i] -= betafreq;
bias[i] += (betafreq << gammashift);
}
freq[bestpos] += beta;
bias[bestpos] -= betagamma;
return (bestbiaspos);
};
NeuQuant.apply(this, arguments);
return exports;
};
function encode64(input) {
var output = "", i = 0, l = input.length,
key = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=",
chr1, chr2, chr3, enc1, enc2, enc3, enc4;
while (i < l) {
chr1 = input.charCodeAt(i++);
chr2 = input.charCodeAt(i++);
chr3 = input.charCodeAt(i++);
enc1 = chr1 >> 2;
enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
enc4 = chr3 & 63;
if (isNaN(chr2)) enc3 = enc4 = 64;
else if (isNaN(chr3)) enc4 = 64;
output = output + key.charAt(enc1) + key.charAt(enc2) + key.charAt(enc3) + key.charAt(enc4);
}
return output;
}
================================================
FILE: assets/page.html
================================================
{TITLE}
{TITLE}
{DESCRIPTION}
{ADVANCED_SETTINGS}
{CONTENT}
Score Display:
Here's your image! To share it, just use right click -> Copy image or Save Image As
Here's your image!
To share it, just use right click -> Copy image or Save Image As.
================================================
FILE: assets/proc.js
================================================
/*
* This file is part of Infinity Grid Generator, view the README.md at https://github.com/mcmonkeyprojects/sd-infinity-grid-generator-script for more information.
*/
let suppressUpdate = true;
let file_extensions_alt = {};
function loadData() {
let rawHash = window.location.hash;
document.getElementById('x_' + rawData.axes[0].id).click();
document.getElementById('x2_none').click();
document.getElementById('y2_none').click();
let makegif_axis = document.getElementById('makegif_axis');
// rawData.ext/title/description
for (var axis of rawData.axes) {
// axis.id/title/description
for (var val of axis.values) {
// val.key/title/description/show
let clicktab = getNavValTab(axis.id, val.key);
clicktab.addEventListener('click', fillTable);
if (!val.show) {
setShowVal(axis.id, val.key, false);
}
}
for (var prefix of ['x_', 'y_', 'x2_', 'y2_']) {
document.getElementById(prefix + axis.id).addEventListener('click', fillTable);
}
for (var label of ['x2_none', 'y2_none']) {
document.getElementById(label).addEventListener('click', fillTable);
}
makegif_axis.appendChild(new Option(axis.title, axis.id));
}
console.log(`Loaded data for '${rawData.title}'`);
document.getElementById('autoScaleImages').addEventListener('change', updateScaling);
document.getElementById('stickyNavigation').addEventListener('change', toggleTopSticky);
document.getElementById('stickyLabels').addEventListener('change', toggleLabelSticky);
document.getElementById('toggle_nav_button').addEventListener('click', updateTitleSticky);
document.getElementById('toggle_adv_button').addEventListener('click', updateTitleSticky);
document.getElementById('showDescriptions').checked = rawData.defaults.show_descriptions;
document.getElementById('autoScaleImages').checked = rawData.defaults.autoscale;
document.getElementById('stickyNavigation').checked = rawData.defaults.sticky;
document.getElementById('stickyLabels').checked = rawData.defaults.sticky_labels;
document.getElementById('score_display').addEventListener('click', fillTable);
document.getElementById('score_setting').style.display = typeof getScoreFor == 'undefined' ? 'none' : 'inline-block';
updateStylesToMatchInputs();
for (var axis of ['x', 'y', 'x2', 'y2']) {
if (rawData.defaults[axis] != '') {
document.getElementById(axis + '_' + rawData.defaults[axis]).click();
}
}
applyHash(rawHash);
suppressUpdate = false;
fillTable();
startAutoScroll();
if (rawData.will_run) {
setTimeout(checkForUpdates, 5000);
}
}
function getExtension(filePath) {
if (filePath in file_extensions_alt) {
return file_extensions_alt[filePath];
}
return rawData.ext;
}
/** External callable. */
function fix_video(path) {
let ext = getExtension(path);
let matches = document.querySelectorAll(`img[data-img_path="${path}"]`);
for (let match of matches) {
if (ext != 'webm' && ext != 'mp4') {
if (!match.src.endsWith(ext)) {
match.src = `${path}.${ext}`;
}
continue;
}
if (match.tagName == 'VIDEO') {
continue;
}
let video = document.createElement('video');
video.loop = true;
video.muted = true;
video.autoplay = true;
video.classList = match.classList;
video.dataset.img_path = match.dataset.img_path;
video.onclick = "doPopupFor(this)";
video.onerror = "setImgPlaceholder(this)";
let source = document.createElement('source');
source.src = `${path}.${ext}`;
source.type = `video/${ext}`;
video.appendChild(source);
match.parentElement.replaceChild(video, match);
}
}
function updateStylesToMatchInputs() {
toggleTopSticky();
toggleLabelSticky();
}
function getAxisById(id) {
return rawData.axes.find(axis => axis.id == id);
}
function getNextAxis(axes, startId) {
var next = false;
for (var subAxis of axes) {
if (subAxis.id == startId) {
next = true;
}
else if (next) {
return subAxis;
}
}
return null;
}
function getSelectedValKey(axis) {
for (var subVal of axis.values) {
if (window.getComputedStyle(document.getElementById('tab_' + axis.id + '__' + subVal.key)).display != 'none') {
return subVal.path;
}
}
return null;
}
var popoverLastImg = null;
function clickRowImage(rows, x, y) {
$('#image_info_modal').modal('hide');
var columns = rows[y].getElementsByTagName('td');
columns[x].getElementsByTagName('img')[0].click();
}
window.addEventListener('keydown', function(kbevent) {
if ($('#image_info_modal').is(':visible')) {
if (kbevent.key == 'Escape') {
$('#image_info_modal').modal('toggle');
kbevent.preventDefault();
kbevent.stopPropagation();
return false;
}
var tableElem = document.getElementById('image_table');
var rows = tableElem.getElementsByTagName('tr');
var matchedRow = null;
var x = 0, y = 0;
for (var row of rows) {
var columns = row.getElementsByTagName('td');
for (var column of columns) {
var images = column.getElementsByTagName('img');
if (images.length == 1 && images[0] == popoverLastImg) {
matchedRow = row;
break;
}
x++;
}
if (matchedRow != null) {
break;
}
x = 0;
y++;
}
if (matchedRow == null) {
return;
}
if (kbevent.key == 'ArrowLeft') {
if (x > 1) {
x--;
clickRowImage(rows, x, y);
}
}
else if (kbevent.key == 'ArrowRight') {
x++;
var columns = matchedRow.getElementsByTagName('td');
if (columns.length > x) {
clickRowImage(rows, x, y);
}
}
else if (kbevent.key == 'ArrowUp') {
if (y > 1) {
y--;
clickRowImage(rows, x, y);
}
}
else if (kbevent.key == 'ArrowDown') {
y++;
if (rows.length > y) {
clickRowImage(rows, x, y);
}
}
else {
return;
}
kbevent.preventDefault();
kbevent.stopPropagation();
return false;
}
var elem = document.activeElement;
if (!elem.id.startsWith('clicktab_')) {
return;
}
var axisId = elem.id.substring('clicktab_'.length);
var splitIndex = axisId.lastIndexOf('__');
axisId = axisId.substring(0, splitIndex);
var axis = getAxisById(axisId);
if (kbevent.key == 'ArrowLeft') {
var tabPage = document.getElementById('tablist_' + axis.id);
var tabs = tabPage.getElementsByClassName('nav-link');
var newTab = clickTabAfterActiveTab(Array.from(tabs).reverse());
newTab.focus({ preventScroll: true });
}
else if (kbevent.key == 'ArrowRight') {
var tabPage = document.getElementById('tablist_' + axis.id);
var tabs = tabPage.getElementsByClassName('nav-link');
var newTab = clickTabAfterActiveTab(tabs);
newTab.focus({ preventScroll: true });
}
else if (kbevent.key == 'ArrowUp') {
var next = getNextAxis(Array.from(rawData.axes).reverse(), axisId);
if (next != null) {
var selectedKey = getSelectedValKey(next);
var swapToTab = getNavValTab(next.id, selectedKey);
swapToTab.focus({ preventScroll: true });
}
}
else if (kbevent.key == 'ArrowDown') {
var next = getNextAxis(rawData.axes, axisId);
if (next != null) {
var selectedKey = getSelectedValKey(next);
var swapToTab = getNavValTab(next.id, selectedKey);
swapToTab.focus({ preventScroll: true });
}
}
else {
return;
}
kbevent.preventDefault();
kbevent.stopPropagation();
return false;
}, true);
function escapeHtml(text) {
if (typeof text != 'string') {
return text;
}
return text.replaceAll('&', '&').replaceAll('<', '<').replaceAll('>', '>').replaceAll('"', '"').replaceAll("'", ''');
}
function unescapeHtml(text) {
return text.replaceAll('<', '<').replaceAll('>', '>').replaceAll('"', '"').replaceAll(''', "'").replaceAll('&', '&');
}
function canShowVal(axis, val) {
return document.getElementById(`showval_${axis}__${val}`).checked;
}
function setShowVal(axis, val, show) {
document.getElementById(`showval_${axis}__${val}`).checked = show;
getNavValTab(axis, val).classList.toggle('tab_hidden', !show);
}
function getNavValTab(axis, val) {
return document.getElementById(`clicktab_${axis}__${val}`);
}
function percentToRedGreen(percent) {
return `color-mix(in srgb, red, green ${percent}%)`;
}
let scoreTrackCounter = 0;
let scoreUpdates = [];
let lastScoreBump = Date.now();
let scoreBumpTracker = null;
let scoreMin = 0, scoreMax = 1;
function getXAxisContent(x, y, xAxis, yval, x2Axis, x2val, y2Axis, y2val) {
let scriptDump = document.getElementById('image_script_dump');
let imgPath = [];
let index = 0;
for (let subAxis of rawData.axes) {
if (subAxis.id == x) {
index = imgPath.length;
imgPath.push(null);
}
else if (subAxis.id == y) {
imgPath.push(yval.path);
}
else if (x2Axis != null && subAxis.id == x2Axis.id) {
imgPath.push(x2val.path);
}
else if (y2Axis != null && subAxis.id == y2Axis.id) {
imgPath.push(y2val.path);
}
else {
imgPath.push(getSelectedValKey(subAxis));
}
}
let newContent = '';
let subInd = 0;
let scoreDisplay = document.getElementById('score_display').value;
for (let xVal of xAxis.values) {
subInd++;
if (!canShowVal(xAxis.id, xVal.key)) {
continue;
}
imgPath[index] = xVal.path;
let slashed = imgPath.join('/');
let ext = getExtension(slashed);
let actualUrl = slashed + '.' + ext;
let id = scoreTrackCounter++;
newContent += `
\n'
advanced_settings = ''
primary = True
for axis in grid.axes:
try:
axis_descrip = clean_for_web(axis.description or '')
tr_class = "primary" if primary else "secondary"
content += f'
\n
\n
{escape_html(axis.title)}
\n'
advanced_settings += f'\n
{axis.title}
Auto cycle every
\nShow value: '
axis_class = "axis_table_cell"
if len(axis_descrip.strip()) == 0:
axis_class += " emptytab"
content += f'
{axis_descrip}
\n
\n'
primary = not primary
is_first = axis.default is None
exported_paths = {}
for val in axis.values:
if val.path in exported_paths:
continue
exported_paths[val.path] = val
if axis.default is not None:
is_first = str(axis.default) == str(val.key)
selected = "true" if is_first else "false"
active = " active" if is_first else ""
is_first = False
descrip = clean_for_web(val.description or '')
content += f'
\n'
advanced_settings += f' '
advanced_settings += f' '
content += '
\n
\n'
is_first = axis.default is None
for val in axis.values:
if axis.default is not None:
is_first = str(axis.default) == str(val.key)
active = " active show" if is_first else ""
is_first = False
descrip = clean_for_web(val.description or '')
if len(descrip.strip()) == 0:
active += " emptytab"
content += f'
{descrip}
\n'
except Exception as e:
raise RuntimeError(f"Failed to build HTML for axis '{axis.id}': {e}")
content += '
\n'
html = html.replace("{TITLE}", grid.title).replace("{CLEAN_DESCRIPTION}", clean_for_web(grid.description)).replace("{DESCRIPTION}", grid.description).replace("{CONTENT}", content).replace("{ADVANCED_SETTINGS}", advanced_settings).replace("{AUTHOR}", grid.author).replace("{EXTRA_FOOTER}", EXTRA_FOOTER).replace("{VERSION}", get_version())
return html
def emit_web_data(path: str, grid, publish_gen_metadata: bool, p, yaml_content: dict, dry_run: bool):
print("Building final web data...")
os.makedirs(path, exist_ok=True)
json = WebDataBuilder.build_json(grid, publish_gen_metadata, p, dry_run)
if not dry_run:
with open(path + '/last.js', 'w', encoding="utf-8") as f:
f.write("window.lastUpdated = []")
with open(path + "/data.js", 'w', encoding="utf-8") as f:
f.write("rawData = " + json)
with open(path + "/config.yml", 'w', encoding="utf-8") as f:
yaml.dump(yaml_content, f, sort_keys=False, default_flow_style=False, width=1000)
for f in ["bootstrap.min.css", "jsgif.js", "bootstrap.bundle.min.js", "proc.js", "jquery.min.js", "styles.css", "placeholder.png"] + EXTRA_ASSETS:
shutil.copyfile(ASSET_DIR + "/" + f, path + "/" + f)
with open(ASSET_DIR + "/styles-user.css", 'r', encoding="utf-8") as style:
with open(path + "/styles-user.css", 'w', encoding="utf-8") as f:
f.write(style.read() + '\n' + (grid.stylesheet or ''))
html = WebDataBuilder.build_html(grid)
with open(path + "/index.html", 'w', encoding="utf-8") as f:
f.write(html)
print(f"Web file is now at {path}/index.html")
return json
######################### Main Runner Function #########################
def run_grid_gen(pass_through_obj, input_file: str, output_folder_base: str, output_folder_name: str = None, do_overwrite: bool = False,
fast_skip: bool = False, generate_page: bool = True, publish_gen_metadata: bool = True, dry_run: bool = False, manual_pairs: list = None, allow_includes: bool = True, skip_invalid: bool = False):
grid = GridFileHelper()
grid.stylesheet = ''
grid.skip_invalid = skip_invalid
yaml_content = None
if manual_pairs is None:
full_input_path = ASSET_DIR + "/" + input_file
if not os.path.exists(full_input_path):
raise RuntimeError(f"Non-existent file '{input_file}'")
# Parse and verify
with open(full_input_path, 'r', encoding="utf-8") as yaml_content_text:
try:
if allow_includes:
yaml_content = yaml.load(yaml_content_text, Loader=GridYamlLoader)
else:
yaml_content = yaml.safe_load(yaml_content_text)
except yaml.YAMLError as exc:
raise RuntimeError(f"Invalid YAML in file '{input_file}': {exc}")
grid.parse_yaml(yaml_content, input_file)
else:
grid.title = output_folder_name
grid.description = ""
grid.variables = dict()
grid.author = "Unspecified"
grid.format = "png"
grid.axes = list()
grid.params = None
grid.grid_obj = {}
yaml_content = {
'grid': {
'title': grid.title,
'description': grid.description,
'format': grid.format,
'author': grid.author
},
'axes': {}
}
for i in range(0, int(len(manual_pairs) / 2)):
key = manual_pairs[i * 2]
if isinstance(key, str) and key.strip() != "":
try:
val = manual_pairs[i * 2 + 1]
grid.axes.append(Axis(grid, key, val))
yaml_key = key
duplicates = 1
while yaml_key in yaml_content['axes']:
duplicates += 1
yaml_key = f"{key} {duplicates}"
yaml_content['axes'][yaml_key] = val
except Exception as e:
raise RuntimeError(f"Invalid axis {(i + 1)} '{key}': errored: {e}")
# Now start using it
if output_folder_name.strip() == "":
if grid.out_path is None:
output_folder_name = input_file.replace(".yml", "")
else:
output_folder_name = grid.out_path.strip()
if os.path.isabs(output_folder_name):
folder = output_folder_name
else:
folder = output_folder_base + "/" + output_folder_name
runner = GridRunner(grid, do_overwrite, folder, pass_through_obj, fast_skip)
runner.preprocess()
if generate_page:
json = WebDataBuilder.emit_web_data(folder, grid, publish_gen_metadata, pass_through_obj, yaml_content, dry_run)
result = runner.run(dry_run)
if dry_run:
print("Infinite Grid dry run succeeded without error")
else:
json = json.replace('"will_run": true, ', '')
with open(folder + "/data.js", 'w', encoding="utf-8") as f:
f.write("rawData = " + json)
os.remove(folder + "/last.js")
return result
================================================
FILE: install.py
================================================
import launch
if not launch.is_installed("yaml"):
launch.run_pip("install pyyaml", "pyyaml for Infinity Grid Script")
if not launch.is_installed("yamlinclude"):
launch.run_pip("install pyyaml-include", "pyyaml-include for Infinity Grid Script")
================================================
FILE: javascript/infinity_grid.js
================================================
/**
* Stable Diffusion Infinity Grid Generator
*
* Author: Alex 'mcmonkey' Goodwin
*
* GitHub URL: https://github.com/mcmonkeyprojects/sd-infinity-grid-generator-script
* Created: 2022/12/08
* Last updated: 2023/02/19
*
* For usage help, view the README.md file in the extension root, or via the GitHub page.
*
*/
// Title injection code referenced from d8ahazard's Dreambooth extension
ex_titles = titles;
new_titles = {
"Select grid definition file": "Select the grid definition yaml file, in your '(extension)/assets' folder. Refer to the README for info.",
"Overwrite existing images (for updating grids)": "If checked, any existing image files will be overwritten - this is useful if you want to redo the grid with completely different base settings. If unchecked, if an image already exists, it will be skipped - this is useful for adding new options to an existing grid.",
"Generate infinite-grid webviewer page": "If checked, generate the webviewer page. If unchecked, won't generate. You can uncheck this for dryruns that don't need it, or to avoid overwriting customized pages.",
"Do a dry run to validate your grid file": "If checked, no images will be rendered - it will just validate your YAML and all its content. Check the WebUI's console for any messages.",
"Publish full generation metadata for viewing on-page": "If checked, any/all image metadata will be stored in the webpage's files, and the internal values of each axis. This is useful for viewing, but if you're sharing a generation where some details are private (eg exact prompt text) you'll want to uncheck this. Note that this doesn't change whether metadata gets stored in images or not, edit your Settings tab to configure that.",
"Use more-performant skipping": "Only matters if you have 'skip: true' on any values - if checked, uses a method of skipping that improves performance but prevents validation of the skipped options.",
"Validate PromptReplace input": "If unchecked, will allow useless PromptReplace settings to be ignored. If checked, will error if the replace won't do anything."
}
for (var i = 1; i <= 16; i++) {
new_titles[`Axis ${i} Mode`] = "Select the desired mode / setting - ie what value it is that should be changing, for this axis. You have as many axes as you need, just select modes and more will be added as you go.";
new_titles[`Axis ${i} Value`] = "Fill in values applicable to the mode, separated by commas. If it's a numeric mode, you can do for example '1, 2, 3'.";
}
ex_titles = Object.assign({}, ex_titles, new_titles);
titles = ex_titles;
================================================
FILE: scripts/infinity_grid.py
================================================
##################
# Stable Diffusion Infinity Grid Generator
#
# Author: Alex 'mcmonkey' Goodwin
# GitHub URL: https://github.com/mcmonkeyprojects/sd-infinity-grid-generator-script
# Created: 2022/12/08
# Last updated: 2023/02/19
# License: MIT
#
# For usage help, view the README.md file in the extension root, or via the GitHub page.
#
##################
import gradio as gr
import os, numpy, threading
from copy import copy
from datetime import datetime
from modules import images, shared, sd_models, sd_vae, sd_samplers, scripts, processing, ui_components
from modules.processing import process_images, Processed
from modules.shared import opts, state
from PIL import Image
import gridgencore as core
from gridgencore import clean_name, clean_mode, get_best_in_list, choose_better_file_name, GridSettingMode, fix_num, apply_field, registerMode
######################### Constants #########################
refresh_symbol = '\U0001f504' # 🔄
fill_values_symbol = "\U0001f4d2" # 📒
INF_GRID_README = "https://github.com/mcmonkeyprojects/sd-infinity-grid-generator-script"
core.EXTRA_FOOTER = 'Images area auto-generated by an AI (Stable Diffusion) and so may not have been reviewed by the page author before publishing.\n'
core.EXTRA_ASSETS = ["a1111webui.js"]
######################### Value Mode Helpers #########################
def get_model_for(name):
return get_best_in_list(name, map(lambda m: m.title, sd_models.checkpoints_list.values()))
def apply_model(p, v):
opts.sd_model_checkpoint = get_model_for(v)
sd_models.reload_model_weights()
def clean_model(p, v):
actual_model = get_model_for(v)
if actual_model is None:
raise RuntimeError(f"Invalid parameter '{p}' as '{v}': model name unrecognized - valid {list(map(lambda m: m.title, sd_models.checkpoints_list.values()))}")
return choose_better_file_name(v, actual_model)
def get_vae_for(name):
return get_best_in_list(name, sd_vae.vae_dict.keys())
def apply_vae(p, v):
vae_name = clean_name(v)
if vae_name == "none":
vae_name = "None"
elif vae_name in ["auto", "automatic"]:
vae_name = "Automatic"
else:
vae_name = get_vae_for(vae_name)
opts.sd_vae = vae_name
sd_vae.reload_vae_weights(None)
def clean_vae(p, v):
vae_name = clean_name(v)
if vae_name in ["none", "auto", "automatic"]:
return vae_name
actual_vae = get_vae_for(vae_name)
if actual_vae is None:
raise RuntimeError(f"Invalid parameter '{p}' as '{v}': VAE name unrecognized - valid: {list(sd_vae.vae_dict.keys())}")
return choose_better_file_name(v, actual_vae)
def apply_codeformer_weight(p, v):
opts.code_former_weight = float(v)
def apply_restore_faces(p, v):
input = str(v).lower().strip()
if input == "false":
p.restore_faces = False
return
p.restore_faces = True
restorer = get_best_in_list(input, map(lambda m: m.name(), shared.face_restorers))
if restorer is not None:
opts.face_restoration_model = restorer
def prompt_replace_parse_list(in_list):
if not any(('=' in x) for x in in_list):
first_val = in_list[0]
for x in range(0, len(in_list)):
in_list[x] = {
"title": in_list[x],
"params": {
"promptreplace": f"{first_val}={in_list[x]}"
}
}
return in_list
def apply_prompt_replace(p, v):
val = v.split('=', maxsplit=1)
if len(val) != 2:
raise RuntimeError(f"Invalid prompt replace, missing '=' symbol, for '{v}'")
match = val[0].strip()
replace = val[1].strip()
if Script.VALIDATE_REPLACE and match not in p.prompt and match not in p.negative_prompt:
raise RuntimeError(f"Invalid prompt replace, '{match}' is not in prompt '{p.prompt}' nor negative prompt '{p.negative_prompt}'")
p.prompt = p.prompt.replace(match, replace)
p.negative_prompt = p.negative_prompt.replace(match, replace)
def apply_enable_hr(p, v):
p.enable_hr = v
if v:
if p.denoising_strength is None:
p.denoising_strength = 0.75
def apply_styles(p, v: str):
p.styles = list(v.split(','))
def apply_setting_override(name: str):
def applier(p, v):
p.override_settings[name] = v
return applier
######################### Value Modes #########################
has_inited = False
def try_init():
global has_inited
if has_inited:
return
has_inited = True
core.grid_call_init_hook = a1111_grid_call_init_hook
core.grid_call_param_add_hook = a1111_grid_call_param_add_hook
core.grid_call_apply_hook = a1111_grid_call_apply_hook
core.grid_runner_pre_run_hook = a1111_grid_runner_pre_run_hook
core.grid_runner_pre_dry_hook = a1111_grid_runner_pre_dry_hook
core.grid_runner_post_dry_hook = a1111_grid_runner_post_dry_hook
core.grid_runner_count_steps = a1111_grid_runner_count_steps
core.webdata_get_base_param_data = a1111_webdata_get_base_param_data
registerMode("Model", GridSettingMode(dry=False, type="text", apply=apply_model, clean=clean_model, valid_list=lambda: list(map(lambda m: m.title, sd_models.checkpoints_list.values()))))
registerMode("VAE", GridSettingMode(dry=False, type="text", apply=apply_vae, clean=clean_vae, valid_list=lambda: list(sd_vae.vae_dict.keys()) + ['none', 'auto', 'automatic']))
registerMode("Sampler", GridSettingMode(dry=True, type="text", apply=apply_field("sampler_name"), valid_list=lambda: list(sd_samplers.all_samplers_map.keys())))
registerMode("Seed", GridSettingMode(dry=True, type="integer", apply=apply_field("seed")))
registerMode("Steps", GridSettingMode(dry=True, type="integer", min=0, max=200, apply=apply_field("steps")))
registerMode("CFG Scale", GridSettingMode(dry=True, type="decimal", min=0, max=500, apply=apply_field("cfg_scale")))
registerMode("Width", GridSettingMode(dry=True, type="integer", apply=apply_field("width")))
registerMode("Height", GridSettingMode(dry=True, type="integer", apply=apply_field("height")))
registerMode("Prompt", GridSettingMode(dry=True, type="text", apply=apply_field("prompt")))
registerMode("Negative Prompt", GridSettingMode(dry=True, type="text", apply=apply_field("negative_prompt")))
registerMode("Prompt Replace", GridSettingMode(dry=True, type="text", apply=apply_prompt_replace, parse_list=prompt_replace_parse_list))
registerMode("Styles", GridSettingMode(dry=True, type="text", apply=apply_styles, valid_list=lambda: list(shared.prompt_styles.styles)))
registerMode("Var Seed", GridSettingMode(dry=True, type="integer", apply=apply_field("subseed")))
registerMode("Var Strength", GridSettingMode(dry=True, type="decimal", min=0, max=1, apply=apply_field("subseed_strength")))
registerMode("ClipSkip", GridSettingMode(dry=False, type="integer", min=1, max=12, apply=apply_setting_override("CLIP_stop_at_last_layers")))
registerMode("Denoising", GridSettingMode(dry=True, type="decimal", min=0, max=1, apply=apply_field("denoising_strength")))
registerMode("ETA", GridSettingMode(dry=True, type="decimal", min=0, max=1, apply=apply_field("eta")))
registerMode("Sigma Churn", GridSettingMode(dry=True, type="decimal", min=0, max=1, apply=apply_field("s_churn")))
registerMode("Sigma TMin", GridSettingMode(dry=True, type="decimal", min=0, max=1, apply=apply_field("s_tmin")))
registerMode("Sigma TMax", GridSettingMode(dry=True, type="decimal", min=0, max=1, apply=apply_field("s_tmax")))
registerMode("Sigma Noise", GridSettingMode(dry=True, type="decimal", min=0, max=1, apply=apply_field("s_noise")))
registerMode("Out Width", GridSettingMode(dry=True, type="integer", min=0, apply=apply_field("inf_grid_out_width")))
registerMode("Out Height", GridSettingMode(dry=True, type="integer", min=0, apply=apply_field("inf_grid_out_height")))
registerMode("Restore Faces", GridSettingMode(dry=True, type="text", apply=apply_restore_faces, valid_list=lambda: list(map(lambda m: m.name(), shared.face_restorers)) + ["true", "false"]))
registerMode("CodeFormer Weight", GridSettingMode(dry=True, type="decimal", min=0, max=1, apply=apply_codeformer_weight))
registerMode("Tiling", GridSettingMode(dry=True, type="boolean", apply=apply_field("tiling")))
registerMode("Image Mask Weight", GridSettingMode(dry=True, type="decimal", min=0, max=1, apply=apply_field("inpainting_mask_weight")))
registerMode("ETA Noise Seed Delta", GridSettingMode(dry=True, type="integer", apply=apply_setting_override("eta_noise_seed_delta")))
registerMode("Enable HighRes Fix", GridSettingMode(dry=True, type="boolean", apply=apply_enable_hr))
registerMode("HighRes Scale", GridSettingMode(dry=True, type="decimal", min=1, max=16, apply=apply_field("hr_scale")))
registerMode("HighRes Steps", GridSettingMode(dry=True, type="integer", min=0, max=200, apply=apply_field("hr_second_pass_steps")))
registerMode("HighRes Resize Width", GridSettingMode(dry=True, type="integer", apply=apply_field("hr_resize_x")))
registerMode("HighRes Resize Height", GridSettingMode(dry=True, type="integer", apply=apply_field("hr_resize_y")))
registerMode("HighRes Upscale to Width", GridSettingMode(dry=True, type="integer", apply=apply_field("hr_upscale_to_x")))
registerMode("HighRes Upscale to Height", GridSettingMode(dry=True, type="integer", apply=apply_field("hr_upscale_to_y")))
registerMode("HighRes Upscaler", GridSettingMode(dry=True, type="text", apply=apply_field("hr_upscaler"), valid_list=lambda: list(map(lambda u: u.name, shared.sd_upscalers)) + list(shared.latent_upscale_modes.keys())))
registerMode("HighRes Sampler", GridSettingMode(dry=True, type="text", apply=apply_field("hr_sampler_name"), valid_list=lambda: list(sd_samplers.all_samplers_map.keys())))
registerMode("HighRes Checkpoint", GridSettingMode(dry=False, type="text", apply=apply_field("hr_checkpoint_name"), clean=clean_model, valid_list=lambda: list(map(lambda m: m.title, sd_models.checkpoints_list.values()))))
registerMode("Image CFG Scale", GridSettingMode(dry=True, type="decimal", min=0, max=500, apply=apply_field("image_cfg_scale")))
registerMode("Use Result Index", GridSettingMode(dry=True, type="integer", min=0, max=500, apply=apply_field("inf_grid_use_result_index")))
try:
script_list = [x for x in scripts.scripts_data if x.script_class.__module__ == "dynamic_thresholding.py"][:1]
if len(script_list) == 1:
dynamic_thresholding = script_list[0].module
registerMode("[DynamicThreshold] Enable", GridSettingMode(dry=True, type="boolean", apply=apply_field("dynthres_enabled")))
registerMode("[DynamicThreshold] Mimic Scale", GridSettingMode(dry=True, type="decimal", min=0, max=500, apply=apply_field("dynthres_mimic_scale")))
registerMode("[DynamicThreshold] Threshold Percentile", GridSettingMode(dry=True, type="decimal", min=0.0, max=100.0, apply=apply_field("dynthres_threshold_percentile")))
registerMode("[DynamicThreshold] Mimic Mode", GridSettingMode(dry=True, type="text", apply=apply_field("dynthres_mimic_mode"), valid_list=lambda: list(dynamic_thresholding.VALID_MODES)))
registerMode("[DynamicThreshold] CFG Mode", GridSettingMode(dry=True, type="text", apply=apply_field("dynthres_cfg_mode"), valid_list=lambda: list(dynamic_thresholding.VALID_MODES)))
registerMode("[DynamicThreshold] Mimic Scale Minimum", GridSettingMode(dry=True, type="decimal", min=0.0, max=100.0, apply=apply_field("dynthres_mimic_scale_min")))
registerMode("[DynamicThreshold] CFG Scale Minimum", GridSettingMode(dry=True, type="decimal", min=0.0, max=100.0, apply=apply_field("dynthres_cfg_scale_min")))
registerMode("[DynamicThreshold] Experiment Mode", GridSettingMode(dry=True, type="decimal", min=0, max=100000, apply=apply_field("dynthres_experiment_mode")))
registerMode("[DynamicThreshold] Scheduler Value", GridSettingMode(dry=True, type="decimal", min=0, max=100, apply=apply_field("dynthres_scheduler_val")))
registerMode("[DynamicThreshold] Scaling Startpoint", GridSettingMode(dry=True, type="text", apply=apply_field("dynthres_scaling_startpoint"), valid_list=lambda: list(['ZERO', 'MEAN'])))
registerMode("[DynamicThreshold] Variability Measure", GridSettingMode(dry=True, type="text", apply=apply_field("dynthres_variability_measure"), valid_list=lambda: list(['STD', 'AD'])))
registerMode("[DynamicThreshold] Interpolate Phi", GridSettingMode(dry=True, type="decimal", min=0, max=1, apply=apply_field("dynthres_interpolate_phi")))
registerMode("[DynamicThreshold] Separate Feature Channels", GridSettingMode(dry=True, type="boolean", apply=apply_field("dynthres_separate_feature_channels")))
script_list = [x for x in scripts.scripts_data if x.script_class.__module__ == "controlnet.py"][:1]
if len(script_list) == 1:
# Hacky but works
module = script_list[0].module
preprocessors_list = list(p.name for p in module.Preprocessor.get_sorted_preprocessors())
def validate_param(p, v):
if not shared.opts.data.get("control_net_allow_script_control", False):
raise RuntimeError("ControlNet options cannot currently work, you must enable 'Allow other script to control this extension' in Settings -> ControlNet first")
return v
registerMode("[ControlNet] Enable", GridSettingMode(dry=True, type="boolean", apply=apply_field("control_net_enabled"), clean=validate_param))
registerMode("[ControlNet] Preprocessor", GridSettingMode(dry=True, type="text", apply=apply_field("control_net_module"), clean=validate_param, valid_list=lambda: list(preprocessors_list)))
registerMode("[ControlNet] Model", GridSettingMode(dry=True, type="text", apply=apply_field("control_net_model"), clean=validate_param, valid_list=lambda: list(list(module.global_state.cn_models.keys()))))
registerMode("[ControlNet] Weight", GridSettingMode(dry=True, type="decimal", min=0.0, max=2.0, apply=apply_field("control_net_weight"), clean=validate_param))
registerMode("[ControlNet] Guidance Strength", GridSettingMode(dry=True, type="decimal", min=0.0, max=1.0, apply=apply_field("control_net_guidance_strength"), clean=validate_param))
registerMode("[ControlNet] Annotator Resolution", GridSettingMode(dry=True, type="integer", min=0, max=2048, apply=apply_field("control_net_pres"), clean=validate_param))
registerMode("[ControlNet] Threshold A", GridSettingMode(dry=True, type="integer", min=0, max=256, apply=apply_field("control_net_pthr_a"), clean=validate_param))
registerMode("[ControlNet] Threshold B", GridSettingMode(dry=True, type="integer", min=0, max=256, apply=apply_field("control_net_pthr_b"), clean=validate_param))
registerMode("[ControlNet] Image", GridSettingMode(dry=True, type="text", apply=core.apply_field_as_image_data("control_net_input_image"), clean=validate_param, valid_list=lambda: core.list_image_files()))
except Exception as e:
print(f"Infinity Grid Generator failed to import a dependency module: {e}")
pass
######################### Actual Execution Logic #########################
def a1111_grid_call_init_hook(grid_call: core.SingleGridCall):
grid_call.replacements = list()
def a1111_grid_call_param_add_hook(grid_call: core.SingleGridCall, param: str, value):
if grid_call.grid.min_width is None:
grid_call.grid.min_width = grid_call.grid.initial_p.width
if grid_call.grid.min_height is None:
grid_call.grid.min_height = grid_call.grid.initial_p.height
cleaned = clean_mode(param)
if cleaned == "promptreplace":
grid_call.replacements.append(value)
return True
elif cleaned in ["width", "outwidth"]:
grid_call.grid.min_width = min(grid_call.grid.min_width or 99999, int(value))
elif cleaned in ["height", "outheight"]:
grid_call.grid.min_height = min(grid_call.grid.min_height or 99999, int(value))
return False
def a1111_grid_call_apply_hook(grid_call: core.SingleGridCall, param: str, dry: bool):
for replace in grid_call.replacements:
apply_prompt_replace(param, replace)
def a1111_grid_runner_pre_run_hook(grid_runner: core.GridRunner):
state.job_count = grid_runner.total_run
shared.total_tqdm.updateTotal(grid_runner.total_steps)
# prevents the steps from from being recalculated by Auto1 using the current value of hires steps
state.processing_has_refined_job_count = True
class TempHolder: pass
def a1111_grid_runner_pre_dry_hook(grid_runner: core.GridRunner):
grid_runner.temp = TempHolder()
grid_runner.temp.old_codeformer_weight = opts.code_former_weight
grid_runner.temp.old_face_restorer = opts.face_restoration_model
grid_runner.temp.old_vae = opts.sd_vae
grid_runner.temp.old_model = opts.sd_model_checkpoint
def a1111_grid_runner_post_dry_hook(grid_runner: core.GridRunner, p, set):
p.seed = processing.get_fixed_seed(p.seed)
p.subseed = processing.get_fixed_seed(p.subseed)
processed = process_images(p)
if len(processed.images) < 1:
raise RuntimeError(f"Something went wrong! Image gen '{set.data}' produced {len(processed.images)} images, which is wrong")
os.makedirs(os.path.dirname(set.filepath), exist_ok=True)
result_index = getattr(p, 'inf_grid_use_result_index', 0)
if result_index >= len(processed.images):
result_index = len(processed.images) - 1
img = processed.images[result_index]
if type(img) == numpy.ndarray:
img = Image.fromarray(img)
if hasattr(p, 'inf_grid_out_width') and hasattr(p, 'inf_grid_out_height'):
img = img.resize((p.inf_grid_out_width, p.inf_grid_out_height), resample=images.LANCZOS)
processed.images[result_index] = img
info = processing.create_infotext(p, [p.prompt], [p.seed], [p.subseed], [])
ext = grid_runner.grid.format
prompt = p.prompt
seed = processed.seed
def save_offthread():
images.save_image(img, path=os.path.dirname(set.filepath), basename="", forced_filename=os.path.basename(set.filepath), save_to_dirs=False, info=info, extension=ext, p=p, prompt=prompt, seed=seed)
threading.Thread(target=save_offthread).start()
opts.code_former_weight = grid_runner.temp.old_codeformer_weight
opts.face_restoration_model = grid_runner.temp.old_face_restorer
opts.sd_vae = grid_runner.temp.old_vae
opts.sd_model_checkpoint = grid_runner.temp.old_model
grid_runner.temp = None
return processed
def a1111_grid_runner_count_steps(grid_runner: core.GridRunner, set):
step_count = set.params.get("steps")
step_count = int(step_count) if step_count is not None else grid_runner.p.steps
total_steps = step_count
enable_hr = set.params.get("enable highres fix")
if enable_hr is None:
enable_hr = grid_runner.p.enable_hr if hasattr(grid_runner.p, 'enable_hr') else False
if enable_hr:
highres_steps = set.params.get("highres steps")
highres_steps = int(highres_steps) if highres_steps is not None else (grid_runner.p.hr_second_pass_steps or step_count)
total_steps += highres_steps
return total_steps
def a1111_webdata_get_base_param_data(p):
return {
"sampler": p.sampler_name,
"seed": p.seed,
"restorefaces": (opts.face_restoration_model if p.restore_faces else None),
"steps": p.steps,
"cfgscale": p.cfg_scale,
"model": choose_better_file_name('', shared.sd_model.sd_checkpoint_info.model_name).replace(',', '').replace(':', ''),
"vae": (None if sd_vae.loaded_vae_file is None else (choose_better_file_name('', sd_vae.loaded_vae_file).replace(',', '').replace(':', ''))),
"width": p.width,
"height": p.height,
"prompt": p.prompt,
"negativeprompt": p.negative_prompt,
"varseed": (None if p.subseed_strength == 0 else p.subseed),
"varstrength": (None if p.subseed_strength == 0 else p.subseed_strength),
"clipskip": opts.CLIP_stop_at_last_layers,
"codeformerweight": opts.code_former_weight,
"denoising": getattr(p, 'denoising_strength', None),
"eta": fix_num(p.eta),
"sigmachurn": fix_num(p.s_churn),
"sigmatmin": fix_num(p.s_tmin),
"sigmatmax": fix_num(p.s_tmax),
"sigmanoise": fix_num(p.s_noise),
"ENSD": None if opts.eta_noise_seed_delta == 0 else opts.eta_noise_seed_delta
}
class SettingsFixer():
def __enter__(self):
self.model = opts.sd_model_checkpoint
self.code_former_weight = opts.code_former_weight
self.face_restoration_model = opts.face_restoration_model
self.vae = opts.sd_vae
def __exit__(self, exc_type, exc_value, tb):
opts.code_former_weight = self.code_former_weight
opts.face_restoration_model = self.face_restoration_model
opts.sd_vae = self.vae
opts.sd_model_checkpoint = self.model
sd_models.reload_model_weights()
sd_vae.reload_vae_weights()
######################### Script class entrypoint #########################
class Script(scripts.Script):
BASEDIR = scripts.basedir()
VALIDATE_REPLACE = True
def title(self):
return "Generate Infinite-Axis Grid"
def show(self, is_img2img):
return True
def ui(self, is_img2img):
core.list_image_files()
try_init()
gr.HTML(value=f" Confused/new? View the README for usage instructions.
")
with gr.Row():
grid_file = gr.Dropdown(value="Create in UI",label="Select grid definition file", choices=["Create in UI"] + core.get_name_list())
def refresh():
new_choices = ["Create in UI"] + core.get_name_list()
grid_file.choices = new_choices
return gr.update(choices=new_choices)
refresh_button = ui_components.ToolButton(value=refresh_symbol, elem_id="infinity_grid_refresh_button")
refresh_button.click(fn=refresh, inputs=[], outputs=[grid_file])
output_file_path = gr.Textbox(value="", label="Output folder name (if blank uses yaml's 'outpath' parameter, filename, or current date)")
page_will_be = gr.HTML(value="(...)
")
manual_group = gr.Group(visible=True)
manual_axes = list()
sets = list()
def get_page_url_text(file):
if file is None:
return "(...)"
notice = ""
if not os.path.isabs(file):
out_path = opts.outdir_grids or (opts.outdir_img2img_grids if is_img2img else opts.outdir_txt2img_grids)
full_out_path = out_path + "/" + file
url = "/file=" + full_out_path
else:
full_out_path = file
url = "file://" + ("" if file.startswith("/") else "/") + file
notice = " This is a raw file path, not within the WebUI output directory. You may need to open the output file manually."
if os.path.exists(full_out_path):
notice += " NOTICE: There is already something saved there! This will overwrite prior data."
return f"Page will be at (Click me) {full_out_path}{notice}
"
def update_page_url(file_path, selected_file):
out_file_update = gr.Textbox.update()
if file_path == "" and selected_file == "Create in UI":
file_path = f"autonamed_inf_grid_{datetime.now().strftime('%Y_%m_%d_%H_%M_%S')}"
out_file_update = gr.Textbox.update(value=file_path)
info_update = gr.update(value=get_page_url_text(file_path or (selected_file.replace(".yml", "") if selected_file is not None else None)))
return [out_file_update, info_update]
def update_page_url_single(file_path, selected_file):
(_, info_update) = update_page_url(file_path, selected_file)
return info_update
with manual_group:
with gr.Row():
with gr.Column():
axis_count = 0
for group in range(0, 4):
group_obj = gr.Group(visible=group == 0)
with group_obj:
rows = list()
for i in range(0, 4):
with gr.Row():
axis_count += 1
row_mode = gr.Dropdown(value="", label=f"Axis {axis_count} Mode", choices=[" "] + [x.name for x in core.valid_modes.values()])
row_value = gr.Textbox(label=f"Axis {axis_count} Value", lines=1)
fill_row_button = ui_components.ToolButton(value=fill_values_symbol, visible=False)
def fill_axis(mode_name):
core.clear_caches()
mode = core.valid_modes.get(clean_mode(mode_name))
if mode is None:
return gr.update()
elif mode.type == "boolean":
return "true, false"
elif mode.valid_list is not None:
return ", ".join(list(mode.valid_list()))
raise RuntimeError(f"Can't fill axis for {mode_name}")
fill_row_button.click(fn=fill_axis, inputs=[row_mode], outputs=[row_value])
def on_axis_change(mode_name, out_file):
mode = core.valid_modes.get(clean_mode(mode_name))
button_update = gr.Button.update(visible=mode is not None and (mode.valid_list is not None or mode.type == "boolean"))
(out_file_update, info_update) = update_page_url(out_file, "Create in UI")
return [button_update, out_file_update, info_update]
row_mode.change(fn=on_axis_change, inputs=[row_mode, output_file_path], outputs=[fill_row_button, output_file_path, page_will_be])
manual_axes += list([row_mode, row_value])
rows.append(row_mode)
sets.append([group_obj, rows])
for group in range(0, 3):
row_mode = sets[group][1][3]
group_obj = sets[group + 1][0]
next_rows = sets[group + 1][1]
def make_vis(prior, r1, r2, r3, r4):
return gr.Group.update(visible=(prior+r1+r2+r3+r4).strip() != "")
row_mode.change(fn=make_vis, inputs=[row_mode] + next_rows, outputs=[group_obj])
gr.HTML('(More input rows will be automatically added after you select modes above.)')
grid_file.change(
fn=lambda x: {"visible": x == "Create in UI", "__type__": "update"},
inputs=[grid_file],
outputs=[manual_group],
show_progress = False)
output_file_path.change(fn=update_page_url_single, inputs=[output_file_path, grid_file], outputs=[page_will_be])
grid_file.change(fn=update_page_url, inputs=[output_file_path, grid_file], outputs=[output_file_path, page_will_be])
with gr.Row():
do_overwrite = gr.Checkbox(value=False, label="Overwrite existing images (for updating grids)")
dry_run = gr.Checkbox(value=False, label="Do a dry run to validate your grid file")
fast_skip = gr.Checkbox(value=False, label="Use more-performant skipping")
skip_invalid = gr.Checkbox(value=False, label="Skip invalid entries")
with gr.Row():
generate_page = gr.Checkbox(value=True, label="Generate infinite-grid webviewer page")
validate_replace = gr.Checkbox(value=True, label="Validate PromptReplace input")
publish_gen_metadata = gr.Checkbox(value=True, label="Publish full generation metadata for viewing on-page")
return [do_overwrite, generate_page, dry_run, validate_replace, publish_gen_metadata, grid_file, fast_skip, output_file_path, skip_invalid] + manual_axes
def run(self, p, do_overwrite, generate_page, dry_run, validate_replace, publish_gen_metadata, grid_file, fast_skip, output_file_path, skip_invalid, *manual_axes):
core.clear_caches()
try_init()
# Clean up default params
p = copy(p)
p.n_iter = 1
p.batch_size = 1
p.do_not_save_samples = True
p.do_not_save_grid = True
p.seed = processing.get_fixed_seed(p.seed)
# Store extra variable
Script.VALIDATE_REPLACE = validate_replace
# Validate to avoid abuse
if '..' in grid_file or grid_file == "":
raise RuntimeError(f"Unacceptable filename '{grid_file}'")
if '..' in output_file_path:
raise RuntimeError(f"Unacceptable alt file path '{output_file_path}'")
if grid_file == "Create in UI":
if output_file_path is None or output_file_path == "":
raise RuntimeError(f"Must specify the output file path")
manual_axes = list(manual_axes)
else:
manual_axes = None
with SettingsFixer():
result = core.run_grid_gen(p, grid_file, p.outpath_grids, output_file_path, do_overwrite, fast_skip, generate_page, publish_gen_metadata, dry_run, manual_axes, skip_invalid=skip_invalid)
if result is None:
return Processed(p, list())
return result
================================================
FILE: style.css
================================================
/**
* This file is part of Stable Diffusion Infinity Grid Generator, view the README.md for more information.
*/
#infinity_grid_refresh_button {
max-width: 2.5em;
min-width: 2.5em;
height: 2.4em;
}