Skip to content

Commit

Permalink
Merge pull request AUTOMATIC1111#1 from w-e-w/Rounding-Method
Browse files Browse the repository at this point in the history
Rounding Method
  • Loading branch information
Gerschel authored Feb 8, 2023
2 parents 745382a + 31bbfa7 commit 898922e
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 7 deletions.
36 changes: 29 additions & 7 deletions javascript/aspectRatioSliders.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
class AspectRatioSliderController {
constructor(widthSlider, heightSlider, ratioSource, roundingSource) {
constructor(widthSlider, heightSlider, ratioSource, roundingSource, roundingMethod) {
//References
this.widthSlider = new SliderComponentController(widthSlider);
this.heightSlider = new SliderComponentController(heightSlider);
this.ratioSource = new DropdownComponentController(ratioSource);
this.roundingSource = new CheckboxComponentController(roundingSource);
this.roundingMethod = new RadioComponentController(roundingMethod)
// Badge implementation
this.roundingIndicatorBadge = document.createElement("div");
this.roundingIndicatorBadge.innerText = "📐";
Expand Down Expand Up @@ -46,14 +47,34 @@ class AspectRatioSliderController {
if (dimension == 'width') {
let newHeight = parseInt(this.widthSlider.getVal()) / ratio;
if (this.roundingSource.getVal()) {
newHeight = Math.ceil(newHeight / 8) * 8;
switch (this.roundingMethod.getVal()){
case 'Round':
newHeight = Math.round(newHeight / 8) * 8;
break;
case 'Ceiling':
newHeight = Math.ceil(newHeight / 8) * 8;
break;
case 'Floor':
newHeight = Math.floor(newHeight / 8) * 8;
break;
}
}
this.heightSlider.setVal(newHeight.toString());
}
else if (dimension == "height") {
let newWidth = parseInt(this.heightSlider.getVal()) * ratio;
if (this.roundingSource.getVal()) {
newWidth = Math.ceil(newWidth / 8) * 8;
switch (this.roundingMethod.getVal()){
case 'Round':
newWidth = Math.round(newWidth / 8) * 8;
break;
case 'Ceiling':
newWidth = Math.ceil(newWidth / 8) * 8;
break;
case 'Floor':
newWidth = Math.floor(newWidth / 8) * 8;
break;
}
}
this.widthSlider.setVal(newWidth.toString());
}
Expand Down Expand Up @@ -120,22 +141,23 @@ class AspectRatioSliderController {
}
return this.gcd(b, a % b);
}
static observeStartup(widthSliderId, heightSliderId, ratioSourceId, roundingSourceId) {
static observeStartup(widthSliderId, heightSliderId, ratioSourceId, roundingSourceId, roundingMethodId) {
let observer = new MutationObserver(() => {
let widthSlider = document.querySelector("gradio-app").shadowRoot.getElementById(widthSliderId);
let heightSlider = document.querySelector("gradio-app").shadowRoot.getElementById(heightSliderId);
let ratioSource = document.querySelector("gradio-app").shadowRoot.getElementById(ratioSourceId);
let roundingSource = document.querySelector("gradio-app").shadowRoot.getElementById(roundingSourceId);
let roundingMethod = document.querySelector("gradio-app").shadowRoot.getElementById(roundingMethodId);
if (widthSlider && heightSlider && ratioSource && roundingSource) {
observer.disconnect();
new AspectRatioSliderController(widthSlider, heightSlider, ratioSource, roundingSource);
new AspectRatioSliderController(widthSlider, heightSlider, ratioSource, roundingSource, roundingMethod);
}
});
observer.observe(gradioApp(), { childList: true, subtree: true });
}
}
document.addEventListener("DOMContentLoaded", () => {
//Register mutation observer for self start-up;
AspectRatioSliderController.observeStartup("txt2img_width", "txt2img_height", "txt2img_ratio", "setting_aspect_ratios_rounding");
AspectRatioSliderController.observeStartup("img2img_width", "img2img_height", "img2img_ratio", "setting_aspect_ratios_rounding");
AspectRatioSliderController.observeStartup("txt2img_width", "txt2img_height", "txt2img_ratio", "setting_aspect_ratios_rounding", "setting_aspect_ratios_rounding_method");
AspectRatioSliderController.observeStartup("img2img_width", "img2img_height", "img2img_ratio", "setting_aspect_ratios_rounding", "setting_aspect_ratios_rounding_method");
});
1 change: 1 addition & 0 deletions modules/shared.py
Original file line number Diff line number Diff line change
Expand Up @@ -475,6 +475,7 @@ def list_samplers():
"quicksettings": OptionInfo("sd_model_checkpoint", "Quicksettings list"),
"ui_reorder": OptionInfo(", ".join(ui_reorder_categories), "txt2img/img2img UI item order"),
"aspect_ratios_rounding": OptionInfo(True, "Round aspect ratios for more flexibility?", gr.Checkbox),
"aspect_ratios_rounding_method": OptionInfo("Ceiling", "Aspect ratios rounding method", gr.Radio,{"choices": ["Round", "Ceiling", "Floor"]}),
"aspect_ratios": OptionInfo(", ".join(aspect_ratio_defaults), "txt2img/img2img aspect ratios"),
"ui_extra_networks_tab_reorder": OptionInfo("", "Extra networks tab order"),
"localization": OptionInfo("None", "Localization (requires restart)", gr.Dropdown, lambda: {"choices": ["None"] + list(localization.localizations.keys())}, refresh=lambda: localization.list_localizations(cmd_opts.localizations_dir)),
Expand Down

0 comments on commit 898922e

Please sign in to comment.