diff options
author | Leonard Kugis <leonard@kug.is> | 2023-03-21 23:49:42 +0100 |
---|---|---|
committer | Leonard Kugis <leonard@kug.is> | 2023-03-21 23:49:42 +0100 |
commit | 7c635fed24bba638f10a73fe39134519ecd675bf (patch) | |
tree | 52f1097c02e64ea95fda92766e89d0f9b313b2ba /util.py | |
parent | 23ed1ff2254a3bf70aced1f10534e6dd1f2c3a3c (diff) |
Split util and tmsu functions
Diffstat (limited to 'util.py')
-rw-r--r-- | util.py | 79 |
1 files changed, 79 insertions, 0 deletions
@@ -0,0 +1,79 @@ +import subprocess +import logging +import cv2 +import platform +import readline +import os + +def image_resize(image, width = None, height = None, inter = cv2.INTER_AREA): + # initialize the dimensions of the image to be resized and + # grab the image size + dim = None + (h, w) = image.shape[:2] + + # if both the width and height are None, then return the + # original image + if width is None and height is None: + return image + + # check to see if the width is None + if width is None: + # calculate the ratio of the height and construct the + # dimensions + r = height / float(h) + dim = (int(w * r), height) + + # otherwise, the height is None + else: + # calculate the ratio of the width and construct the + # dimensions + r = width / float(w) + dim = (width, int(h * r)) + + # resize the image + resized = cv2.resize(image, dim, interpolation = inter) + + # return the resized image + return resized + +''' +Fetch input prompt with prefilled text. + +Parameters: +prompt: Prompt message. +text: Prefilled input. +''' +def input_with_prefill(prompt, text): + def hook(): + readline.insert_text(text) + readline.redisplay() + readline.set_pre_input_hook(hook) + result = input(prompt) + readline.set_pre_input_hook() + return result + +''' +Checks if the given string is a valid path. + +Parameters: +string: String to be checked. +''' +def dir_path(string): + if os.path.isdir(string): + return string + else: + raise NotADirectoryError(string) + +''' +Opens the given file with the platform default handler. + +Parameters: +file: Path to the file. +''' +def open_system(file): + if platform.system() == 'Darwin': # macOS + subprocess.call(('open', file)) + elif platform.system() == 'Windows': # Windows + os.startfile(file) + else: # linux variants + subprocess.call(('xdg-open', file))
\ No newline at end of file |