add resolutions.txt

This commit is contained in:
alemelis
2023-02-06 08:42:34 +00:00
parent 1b044c883a
commit 9c2a7edceb
3 changed files with 112 additions and 40 deletions

View File

@@ -9,19 +9,22 @@ Browse to the `Extensions` tab -> go to `Install from URL` -> paste in `https://
Here's how the UI looks like after installing this extension
![](https://user-images.githubusercontent.com/4661737/216571656-bc5cffe4-c155-4e73-8ca6-b84a48df0fbf.png)
In red you have some pre-defined aspect ratio buttons, in blue the custom ones.
![](https://user-images.githubusercontent.com/4661737/216922488-fe484735-af16-477f-bd2d-a8606b89a082.png)
## Usage
Simply click on the aspect ratio button you want to set. In the case of an aspect ratio greater than 1, the script fixes the width and changes the height. Whereas if the aspect ratio is smaller than 1, the width changes while the height is fixed.
- Click on the aspect ratio button you want to set. In the case of an aspect ratio greater than 1, the script fixes the width and changes the height. Whereas if the aspect ratio is smaller than 1, the width changes while the height is fixed.
- Reset image resolution by clicking on one of the buttons on the second row. This will set a 1:1 aspect ratio.
### Configuration
Custom aspect ratios can be defined in the `/sd-webui-ar/aspect_ratios.txt` file. The file is pre-populated with the most common values
Aspect ratios can be defined in the `/sd-webui-ar/aspect_ratios.txt` file. The file is pre-populated with the most common values
```
1:1, 1.0
3:2, 1.5
4:3, 1.333
16:9, 1.777
# 6:13, 0.461538
# 9:16, 0.5625
# 3:5, 0.6
@@ -29,22 +32,31 @@ Custom aspect ratios can be defined in the `/sd-webui-ar/aspect_ratios.txt` file
# 19:16, 1.19 # fox movietone
# 5:4, 1.25 # medium format photo
# 11:8, 1.375 # academy standard
IMAX, 1.43
# IMAX, 1.43
# 14:9, 1.56
# 16:10, 1.6
𝜑, 1.6180 # golden ratio
# 𝜑, 1.6180 # golden ratio
# 5:3, 1.6666 # super 16mm
# 1.85, 1.85 # US widescreen cinema
# DCI, 1.9 # digital imax
# 2:1, 2.0 # univisium
# 70mm, 2.2
# 21:9, 2.370 # cinematic wide screen
δ, 2.414 # silver ratio
# δ, 2.414 # silver ratio
# UPV70, 2.76 # ultra panavision 70
# 32:9, 3.6 # ultra wide screen
# PV, 4.0 # polyvision
```
Note the `#` marking the line as a comment, i.e. the extension is not reading that line. To use a custom value, un-comment the relative line by removing the starting `#`.
A custom aspect ratio is defined as `button-label, aspect-ratio-value # comment`. The `aspect-ratio-value` must be a number (either `float` or `int`) while the `# comment` is optional.
A custom aspect ratio is defined as `button-label, aspect-ratio-value # comment`. The `aspect-ratio-value` must be a number (either `float` or `int`) while the `# comment` is optional.
Resolutions are defined in the same into `resolutions.txt` file. By default this reads
```
# 416, 416
512, 512
# 768, 768
```
thus only 416x416 preset is loaded onto the webui.

3
resolutions.txt Normal file
View File

@@ -0,0 +1,3 @@
# 416, 416
512, 512
# 768, 768

View File

