stouputils 1.2.35__tar.gz → 1.2.37__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (35) hide show
  1. {stouputils-1.2.35 → stouputils-1.2.37}/.gitignore +6 -0
  2. {stouputils-1.2.35 → stouputils-1.2.37}/PKG-INFO +2 -1
  3. {stouputils-1.2.35 → stouputils-1.2.37}/README.md +1 -0
  4. {stouputils-1.2.35 → stouputils-1.2.37}/pyproject.toml +33 -1
  5. {stouputils-1.2.35 → stouputils-1.2.37}/stouputils/__init__.py +9 -8
  6. {stouputils-1.2.35 → stouputils-1.2.37}/stouputils/all_doctests.py +16 -10
  7. {stouputils-1.2.35 → stouputils-1.2.37}/stouputils/applications/__init__.py +8 -1
  8. stouputils-1.2.37/stouputils/applications/upscaler/__init__.py +39 -0
  9. stouputils-1.2.37/stouputils/applications/upscaler/config.py +128 -0
  10. stouputils-1.2.37/stouputils/applications/upscaler/image.py +247 -0
  11. stouputils-1.2.37/stouputils/applications/upscaler/video.py +285 -0
  12. {stouputils-1.2.35 → stouputils-1.2.37}/stouputils/archive.py +11 -6
  13. {stouputils-1.2.35 → stouputils-1.2.37}/stouputils/backup.py +21 -15
  14. {stouputils-1.2.35 → stouputils-1.2.37}/stouputils/collections.py +1 -0
  15. {stouputils-1.2.35 → stouputils-1.2.37}/stouputils/continuous_delivery/__init__.py +1 -0
  16. {stouputils-1.2.35 → stouputils-1.2.37}/stouputils/continuous_delivery/cd_utils.py +17 -11
  17. {stouputils-1.2.35 → stouputils-1.2.37}/stouputils/continuous_delivery/github.py +59 -19
  18. {stouputils-1.2.35 → stouputils-1.2.37}/stouputils/continuous_delivery/pypi.py +15 -10
  19. {stouputils-1.2.35 → stouputils-1.2.37}/stouputils/continuous_delivery/pyproject.py +4 -1
  20. {stouputils-1.2.35 → stouputils-1.2.37}/stouputils/ctx.py +237 -160
  21. {stouputils-1.2.35 → stouputils-1.2.37}/stouputils/decorators.py +34 -77
  22. {stouputils-1.2.35 → stouputils-1.2.37}/stouputils/dont_look/zip_file_override.py +15 -5
  23. {stouputils-1.2.35 → stouputils-1.2.37}/stouputils/image.py +88 -85
  24. stouputils-1.2.37/stouputils/installer/__init__.py +18 -0
  25. stouputils-1.2.37/stouputils/installer/common.py +67 -0
  26. stouputils-1.2.37/stouputils/installer/downloader.py +101 -0
  27. stouputils-1.2.37/stouputils/installer/linux.py +144 -0
  28. stouputils-1.2.37/stouputils/installer/main.py +223 -0
  29. stouputils-1.2.37/stouputils/installer/windows.py +136 -0
  30. {stouputils-1.2.35 → stouputils-1.2.37}/stouputils/io.py +2 -1
  31. {stouputils-1.2.35 → stouputils-1.2.37}/stouputils/parallel.py +112 -47
  32. {stouputils-1.2.35 → stouputils-1.2.37}/stouputils/print.py +37 -13
  33. {stouputils-1.2.35 → stouputils-1.2.37}/LICENSE +0 -0
  34. {stouputils-1.2.35 → stouputils-1.2.37}/stouputils/applications/automatic_docs.py +0 -0
  35. {stouputils-1.2.35 → stouputils-1.2.37}/stouputils/py.typed +0 -0
@@ -13,3 +13,9 @@ docs/source/_templates/
13
13
  docs/source/modules/
14
14
  docs/source/index.rst
15
15
 
