Spaces:
Running
on
Zero
Running
on
Zero
File size: 2,463 Bytes
31931d9 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
# 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
|