CoolFace
Apppublic

Graduation-Proect-Team/Text_Driven_Image_to_Image_Generation

sourceHugging Facemitupdated 2y agoView on Hugging Face
2likes
Helper_functions.py86 linesDownload Raw Back to root
1import shutil2import subprocess3import os4import json5import pytz6import random7from datetime import datetime8from PIL import Image9 10 11def execute_terminal_command(command: str):12    try:13        process = subprocess.Popen(14            command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE15        )16        output, error = process.communicate()17        output = str(output.decode("utf-8"))18        print(rf"Command executed successfully: {command}")19        return output20    except Exception as e:21        return None, str(e)22 23 24def correct_path(path: str):25    return path[1:] if path.startswith("\\") else path26 27 28def write_file(data_list: list, file_path: str, file_name: str = ""):29    if len(file_name) > 0:30        file_path = rf"{file_path}\{file_name}"31 32    # Writing JSON data33    with open(file_path, "w") as file:34        json.dump(data_list, file)35 36 37def read_file(file_path: str):38    with open(file_path, 'r') as file:39        data = json.load(file)40    return data41 42 43def copy_file(source_path: str, destination_path: str):44    try:45        shutil.copyfile(source_path, destination_path)46    except Exception as e:47        print(rf"Failed to copy {source_path} to {destination_path}")48        raise e49 50 51def read_image(image_path: str):52    try:53        image = Image.open(image_path)54        return image55    except IOError:56        print("Unable to load image")57        return None58 59 60def get_current_time():61    # Get the current datetime in UTC timezone62    current_datetime_utc = datetime.now(pytz.utc)63    # Convert UTC datetime to Egypt timezone64    egypt_timezone = pytz.timezone("Africa/Cairo")65    current_datetime_local = current_datetime_utc.astimezone(egypt_timezone)66 67    return str(current_datetime_local.strftime("%Y-%m-%d %H:%M:%S %Z"))68 69 70def get_random_str(sz: int):71    result: str = ""72    while len(result) < sz:73        result += str(random.randint(0, 9))74 75    return result76 77 78def create_folder(path: str, Replace_if_exist = True):79    try:80        if Replace_if_exist and os.path.exists(path):81            shutil.rmtree(path)82 83        os.makedirs(path, exist_ok=False)84        print(f"Folder '{path}' created successfully.")85    except Exception as e:86        print(f"Failed to create folder '{path}'. Error: {e}")