@@ -9,6 +9,16 @@ from modules.ui_components import ToolButton
aspect_ratios_dir = scripts.basedir()
class ResButton(ToolButton):
def __init__(self, res=512, **kwargs):
super().__init__(**kwargs)
self.res = res
def reset(self, w, h):
return [self.res, self.res]
class ARButton(ToolButton):
def __init__(self, ar=1.0, **kwargs):
super().__init__(**kwargs)
@@ -17,43 +27,67 @@ class ARButton(ToolButton):
def apply(self, w, h):
if self.ar > 1.0: # fix height, change width
w = self.ar*h
w = self.ar * h
elif self.ar < 1.0: # fix width, change height
h = self.ar*w
h = self.ar * w
else: # set minimum dimension to both
min_dim = min([w, h])
w, h = min_dim, min_dim
return list(map(round, [w, h]))
def reset(self, w, h):
return [self.res, self.res]
def parse_file(filename):
labels, values, comments = [], [], []
file = Path(aspect_ratios_dir, filename)
if not file.exists():
return labels, values, comments
with open(file, "r", encoding="utf-8") as f:
lines = f.readlines()
if not lines:
return labels, values, comments
for line in lines:
if line.startswith("#"):
continue
label, value = line.strip().split(",")
comment = ""
if "#" in value:
value, comment = value.split("#")[0]
labels.append(label)
values.append(value)
comments.append(comment)
return labels, values, comments
class AspectRatioScript(scripts.Script):
aspect_ratios = [1, 3/2, 4/3, 16/9]
aspect_ratio_labels = ["1:1", "3:2", "4:3", "16:9"]
def read_aspect_ratios(self):
aspect_ratios_file = Path(aspect_ratios_dir, "aspect_ratios.txt")
(
self.aspect_ratio_labels,
aspect_ratios,
self.aspect_ratio_comments,
) = parse_file("aspect_ratios.txt")
self.aspect_ratios = list(map(float, aspect_ratios))
# TODO: check for duplicates
# TODO: check for invalid values
if not aspect_ratios_file.exists():
return
# TODO: use comments as tooltips
# see https://github.com/alemelis/sd-webui-ar/issues/5
with open(aspect_ratios_file, 'r', encoding='utf-8') as f:
lines = f.readlines()
for line in lines:
if line.startswith('#'):
continue
label, ratio = line.strip().split(',')
if '#' in ratio:
ratio = ratio.split('#')[0]
ratio = float(ratio)
if label in self.aspect_ratio_labels or ratio in self.aspect_ratios:
continue
self.aspect_ratios.append(ratio)
self.aspect_ratio_labels.append(label)
def read_resolutions(self):
self.res_labels, res, self.res_comments = parse_file("resolutions.txt")
self.res = list(map(int, res))
def title(self):
return "Aspect Ratio picker"
@@ -64,17 +98,40 @@ class AspectRatioScript(scripts.Script):
def ui(self, is_img2img):
self.read_aspect_ratios()
with gr.Row(elem_id="img2img_row_aspect_ratio"):
btns = [ARButton(ar=ar, value=label) for ar, label in zip(
self.aspect_ratios, self.aspect_ratio_labels)]
btns = [
ARButton(ar=ar, value=label)
for ar, label in zip(
self.aspect_ratios,
self.aspect_ratio_labels,
)
]
with contextlib.suppress(AttributeError):
for b in btns:
b.click(b.apply, inputs=[self.w, self.h], outputs=[
self.w, self.h])
b.click(
b.apply,
inputs=[self.w, self.h],
outputs=[self.w, self.h],
)
self.read_resolutions()
with gr.Row(elem_id="img2img_row_resolutions"):
btns = [
ResButton(res=res, value=label)
for res, label in zip(self.res, self.res_labels)
]
with contextlib.suppress(AttributeError):
for b in btns:
b.click(
b.reset,
inputs=[self.w, self.h],
outputs=[self.w, self.h],
)
# https://github.com/AUTOMATIC1111/stable-diffusion-webui/pull/7456#issuecomment-1414465888
def after_component(self, component, **kwargs):
if kwargs.get('elem_id') == 'txt2img_width':
if kwargs.get("elem_id") == "txt2img_width":
self.w = component
if kwargs.get('elem_id') == 'txt2img_height':
if kwargs.get("elem_id") == "txt2img_height":
self.h = component