Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python3
- # You will need to install the Lato-Medium.ttf font on this
- # To do this use https://fonts.google.com/specimen/Lato?query=Lato and download font family
- # This will downlaod a zip
- # Open this zip folder and extract then goto that folder and double click the Lato-Medium.ttf file
- # This will open system font viewer and within this is and install button
- # Install this font for system wide all users
- # MD Harrington 20/07/23 London UK Kent
- import os
- import colorama
- from colorama import Fore, Style
- import time
- from PIL import Image, ImageDraw, ImageFont
- import tkinter as tk
- from PIL import ImageTk
- class Tree_Creator:
- @staticmethod
- def list_files(folder_path, indent="", file=None):
- output_text = ""
- for item in os.listdir(folder_path):
- item_path = os.path.join(folder_path, item)
- if os.path.isdir(item_path):
- output_text += (indent + "|-- " + item + "/\n")
- Tree_Creator.typewriter_print(indent + "|-- " + item + "/\n", color=Fore.YELLOW)
- file.write(indent + "|-- " + item + "/\n")
- output_text += Tree_Creator.list_files(item_path, indent + " ", file)
- else:
- output_text += (indent + "|-- " + item + "\n")
- Tree_Creator.typewriter_print(indent + "|-- " + item + "\n", color=Fore.RED)
- file.write(indent + "|-- " + item + "\n")
- return output_text
- @staticmethod
- def main():
- # Prompt user for folder name
- Tree_Creator.typewriter_print("Enter a folder name: ", color=Fore.CYAN)
- folder_name = input(": ")
- # Search for the folder in the user's home area
- home_directory = os.path.expanduser("~")
- folder_path = None
- for root, dirs, files in os.walk(home_directory):
- if folder_name in dirs:
- folder_path = os.path.join(root, folder_name)
- break
- # Verify if the folder exists
- if folder_path is None:
- print("Folder not found.")
- exit(1)
- # Change to the directory
- os.chdir(folder_path)
- # Save folder structure to file
- output_filename = "Project_Tree.txt"
- with open(output_filename, "w") as file:
- file.write("Folder Structure:\n")
- output_text = Tree_Creator.list_files(folder_path, file=file)
- # Create and display the image
- Tree_Creator.create_image(output_text, "folder_structure.png")
- # Print folder structure saved message
- Tree_Creator.typewriter_print("Folder structure saved to: ", color=Fore.GREEN)
- Tree_Creator.typewriter_print(os.path.abspath(output_filename), color=Fore.CYAN)
- @staticmethod
- def typewriter_print(text, color=Fore.WHITE, delay=0.04):
- for char in text:
- print(color + char, end='', flush=True)
- time.sleep(delay)
- print(Style.RESET_ALL)
- @staticmethod
- def create_image(text, output_filename):
- # Set the font properties
- font_size = 20 # Updated font size
- line_spacing = 10 # Extra line spacing
- font_path = "/usr/share/fonts/truetype/lato/Lato-Medium.ttf"
- font = ImageFont.truetype(font_path, font_size)
- # Create a sample image to calculate line height and maximum line width
- sample_image = Image.new("RGB", (1, 1))
- sample_draw = ImageDraw.Draw(sample_image)
- line_height = sample_draw.textbbox((0, 0), "A", font=font)[3] # Calculate line height based on a sample character
- line_height_with_spacing = line_height + line_spacing # Line height with extra spacing
- max_line_width = max(sample_draw.textbbox((0, 0), line, font=font)[2] for line in text.split("\n")) # Calculate maximum line width
- # Calculate the image size based on the text
- image_width = int(max_line_width) + 20
- image_height = int(line_height_with_spacing) * len(text.split("\n")) + 20
- # Create a new image
- image = Image.new("RGB", (image_width, image_height), (0, 0, 255))
- draw = ImageDraw.Draw(image)
- # Draw the text on the image with extra line spacing
- y = 10
- for line in text.split("\n"):
- draw.text((10, y), line, fill=(0, 0, 0), font=font)
- y += line_height_with_spacing
- # Display the image in a GUI window
- root = tk.Tk()
- root.title("Image Viewer")
- img = ImageTk.PhotoImage(Image.open(output_filename))
- panel = tk.Label(root, image=img)
- panel.pack()
- root.mainloop()
- # Run the main method if executed as a script
- if __name__ == '__main__':
- colorama.init()
- Tree_Creator.main() # RcvSerHDF5
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement