Advertisement
Mark2020H

Python 3 program that gives you a graph x , y of ascii text input

Sep 12th, 2024 (edited)
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 6.07 KB | Source Code | 0 0
  1. #!/usr/bin/env python3
  2. # MD Harrington Kent London UK 12-09-2024 Time 1:56
  3. # Facebook https://www.facebook.com/mark.harrington.14289
  4.  
  5. # Now Improved  much more  This is just so cool
  6.  
  7. # Instagram https://www.instagram.com/markukh2021/
  8.  
  9. # Homesite (Still  busy with this at present ) https://eliteprojects.x10host.com/
  10.  
  11. # Github   https://github.com/markh2024   && https://github.com/markh2016
  12.  
  13. # Codeshare https://codeshare.io/codes
  14.  
  15. # To most this would appear as a graph but ,  ha , ha  Alas it is not and so  I  win again, eh , eh , eh
  16. # Who would never even think anything of this  ?
  17.  
  18. # Any one thought of this ?  
  19. # Send a graph  to who you wish  to communicate in private with and  extract the real hidden code within
  20.  
  21. # Have  fun ,  I wont expand on what else you can do with this yet  , Im  going to let  you think
  22.  
  23. #!/usr/bin/env python3
  24. # MD Harrington Kent London UK 12-09-2024 Time 1:56
  25.  
  26. import time
  27. import matplotlib.pyplot as plt
  28. import numpy as np
  29. import datetime
  30. import os  # For clearing the screen
  31. import platform  # To detect the OS
  32. from PIL import Image, PngImagePlugin  # For embedding metadata in the image
  33.  
  34. # Function to print text in a slow typewriter style with color support
  35. def slow_print(text, color=None):
  36.     if color:
  37.         print(f"\033[{color}m", end="")  # Set the color (e.g., '91' for red)
  38.     for char in text:
  39.         print(char, end='', flush=True)
  40.         time.sleep(0.05)  # Slow print effect
  41.     if color:
  42.         print("\033[0m", end="")  # Reset color to default after text
  43.     print()  # New line after the text
  44.  
  45. # Function to plot the graph of ASCII values of a sentence, save as an image, and embed metadata
  46. def plot_ascii_graph(sentence):
  47.     ascii_values = [ord(char) for char in sentence]
  48.     x = np.arange(len(ascii_values))  # X-axis corresponds to sentence length
  49.     y = np.array(ascii_values)
  50.  
  51.     fig, ax = plt.subplots()
  52.     line, = ax.plot(x, y, 'r-', linewidth=1)  # Plot with red line, width=1
  53.     ax.set_title('ASCII Value Plot of the Sentence MD Harrington Kent London')
  54.     ax.set_xlabel('Character Position')
  55.     ax.set_ylabel('ASCII Value')
  56.     ax.set_ylim([0, 127])  # Y-axis max value is 127 (ASCII max value)
  57.     ax.grid(True)
  58.  
  59.     # Add a movable crosshair
  60.     vertical_line = ax.axvline(color='k', linestyle='--')  # Vertical crosshair
  61.     horizontal_line = ax.axhline(color='k', linestyle='--')  # Horizontal crosshair
  62.  
  63.     # Fixed label near X-axis title (right above 'Character Position')
  64.     label = ax.text(0.2, 0.1, "", color="blue", fontsize=12, ha='center',
  65.                     transform=ax.transAxes)
  66.  
  67.     def on_move(event):
  68.         if event.inaxes == ax:
  69.             # Update the position of the crosshair
  70.             vertical_line.set_xdata(event.xdata)
  71.             horizontal_line.set_ydata(event.ydata)
  72.             plt.draw()
  73.  
  74.     def on_click(event):
  75.         if event.inaxes == ax:
  76.             # Find the nearest data point
  77.             index = int(round(event.xdata))
  78.             if 0 <= index < len(sentence):
  79.                 character = sentence[index]
  80.                 label.set_text(f"'{character}' at index {index}")
  81.                 plt.draw()
  82.  
  83.     # Connect the mouse events to their handlers
  84.     fig.canvas.mpl_connect('motion_notify_event', on_move)
  85.     fig.canvas.mpl_connect('button_press_event', on_click)
  86.  
  87.     plt.show()
  88.  
  89.     # Save the plot as an image file (ascplot.png)
  90.     img_filename = "ascplot.png"
  91.     fig.savefig(img_filename, dpi=300)
  92.     print("Graph saved as ascplot.png with ASCII metadata. Please wait 2 seconds.")
  93.     time.sleep(2)
  94.  
  95.     # Embed ASCII values into the image metadata
  96.     img = Image.open(img_filename)
  97.     meta = PngImagePlugin.PngInfo()
  98.  
  99.     # Convert ASCII values to a comma-separated string and add as metadata
  100.     ascii_values_str = ','.join(map(str, ascii_values))
  101.     meta.add_text("ASCII Values", ascii_values_str)
  102.     img.save(img_filename, "PNG", pnginfo=meta)
  103.  
  104. # Function to display the menu
  105. def display_menu():
  106.     slow_print("The Program allows me to send a graph to anyone without them knowing what that message is \n", color="93")
  107.     slow_print("1: Provide a sentence of no longer than 20 words", color="91")
  108.     slow_print("2: Plot a graph of the sentence you provided", color="92")
  109.     slow_print("3: Quit", color="93")
  110.  
  111. # Function to clear the screen based on the OS
  112. def clear_screen():
  113.     # Detect the OS and clear screen appropriately
  114.     if platform.system() == "Windows":
  115.         os.system('cls')
  116.     else:
  117.         os.system('clear')
  118.  
  119. def main():
  120.     sentence = ""
  121.     while True:
  122.         clear_screen()  # Clear the screen before showing the menu
  123.         display_menu()
  124.         choice = input("Enter your choice (1-3): ")
  125.        
  126.         if choice == '1':
  127.             while True:
  128.                 user_input = input("Please enter a sentence (max 20 words): ")
  129.                 word_count = len(user_input.split())
  130.                 if word_count > 20:
  131.                     print(f"Your sentence has {word_count} words, which exceeds the 20-word limit.")
  132.                     retry = input("Do you want to re-enter the sentence? (y/n): ")
  133.                     if retry.lower() != 'y':
  134.                         break
  135.                 else:
  136.                     sentence = user_input
  137.                     slow_print(f"Your sentence has {word_count} words.", color="91")
  138.                     break
  139.        
  140.         elif choice == '2':
  141.             if sentence:
  142.                 slow_print(sentence, color="92")
  143.                 plot_ascii_graph(sentence)  # Plot the graph, save it, and embed metadata
  144.             else:
  145.                 slow_print("No sentence provided. Please choose option 1 first.", color="94")
  146.  
  147.         elif choice == '3':
  148.             current_time = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
  149.             slow_print(f"Program exited gracefully, thank you MD Harrington. {current_time}", color="92")
  150.             time.sleep(2)
  151.             clear_screen()
  152.             break
  153.  
  154.         else:
  155.             print("Invalid choice. Please select again.")
  156.  
  157. if __name__ == "__main__":
  158.     main()
  159.  
  160.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement