stouputils.applications.upscaler.image module#

This module provides utility functions for upscaling images using waifu2x-ncnn-vulkan.

It includes functions to upscale individual images, batches of images in a folder, and handle intermediate operations like converting between image formats. The module also manages temporary directories for partial processing and tracks progress of batch operations.

Main functionalities:

  • Converting frames between image formats (PNG to JPG)

  • Upscaling individual images with configurable upscale ratio

  • Batch processing folders of images with progress tracking

  • Handling already processed images to resume interrupted operations

Example usage:

from stouputils.applications.upscaler import upscale, upscale_folder

# Upscale a single image
upscale("input.jpg", "output.jpg", 2)

# Upscale a folder of images
upscale_folder("input_folder", "output_folder", 2)
convert_frame(frame_path: str, delete_png: bool = True) None[source]#

Convert a PNG frame to JPG format to take less space.

Parameters:
  • frame_path (str) – Path to the PNG frame to convert.

  • delete_png (bool) – Whether to delete the original PNG file after conversion.

Returns:

This function doesn’t return anything.

Return type:

None

Example

> convert_frame("input.png", delete_png=True)
> # input.png will be converted to input.jpg and the original file will be deleted

> convert_frame("input.png", delete_png=False)
> # input.png will be converted to input.jpg and the original file will be kept
get_all_files(folder: str, suffix: str | tuple[str, ...] = '') list[str][source]#

Get all files paths in a folder, with a specific suffix if provided.

Parameters:
  • folder (str) – Path to the folder containing the files.

  • suffix (str | tuple[str, ...]) – Suffix of the files to get (e.g. “.png”, “.jpg”, etc.).

Returns:

List of all files paths in the folder.

Return type:

list[str]

Example

>>> files: list[str] = get_all_files("some_folder", ".png")
>>> len(files)
0
create_temp_dir_for_not_upscaled(input_path: str, output_path: str) TemporaryDirectory[str] | None[source]#

Creates a temporary directory containing only images that haven’t been upscaled yet.

Parameters:
  • input_path (str) – Path to the folder containing input images.

  • output_path (str) – Path to the folder where upscaled images are saved.

Returns:

A temporary directory object if there are images to process,

None if all images are already upscaled.

Return type:

TemporaryDirectory[str] | None

upscale(input_path: str, output_path: str, upscale_ratio: int) None[source]#

Upscale an input image (or a directory of images) with the upscaler executable.

Parameters:
  • input_path (str) – Path to the image to upscale (or a directory).

  • output_path (str) – Path to the output image (or a directory).

  • upscale_ratio (int) – Upscaling ratio.

Example

> upscale("folder", "folder", 2)
Traceback (most recent call last):
        ...
AssertionError: Input and output paths cannot be the same, got 'folder'

> upscale("stouputils", "stouputils/output.jpg", 2)
Traceback (most recent call last):
        ...
AssertionError: If input is a directory, output must be a directory too, got 'stouputils/output.jpg'


> upscale("input.jpg", "output.jpg", 2)
> # The input.jpg will be upscaled to output.jpg with a ratio of 2

> upscale("input_folder", "output_folder", 2)
> # The input_folder will be upscaled to output_folder with a ratio of 2
upscale_images(images: list[str], output_folder: str, upscale_ratio: int, desc: str = 'Upscaling images') None[source]#

Upscale multiple images from a list.

Parameters:
  • images (list[str]) – List of paths to the images to upscale.

  • output_folder (str) – Path to the output folder where the upscaled images will be saved.

  • upscale_ratio (int) – Upscaling ratio.

  • desc (str) – Description of the function execution displayed in the progress bar. No progress bar will be displayed if desc is empty.

Returns:

This function doesn’t return anything.

Return type:

None

upscale_folder(input_folder: str, output_folder: str, upscale_ratio: int, slightly_faster_mode: bool = True, desc: str = 'Upscaling folder') None[source]#

Upscale all images in a folder.

Parameters:
  • input_folder (str) – Path to the input folder containing the images to upscale.

  • output_folder (str) – Path to the output folder where the upscaled images will be saved.

  • upscale_ratio (int) – Upscaling ratio.

  • slightly_faster_mode (bool) – Whether to use the slightly faster mode (no progress bar), one call to the upscaler executable.

  • desc (str) – Description of the function execution displayed in the progress bar. No progress bar will be displayed if desc is empty.

Returns:

This function doesn’t return anything.

Return type:

None