image_utils
IMAGE_CHUNK_SIZE
module-attribute
The chunk size used for image processing. Images must be divisible by this value.
Note that, at the time of writing, 64 is the de-facto standard value for all image models.
DEFAULT_IMAGE_MIN_RESOLUTION
module-attribute
The default minimum resolution of the shortest dimension to use for the first pass.
DEFAULT_HIGHER_RES_MAX_RESOLUTION
module-attribute
The default maximum resolution of the shortest dimension to use for the second pass.
IDEAL_SDXL_RESOLUTIONS
module-attribute
IDEAL_SDXL_RESOLUTIONS = [
(1024, 1024),
(1152, 896),
(896, 1152),
(1216, 832),
(832, 1216),
(1344, 768),
(768, 1344),
(1536, 640),
(640, 1536),
]
The Stability.AI recommended resolutions for SDXL generation.
IDEAL_SDXL_RESOLUTIONS_ASPECT_RATIOS
module-attribute
IDEAL_SDXL_RESOLUTIONS_ASPECT_RATIOS = [
(width / height)
for width, height in IDEAL_SDXL_RESOLUTIONS
]
The aspect ratios of the Stability.AI recommended resolutions for SDXL generation.
MIN_DENOISING_STRENGTH
module-attribute
The minimum denoising strength to use for the upscale sampler
MAX_DENOISING_STRENGTH
module-attribute
The maximum denoising strength to use for the upscale sampler
DECAY_RATE
module-attribute
The rate at which the upscale steps decay based on the denoising strength
MIN_STEPS
module-attribute
The minimum number of steps to use for the upscaling sampler
UPSCALE_ADJUSTMENT_FACTOR
module-attribute
The factor by which the upscale steps are adjusted based on the native resolution distance factor
UPSCALE_DIVISOR
module-attribute
The divisor used to adjust the upscale steps based on the native resolution distance factor
STANDARD_RESOLUTION
module-attribute
The standard resolution used for the resolution penalty calculation
RESOLUTION_PENALTY_MULTIPLIER
module-attribute
The multiplier used for the resolution penalty calculation
STEP_FLOOR_THRESHOLD
module-attribute
The threshold at which the upscale steps are adjusted to the ddim steps
resize_image_dimensions
resize_image_dimensions(
width: int,
height: int,
desired_dimension: int,
use_min: bool,
*,
image_chunk_size: int = IMAGE_CHUNK_SIZE
) -> tuple[int, int]
Resize the image dimensions to have one side equal to the desired resolution, keeping the aspect ratio.
- If use_min is True, the side with the minimum length will be resized to the desired resolution.
- For example, if the image is 1024x2048 and the desired resolution is 512, the image will be resized to 512x1024. (As desired for 512x trained models)
- If use_min is False, the side with the maximum length will be resized to the desired resolution.
- For example, if the image is 1024x2048 and the desired resolution is 1024, the image will be resized to 512x1024. (As desired for 1024x trained models)
- If the image is smaller than the desired resolution, the image will not be resized.
Parameters:
-
width(int) –The width of the image.
-
height(int) –The height of the image.
-
desired_dimension(int) –The desired single side resolution.
-
use_min(bool) –Whether to use the minimum or maximum side.
-
image_chunk_size(int, default:IMAGE_CHUNK_SIZE) –The chunk size used for image processing. Images must be divisible by this value. Defaults to 64, which is the de-facto standard value for all image models at the time of writing. This only should be changed if you are certain that the model you are using requires a different value.
Returns:
-
tuple[int, int]–tuple[int, int]: The target first pass width and height of the image.
Source code in horde_sdk/utils/image_utils.py
get_first_pass_image_resolution_min
get_first_pass_image_resolution_min(
width: int,
height: int,
min_dimension: int = DEFAULT_IMAGE_MIN_RESOLUTION,
) -> tuple[int, int]
Resize the image dimensions to have one side equal to the desired resolution, keeping the aspect ratio.
- If the image is larger than the desired resolution, the side with the minimum length will be resized to the desired resolution.
- If the image is smaller than the desired resolution, the image will not be resized.
Source code in horde_sdk/utils/image_utils.py
get_first_pass_image_resolution_max
get_first_pass_image_resolution_max(
width: int,
height: int,
max_dimension: int = DEFAULT_HIGHER_RES_MAX_RESOLUTION,
) -> tuple[int, int]
Resize the image dimensions to have one side equal to the desired resolution, keeping the aspect ratio.
- If the image is larger than the desired resolution, the side with the maximum length will be resized to the desired resolution.
- If the image is smaller than the desired resolution, the image will not be resized.
Source code in horde_sdk/utils/image_utils.py
get_first_pass_image_resolution_sdxl
Resize the image to fit the SDXL resolution bucket which most closely matches the aspect ratio.
Source code in horde_sdk/utils/image_utils.py
get_first_pass_image_resolution_by_baseline
get_first_pass_image_resolution_by_baseline(
width: int,
height: int,
baseline: KNOWN_IMAGE_GENERATION_BASELINE | None,
) -> tuple[int, int]
Get the first pass image resolution based on the baseline category.
Source code in horde_sdk/utils/image_utils.py
calc_upscale_sampler_steps
calc_upscale_sampler_steps(
model_native_resolution: int | None,
width: int,
height: int,
hires_fix_denoising_strength: float,
ddim_steps: int,
*,
resolution_penalty_multiplier: float = RESOLUTION_PENALTY_MULTIPLIER,
standard_resolution: int = STANDARD_RESOLUTION,
min_denoising_strength: float = MIN_DENOISING_STRENGTH,
max_denoising_strength: float = MAX_DENOISING_STRENGTH,
min_steps: int = MIN_STEPS,
decay_rate: int = DECAY_RATE,
upscale_adjustment_factor: float = UPSCALE_ADJUSTMENT_FACTOR,
upscale_divisor: float = UPSCALE_DIVISOR,
step_floor_threshold: int = STEP_FLOOR_THRESHOLD
) -> int
Calculate the number of upscale steps to use for the upscale sampler based on the input parameters.
Note: The resulting values are non-linear to the input values. The heuristic is based on the native resolution of the model, the requested resolution, the denoising strength and the number of steps used for the ddim sampler.
Practically speaking, the resulting number of steps should be roughly the number of step required for most models to converge to a good result. Generally, doing more second pass steps than the value returned by this function is wasted effort.
Parameters:
-
model_native_resolution(int) –The native resolution of the model to use for the generation. This is the single side resolution (e.g. 512 for a 512x512 model). Note that if this is unspecified (None), the upscale steps will not be adjusted based on the native which will lead to suboptimal results, especially for models which work best at high resolutions.
-
width(int) –The width of the image to generate.
-
height(int) –The height of the image to generate.
-
hires_fix_denoising_strength(float) –The denoising strength to use for the upscale sampler.
-
ddim_steps(int) –The number of steps used for the sampler.
-
resolution_penalty_multiplier(float, default:RESOLUTION_PENALTY_MULTIPLIER) –The multiplier used for the resolution penalty calculation.
-
standard_resolution(int, default:STANDARD_RESOLUTION) –The standard resolution used for the resolution penalty calculation.
-
min_denoising_strength(float, default:MIN_DENOISING_STRENGTH) –The minimum denoising strength to use for the upscale sampler.
-
max_denoising_strength(float, default:MAX_DENOISING_STRENGTH) –The maximum denoising strength to use for the upscale sampler.
-
min_steps(int, default:MIN_STEPS) –The minimum number of steps to use for the upscaling sampler.
-
decay_rate(int, default:DECAY_RATE) –The rate at which the upscale steps decay based on the denoising strength.
-
upscale_adjustment_factor(float, default:UPSCALE_ADJUSTMENT_FACTOR) –The factor by which the upscale steps are adjusted based on the native resolution distance factor.
-
upscale_divisor(float, default:UPSCALE_DIVISOR) –The divisor used to adjust the upscale steps based on the native resolution distance factor.
-
step_floor_threshold(int, default:STEP_FLOOR_THRESHOLD) –The threshold at which the upscale steps are adjusted to the ddim steps.
Returns:
-
int(int) –The number of upscale steps to use for the upscale sampler.
Source code in horde_sdk/utils/image_utils.py
186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 | |
base64_str_to_pil_image
base64_str_to_pil_image(
base64_str: str, *, except_on_parse_fail: bool = False
) -> PIL.Image.Image | None
Convert a base64 string to a PIL image.
Parameters:
-
base64_str(str) –The base64 string to convert to a PIL image.
-
except_on_parse_fail(bool, default:False) –Whether to raise an exception if the base64 string cannot be parsed. Defaults to False.
Returns:
-
Image | None–PIL.Image.Image: The PIL image.
Source code in horde_sdk/utils/image_utils.py
bytes_to_pil_image
bytes_to_pil_image(
image_bytes: bytes,
*,
except_on_parse_fail: bool = False
) -> PIL.Image.Image | None
Convert bytes to a PIL image.
Parameters:
-
image_bytes(bytes) –The bytes to convert to a PIL image.
-
except_on_parse_fail(bool, default:False) –Whether to raise an exception if the bytes cannot be parsed. Defaults to False.
Returns:
-
Image | None–PIL.Image.Image: The PIL image.
Source code in horde_sdk/utils/image_utils.py
base64_str_to_bytes
Convert a base64 string to bytes.
Parameters:
-
base64_str(str) –The base64 string to convert to bytes.
-
except_on_parse_fail(bool, default:False) –Whether to raise an exception if the base64 string cannot be parsed. Defaults to False.
Returns:
-
bytes(bytes | None) –The bytes.