# misc.py file contains miscellaneous utility functions import math import sys def pause(): """ Pauses the execution until any key is pressed. """ if sys.platform.startswith('win'): import msvcrt print("Press any key to continue...") msvcrt.getch() else: import termios import tty print("Press any key to continue...") fd = sys.stdin.fileno() old_settings = termios.tcgetattr(fd) try: tty.setraw(fd) sys.stdin.read(1) finally: termios.tcsetattr(fd, termios.TCSADRAIN, old_settings) def install(package): import subprocess subprocess.check_call([sys.executable, "-m", "pip", "install", package]) def get_filename(file): filename = None if file is not None: filename = file.name return filename def get_extension(file): extension = None if file is not None: extension = file.name.split(".")[-1] return extension def convert_ratio_to_dimensions(ratio, height=512, rotate90=False): """ Calculate pixel dimensions based on a given aspect ratio and base height. This function computes the width and height in pixels for an image, ensuring that both dimensions are divisible by 16. The height is adjusted upwards to the nearest multiple of 16 if necessary, and the width is calculated based on the adjusted height and the provided aspect ratio. Additionally, it ensures that both width and height are at least 16 pixels to avoid extremely small dimensions. Parameters: ratio (float): The aspect ratio of the image (width divided by height). height (int, optional): The base height in pixels. Defaults to 512. Returns: tuple: A tuple containing the calculated (width, height) in pixels, both divisible by 16. """ base_height = 512 # Scale the height based on the provided height parameter # Ensure the height is at least base_height scaled_height = max(height, base_height) # Adjust the height to be divisible by 16 adjusted_height = math.ceil(scaled_height / 16) * 16 # Calculate the width based on the ratio calculated_width = int(adjusted_height * ratio) # Adjust the width to be divisible by 16 adjusted_width = math.ceil(calculated_width / 16) * 16 if rotate90: adjusted_width, adjusted_height = adjusted_height, adjusted_width return adjusted_width, adjusted_height