16
+ # Examples
17
+ examples/archive/repaired.zip
18
+ examples/upscaler/input/*.mp4
19
+ examples/upscaler/progress/
20
+ examples/upscaler/output/
21
+
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: stouputils
3
- Version: 1.2.35
3
+ Version: 1.2.37
4
4
  Summary: Stouputils is a collection of utility modules designed to simplify and enhance the development process. It includes a range of tools for tasks such as execution of doctests, display utilities, decorators, as well as context managers, and many more.
5
5
  Project-URL: Homepage, https://github.com/Stoupy51/stouputils
6
6
  Project-URL: Issues, https://github.com/Stoupy51/stouputils/issues
@@ -62,6 +62,7 @@ It includes a range of tools for tasks such as execution of doctests, display ut
62
62
  <pre class="code-tree">stouputils/
63
63
  ├── <a href="https://stoupy51.github.io/stouputils/latest/modules/stouputils.applications.html">applications/</a>
64
64
  │ ├── <a href="https://stoupy51.github.io/stouputils/latest/modules/stouputils.applications.automatic_docs.html">automatic_docs.py</a> <span class="comment"># 📚 Documentation generation utilities (used to create this documentation)</span>
65
+ │ ├── <a href="https://stoupy51.github.io/stouputils/latest/modules/stouputils.applications.upscaler.html">upscaler/</a> <span class="comment"># 🔎 Image & Video upscaler (configurable)</span>
65
66
  │ └── ...
66
67
 
67
68
  ├── <a href="https://stoupy51.github.io/stouputils/latest/modules/stouputils.continuous_delivery.html">continuous_delivery/</a>
@@ -41,6 +41,7 @@ It includes a range of tools for tasks such as execution of doctests, display ut
41
41
  <pre class="code-tree">stouputils/
42
42
  ├── <a href="https://stoupy51.github.io/stouputils/latest/modules/stouputils.applications.html">applications/</a>
43
43
  │ ├── <a href="https://stoupy51.github.io/stouputils/latest/modules/stouputils.applications.automatic_docs.html">automatic_docs.py</a> <span class="comment"># 📚 Documentation generation utilities (used to create this documentation)</span>
44
+ │ ├── <a href="https://stoupy51.github.io/stouputils/latest/modules/stouputils.applications.upscaler.html">upscaler/</a> <span class="comment"># 🔎 Image & Video upscaler (configurable)</span>
44
45
  │ └── ...
45
46
 
46
47
  ├── <a href="https://stoupy51.github.io/stouputils/latest/modules/stouputils.continuous_delivery.html">continuous_delivery/</a>
@@ -5,7 +5,7 @@ build-backend = "hatchling.build"
5
5
 
6
6
  [project]
7
7
  name = "stouputils"
8
- version = "1.2.35"
8
+ version = "1.2.37"
9
9
  description = "Stouputils is a collection of utility modules designed to simplify and enhance the development process. It includes a range of tools for tasks such as execution of doctests, display utilities, decorators, as well as context managers, and many more."
10
10
  readme = "README.md"
11
11
  requires-python = ">=3.10"
@@ -34,6 +34,38 @@ Issues = "https://github.com/Stoupy51/stouputils/issues"
34
34
  [tool.pyright]
35
35
  typeCheckingMode = "strict"
36
36
 
37
+ [tool.ruff]
38
+ exclude = [
39
+ ".git",
40
+ ".ruff_cache",
41
+ ".venv",
42
+ ".vscode",
43
+ ".cursor",
44
+ "build",
45
+ "dist",
46
+ ]
47
+ line-length = 200
48
+
49
+ [tool.ruff.lint]
50
+ select = [
51
+ "E",
52
+ "W",
53
+ "F",
54
+ "I",
55
+ "N",
56
+ "UP",
57
+ "B",
58
+ "C4",
59
+ "T20",
60
+ "RUF",
61
+ ]
62
+ ignore = [
63
+ "T201",
64
+ "W191",
65
+ "N803",
66
+ "N806",
67
+ ]
68
+
37
69
  [tool.hatch.build]
38
70
  include = ["stouputils"]
39
71
 
@@ -14,19 +14,20 @@ Key Features:
14
14
  - Doctests utilities
15
15
 
16
16
  """
17
+ # ruff: noqa: F403
17
18
 
18
19
  # Imports
19
- from .print import *
20
- from .archive import *
21
- from .io import *
22
- from .decorators import *
23
- from .ctx import *
24
- from .parallel import *
25
20
  from .all_doctests import *
26
- from .collections import *
21
+ from .archive import *
27
22
  from .backup import *
28
- from .image import *
23
+ from .collections import *
29
24
 
30
25
  # Folders
31
26
  from .continuous_delivery import *
27
+ from .ctx import *
28
+ from .decorators import *
29
+ from .image import *
30
+ from .io import *
31
+ from .parallel import *
32
+ from .print import *
32
33
 
@@ -6,15 +6,17 @@ This module is used to run all the doctests for all the modules in a given direc
6
6
  """
7
7
 
8
8
  # Imports
9
+ import importlib
9
10
  import os
11
+ import pkgutil
10
12
  import sys
11
- from .print import info, error, progress
12
- from .decorators import measure_time, handle_error, LogLevels
13
- from . import decorators
14
13
  from doctest import TestResults, testmod
15
14
  from types import ModuleType
16
- import importlib
17
- import pkgutil
15
+
16
+ from . import decorators
17
+ from .decorators import LogLevels, handle_error, measure_time
18
+ from .print import error, info, progress
19
+
18
20
 
19
21
  def test_module_with_progress(module: ModuleType, separator: str) -> TestResults:
20
22
  @measure_time(progress, message=f"Testing module '{module.__name__}' {separator}took")
@@ -80,21 +82,25 @@ def launch_tests(root_dir: str, importing_errors: LogLevels = LogLevels.WARNING_
80
82
  separators: list[str] = []
81
83
  for module_path in modules_file_paths:
82
84
  separator: str = " " * (max_length - len(module_path))
85
+
83
86
  @handle_error(error_log=importing_errors)
84
87
  @measure_time(progress, message=f"Importing module '{module_path}' {separator}took")
85
- def internal() -> None:
86
- modules.append(importlib.import_module(module_path))
87
- separators.append(separator)
88
+ def internal(a: str = module_path, b: str = separator) -> None:
89
+ modules.append(importlib.import_module(a))
90
+ separators.append(b)
88
91
  internal()
89
92
 
90
93
  # Run tests for each module
91
94
  info(f"Testing {len(modules)} modules...")
92
95
  separators = [s + " "*(len("Importing") - len("Testing")) for s in separators]
93
- results: list[TestResults] = [test_module_with_progress(module, separator) for module, separator in zip(modules, separators)]
96
+ results: list[TestResults] = [
97
+ test_module_with_progress(module, separator)
98
+ for module, separator in zip(modules, separators, strict=False)
99
+ ]
94
100
 
95
101
  # Display any error lines for each module at the end of the script
96
102
  nb_failed_tests: int = 0
97
- for module, result in zip(modules, results):
103
+ for module, result in zip(modules, results, strict=False):
98
104
  if result.failed > 0:
99
105
  error(f"Errors in module {module.__name__}", exit=False)
100
106
  nb_failed_tests += result.failed
@@ -2,14 +2,21 @@
2
2
 
3
3
  This module provides higher-level utilities for specific application needs:
4
4
 
5
- Key Features:
5
+ Automatic Documentation:
6
+
6
7
  - Automatic documentation generation with Sphinx: `update_documentation(...)`
7
8
  - Support for multi-version documentation
8
9
  - GitHub Pages integration
9
10
  - Markdown to RST conversion
10
11
 
12
+ Upscaler:
13
+
14
+ - TODO
15
+
11
16
  """
17
+ # ruff: noqa: F403
12
18
 
13
19
  # Imports
14
20
  from .automatic_docs import *
21
+ from .upscaler import *
15
22
 
@@ -0,0 +1,39 @@
1
+ """
2
+ This module provides utilities for upscaling images and videos using waifu2x-ncnn-vulkan (by default).
3
+
4
+ It includes functions to upscale individual images, batches of images in a folder,
5
+ and videos by processing them frame by frame. It also handles configuration and
6
+ installation of required dependencies.
7
+
8
+ .. raw:: html
9
+
10
+ <video width="100%" height="auto" controls>
11
+ <source src="https://raw.githubusercontent.com/Stoupy51/stouputils/refs/heads/main/assets/applications/upscaler.mp4" type="video/mp4">
12
+ Your browser does not support the video tag.
13
+ </video>
14
+
15
+ Example of script:
16
+
17
+ .. code-block:: python
18
+
19
+ # Imports
20
+ import stouputils.applications.upscaler as app
21
+ from stouputils.io import get_root_path
22
+
23
+ # Constants
24
+ ROOT: str = get_root_path(__file__) + "/upscaler"
25
+ INPUT_FOLDER: str = f"{ROOT}/input"
26
+ PROGRESS_FOLDER: str = f"{ROOT}/progress"
27
+ OUTPUT_FOLDER: str = f"{ROOT}/output"
28
+
29
+ # Main
30
+ if __name__ == "__main__":
31
+ app.video_upscaler_cli(INPUT_FOLDER, PROGRESS_FOLDER, OUTPUT_FOLDER)
32
+ """
33
+ # ruff: noqa: F403
34
+
35
+ # Imports
36
+ from .config import *
37
+ from .image import *
38
+ from .video import *
39
+
@@ -0,0 +1,128 @@
1
+ """
2
+ This module provides configuration settings and constants for the upscaler application.
3
+
4
+ It defines the URLs for downloading dependencies like waifu2x-ncnn-vulkan and FFmpeg,
5
+ and provides a Config class with settings for upscaling operations, including output quality,
6
+ bitrates, executable paths, and command-line arguments for the underlying tools.
7
+
8
+ Configuration options include:
9
+ - Image quality settings (JPG quality)
10
+ - Video encoding parameters (bitrate, codec, etc.)
11
+ - Paths to external binaries (FFmpeg, waifu2x-ncnn-vulkan)
12
+ - Command-line arguments for upscaling and video processing
13
+ """
14
+ # Constants
15
+ WAIFU2X_NCNN_VULKAN_RELEASES: dict[str, str] = {
16
+ "Windows": "https://github.com/nihui/waifu2x-ncnn-vulkan/releases/download/20220728/waifu2x-ncnn-vulkan-20220728-windows.zip",
17
+ "Linux": "https://github.com/nihui/waifu2x-ncnn-vulkan/releases/download/20220728/waifu2x-ncnn-vulkan-20220728-ubuntu.zip",
18
+ "Darwin": "https://github.com/nihui/waifu2x-ncnn-vulkan/releases/download/20220728/waifu2x-ncnn-vulkan-20220728-macos.zip"
19
+ }
20
+ """ URLs to download waifu2x-ncnn-vulkan from for each common platform. """
21
+ FFMPEG_RELEASES: dict[str, str] = {
22
+ "Windows": "https://github.com/BtbN/FFmpeg-Builds/releases/download/latest/ffmpeg-master-latest-win64-gpl.zip",
23
+ "Linux": "https://github.com/BtbN/FFmpeg-Builds/releases/download/latest/ffmpeg-master-latest-linux64-gpl.tar.xz",
24
+ "Darwin": "https://github.com/BtbN/FFmpeg-Builds/releases/download/latest/ffmpeg-master-latest-macos64-gpl.tar.xz"
25
+ }
26
+ """ URLs to download FFmpeg from for each common platform. """
27
+ YOUTUBE_BITRATE_RECOMMENDATIONS: dict[str, dict[str, dict[int, int]]] = {
28
+ "SDR": {
29
+ "standard": { # Standard Frame Rate (24, 25, 30)
30
+ 4320: 160000, # 8K - 160 Mbps
31
+ 2160: 45000, # 4K - 45 Mbps
32
+ 1440: 16000, # 2K - 16 Mbps
33
+ 1080: 8000, # 1080p - 8 Mbps
34
+ 720: 5000, # 720p - 5 Mbps
35
+ 480: 2500, # 480p - 2.5 Mbps
36
+ 0: 1000 # 360p or lower - 1 Mbps
37
+ },
38
+ "high": { # High Frame Rate (48, 50, 60)
39
+ 4320: 240000, # 8K - 240 Mbps
40
+ 2160: 68000, # 4K - 68 Mbps
41
+ 1440: 24000, # 2K - 24 Mbps
42
+ 1080: 12000, # 1080p - 12 Mbps
43
+ 720: 7500, # 720p - 7.5 Mbps
44
+ 480: 4000, # 480p - 4 Mbps
45
+ 0: 1500 # 360p or lower - 1.5 Mbps
46
+ }
47
+ },
48
+ "HDR": {
49
+ "standard": { # Standard Frame Rate (24, 25, 30)
50
+ 4320: 200000, # 8K - 200 Mbps
51
+ 2160: 56000, # 4K - 56 Mbps
52
+ 1440: 20000, # 2K - 20 Mbps
53
+ 1080: 10000, # 1080p - 10 Mbps
54
+ 0: 6500 # 720p or lower - 6.5 Mbps
55
+ },
56
+ "high": { # High Frame Rate (48, 50, 60)
57
+ 4320: 300000, # 8K - 300 Mbps
58
+ 2160: 85000, # 4K - 85 Mbps
59
+ 1440: 30000, # 2K - 30 Mbps
60
+ 1080: 15000, # 1080p - 15 Mbps
61
+ 0: 9500 # 720p or lower - 9.5 Mbps
62
+ }
63
+ }
64
+ }
65
+ """ YouTube bitrate recommendations for different resolutions and frame rates.
66
+
67
+ This dictionary contains recommended bitrates for YouTube uploads based on:
68
+ - SDR vs HDR content
69
+ - Standard frame rate (24, 25, 30 fps) vs high frame rate (48, 50, 60 fps)
70
+ - Video resolution (from 360p up to 8K)
71
+
72
+ The values are in kbps (kilobits per second).
73
+
74
+ Source: https://support.google.com/youtube/answer/1722171
75
+ """
76
+
77
+ # Configuration class
78
+ class Config:
79
+ """ Configuration class for the upscaler. """
80
+ JPG_QUALITY: int = 95
81
+ """ JPG quality for the output images. (Range: 0-100) """
82
+
83
+ VIDEO_FINAL_BITRATE: int = -1
84
+ """ Video final bitrate for the output video. -1 for YouTube recommended bitrate based on the video resolution. """
85
+
86
+ FFMPEG_EXECUTABLE: str = "ffmpeg"
87
+ """ Path to the ffmpeg executable, default is "ffmpeg" in the PATH. """
88
+
89
+ FFMPEG_ARGS: tuple[str, ...] = (
90
+ "-c:a", "copy", # Copy the audio without re-encoding
91
+ "-c:v", "hevc_nvenc", # Encode the video
92
+ "-map", "0:v:0", # Map the first input -i (frames) as video
93
+ "-map", "1:a:0?", # Map the second input -i (sound) as audio, with '?' to ignore if no audio stream
94
+ "-preset", "slow", # Set the encoding preset to slow (slower but better quality)
95
+ "-y", # Overwrite the output file if it exists
96
+ )
97
+ """ Additional arguments sent to the ffmpeg executable when calling subprocess.run(). """
98
+
99
+ FFPROBE_EXECUTABLE: str = "ffprobe"
100
+ """ Path to the ffprobe executable, default is "ffprobe" in the PATH. Used to get the framerate of the video. """
101
+
102
+ FFMPEG_CHECK_HELP_TEXT: str = "usage: ffmpeg [options]"
103
+ """ If this text is present in the output of the ffmpeg executable, it means it's installed correctly. """
104
+
105
+ UPSCALER_EXECUTABLE: str = "waifu2x-ncnn-vulkan"
106
+ """ Path to the upscaler executable, default is "waifu2x-ncnn-vulkan" in the PATH. """
107
+
108
+ UPSCALER_ARGS: tuple[str, ...] = (
109
+ "-i", "INPUT_PATH", # Input file path
110
+ "-o", "OUTPUT_PATH", # Output file path
111
+ "-s", "UPSCALE_RATIO", # Upscaling ratio
112
+ "-n", "3", # Noise level
113
+ )
114
+ """ Arguments sent to the upscaler executable when calling subprocess.run(). """
115
+
116
+ UPSCALER_EXECUTABLE_HELP_TEXT: str = "Usage: waifu2x-ncnn-vulkan -i"
117
+ """ If this text is present in the output of the upscaler executable, it means it's installed correctly. """
118
+
119
+ SLIGHTLY_FASTER_MODE: bool = False
120
+ """ If True, the upscaler executable will be called once, which is slightly faster but you can't see the progress. """
121
+
122
+ # Variables
123
+ upscaler_executable_checked: bool = False
124
+ """ Whether the upscaler executable has been checked for. """
125
+
126
+ ffmpeg_executable_checked: bool = False
127
+ """ Whether the ffmpeg executable has been checked for. """
128
+
@@ -0,0 +1,247 @@
1
+ """
2
+ This module provides utility functions for upscaling images using waifu2x-ncnn-vulkan.
3
+
4
+ It includes functions to upscale individual images, batches of images in a folder,
5
+ and handle intermediate operations like converting between image formats.
6
+ The module also manages temporary directories for partial processing and tracks
7
+ progress of batch operations.
8
+
9
+ Main functionalities:
10
+
11
+ - Converting frames between image formats (PNG to JPG)
12
+ - Upscaling individual images with configurable upscale ratio
13
+ - Batch processing folders of images with progress tracking
14
+ - Handling already processed images to resume interrupted operations
15
+
16
+ Example usage:
17
+
18
+ .. code-block:: python
19
+
20
+ from stouputils.applications.upscaler import upscale, upscale_folder
21
+
22
+ # Upscale a single image
23
+ upscale("input.jpg", "output.jpg", 2)
24
+
25
+ # Upscale a folder of images
26
+ upscale_folder("input_folder", "output_folder", 2)
27
+ """
28
+
29
+ # Imports
30
+ import os
31
+ import shutil
32
+ import subprocess
33
+ from tempfile import TemporaryDirectory
34
+
35
+ from PIL import Image
36
+
37
+ from ...installer import check_executable
38
+ from ...io import clean_path
39
+ from ...parallel import colored_for_loop, multithreading
40
+ from ...print import debug, info
41
+ from .config import WAIFU2X_NCNN_VULKAN_RELEASES, Config
42
+
43
+
44
+ # Function to convert a PNG frame to JPG format
45
+ def convert_frame(frame_path: str, delete_png: bool = True) -> None:
46
+ """ Convert a PNG frame to JPG format to take less space.
47
+
48
+ Args:
49
+ frame_path (str): Path to the PNG frame to convert.
50
+ delete_png (bool): Whether to delete the original PNG file after conversion.
51
+
52
+ Returns:
53
+ None: This function doesn't return anything.
54
+
55
+ Example:
56
+ .. code-block:: python
57
+
58
+ > convert_frame("input.png", delete_png=True)
59
+ > # input.png will be converted to input.jpg and the original file will be deleted
60
+
61
+ > convert_frame("input.png", delete_png=False)
62
+ > # input.png will be converted to input.jpg and the original file will be kept
63
+ """
64
+ if frame_path.endswith(".png"):
65
+ with Image.open(frame_path) as img:
66
+ img.save(frame_path.replace(".png", ".jpg"), quality=Config.JPG_QUALITY)
67
+ if delete_png:
68
+ os.remove(frame_path)
69
+
70
+
71
+ # Function to get all frames in a folder
72
+ def get_all_files(folder: str, suffix: str | tuple[str, ...] = "") -> list[str]:
73
+ """ Get all files paths in a folder, with a specific suffix if provided.
74
+
75
+ Args:
76
+ folder (str): Path to the folder containing the files.
77
+ suffix (str | tuple[str, ...]): Suffix of the files to get (e.g. ".png", ".jpg", etc.).
78
+
79
+ Returns:
80
+ list[str]: List of all files paths in the folder.
81
+
82
+ Example:
83
+ >>> files: list[str] = get_all_files("some_folder", ".png")
84
+ >>> len(files)
85
+ 0
86
+ """
87
+ if not os.path.exists(folder):
88
+ return []
89
+ return [f"{folder}/{f}" for f in os.listdir(folder) if f.endswith(suffix)]
90
+
91
+
92
+ # Function to create a temporary directory with not upscaled images
93
+ def create_temp_dir_for_not_upscaled(input_path: str, output_path: str) -> TemporaryDirectory[str] | None:
94
+ """ Creates a temporary directory containing only images that haven't been upscaled yet.
95
+
96
+ Args:
97
+ input_path (str): Path to the folder containing input images.
98
+ output_path (str): Path to the folder where upscaled images are saved.
99
+
100
+ Returns:
101
+ TemporaryDirectory[str] | None: A temporary directory object if there are images to process,
102
+ None if all images are already upscaled.
103
+ """
104
+ # Get all input images and the not upscaled images
105
+ all_inputs: list[str] = get_all_files(input_path)
106
+ not_upscaled_images: list[str] = [x for x in all_inputs if not os.path.exists(f"{output_path}/{os.path.basename(x)}")]
107
+
108
+ # If all images or none are already upscaled, return None
109
+ if len(not_upscaled_images) == 0 or (len(not_upscaled_images) == len(all_inputs)):
110
+ return None
111
+
112
+ # Create a temporary directory and copy the not upscaled images to it
113
+ temp_dir: TemporaryDirectory[str] = TemporaryDirectory()
114
+ debug(f"Creating temporary directory to process {len(not_upscaled_images)} images: {temp_dir.name}")
115
+ for image in not_upscaled_images:
116
+ shutil.copyfile(image, f"{temp_dir.name}/{os.path.basename(image)}")
117
+ return temp_dir
118
+
119
+
120
+ # Helper function to check if the upscaler executable is installed
121
+ def check_upscaler_executable() -> None:
122
+ if not Config.upscaler_executable_checked:
123
+ check_executable(Config.UPSCALER_EXECUTABLE, Config.UPSCALER_EXECUTABLE_HELP_TEXT, WAIFU2X_NCNN_VULKAN_RELEASES)
124
+ Config.upscaler_executable_checked = True
125
+
126
+
127
+ # Function to upscale an image
128
+ def upscale(input_path: str, output_path: str, upscale_ratio: int) -> None:
129
+ """ Upscale an input image (or a directory of images) with the upscaler executable.
130
+
131
+ Args:
132
+ input_path (str): Path to the image to upscale (or a directory).
133
+ output_path (str): Path to the output image (or a directory).
134
+ upscale_ratio (int): Upscaling ratio.
135
+
136
+ Example:
137
+ .. code-block:: python
138
+
139
+ > upscale("folder", "folder", 2)
140
+ Traceback (most recent call last):
141
+ ...
142
+ AssertionError: Input and output paths cannot be the same, got 'folder'
143
+
144
+ > upscale("stouputils", "stouputils/output.jpg", 2)
145
+ Traceback (most recent call last):
146
+ ...
147
+ AssertionError: If input is a directory, output must be a directory too, got 'stouputils/output.jpg'
148
+
149
+
150
+ > upscale("input.jpg", "output.jpg", 2)
151
+ > # The input.jpg will be upscaled to output.jpg with a ratio of 2
152
+
153
+ > upscale("input_folder", "output_folder", 2)
154
+ > # The input_folder will be upscaled to output_folder with a ratio of 2
155
+ """
156
+ check_upscaler_executable()
157
+ is_input_dir: bool = os.path.isdir(input_path)
158
+ is_output_dir: bool = os.path.isdir(output_path)
159
+
160
+ # Assertions
161
+ assert input_path != output_path, f"Input and output paths cannot be the same, got '{input_path}'"
162
+ invalid_dir_combination: bool = is_input_dir == True and is_output_dir == False # noqa: E712
163
+ assert not invalid_dir_combination, f"If input is a directory, output must be a directory too, got '{output_path}'"
164
+
165
+ # Convert output_path to a file path if it's a directory
166
+ if is_output_dir and not is_input_dir:
167
+
168
+ # Needs to be a PNG file to be converted to JPG later
169
+ output_file_name: str = os.path.basename(input_path).replace(".jpg", ".png")
170
+ output_path = clean_path(f"{output_path}/{output_file_name}")
171
+ is_output_dir = False
172
+
173
+ # If both input and output are folders, and there are images already upscaled in the output folder,
174
+ # Then create a temporary folder with not upscaled images
175
+ temp_dir: TemporaryDirectory[str] | None = None
176
+ if is_input_dir and is_output_dir:
177
+ temp_dir = create_temp_dir_for_not_upscaled(input_path, output_path)
178
+ if temp_dir:
179
+ input_path = temp_dir.name
180
+
181
+ # Build the command and run it
182
+ cmd: list[str] = [Config.UPSCALER_EXECUTABLE, *Config.UPSCALER_ARGS]
183
+ cmd[cmd.index("INPUT_PATH")] = input_path # Replace the input path
184
+ cmd[cmd.index("OUTPUT_PATH")] = output_path # Replace the output path
185
+ cmd[cmd.index("UPSCALE_RATIO")] = str(upscale_ratio) # Replace the upscaled ratio (if using waifu2x-ncnn-vulkan)
186
+ subprocess.run(cmd, capture_output=True)
187
+
188
+ # If the input was a temporary folder, delete it
189
+ if temp_dir:
190
+ temp_dir.cleanup()
191
+
192
+ # Convert the output frames to JPG format
193
+ if not is_output_dir:
194
+ convert_frame(output_path)
195
+ else:
196
+ frames_to_convert: list[str] = get_all_files(output_path, ".png")
197
+ if frames_to_convert:
198
+ multithreading(convert_frame, frames_to_convert, desc="Converting frames to JPG format")
199
+
200
+
201
+ # Function to upscale multiple images
202
+ def upscale_images(images: list[str], output_folder: str, upscale_ratio: int, desc: str = "Upscaling images") -> None:
203
+ """ Upscale multiple images from a list.
204
+
205
+ Args:
206
+ images (list[str]): List of paths to the images to upscale.
207
+ output_folder (str): Path to the output folder where the upscaled images will be saved.
208
+ upscale_ratio (int): Upscaling ratio.
209
+ desc (str): Description of the function execution displayed in the progress bar.
210
+ No progress bar will be displayed if desc is empty.
211
+
212
+ Returns:
213
+ None: This function doesn't return anything.
214
+ """
215
+ for image_path in (colored_for_loop(images, desc=desc) if desc != "" else images):
216
+ upscale(image_path, output_folder, upscale_ratio)
217
+
218
+ # Function to upscale a folder of images
219
+ def upscale_folder(
220
+ input_folder: str,
221
+ output_folder: str,
222
+ upscale_ratio: int,
223
+ slightly_faster_mode: bool = True,
224
+ desc: str = "Upscaling folder"
225
+ ) -> None:
226
+ """ Upscale all images in a folder.
227
+
228
+ Args:
229
+ input_folder (str): Path to the input folder containing the images to upscale.
230
+ output_folder (str): Path to the output folder where the upscaled images will be saved.
231
+ upscale_ratio (int): Upscaling ratio.
232
+ slightly_faster_mode (bool): Whether to use the slightly faster mode (no progress bar),
233
+ one call to the upscaler executable.
234
+ desc (str): Description of the function execution displayed in the progress bar.
235
+ No progress bar will be displayed if desc is empty.
236
+
237
+ Returns:
238
+ None: This function doesn't return anything.
239
+ """
240
+ info(f"Upscaling '{input_folder}' to '{output_folder}' with a ratio of {upscale_ratio}...")
241
+ if slightly_faster_mode:
242
+ upscale(input_folder, output_folder, upscale_ratio)
243
+ else:
244
+ inputs: list[str] = get_all_files(input_folder)
245
+ not_upscaled_images: list[str] = [x for x in inputs if not os.path.exists(f"{output_folder}/{os.path.basename(x)}")]
246
+ upscale_images(not_upscaled_images, output_folder, upscale_ratio, desc=desc)
247
+