Advertisement
Mark2020H

Extracting data from and image with data embededd Python3

Sep 12th, 2024 (edited)
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.51 KB | Source Code | 0 0
  1. #!/usr/bin/env python3
  2.  
  3. # The decode  py for the extracting data  from an image embedded with data
  4.  
  5.  
  6. # MD Harrington Kent London UK 12-09-2024 Time 1:56
  7. # Facebook https://www.facebook.com/mark.harrington.14289
  8.  
  9. # Now Improved  much more  This is just so cool
  10.  
  11. # Instagram https://www.instagram.com/markukh2021/
  12.  
  13. # Homesite (Still  busy with this at present ) https://eliteprojects.x10host.com/
  14.  
  15. # Github   https://github.com/markh2024   && https://github.com/markh2016
  16.  
  17. # Codeshare https://codeshare.io/codes
  18.  
  19.  
  20. # To most this would appear as a graph but ,  ha , ha  Alas it is not and so  I  win again, eh , eh , eh
  21. # Who would never even think anything of this  ?   Not to many by the looks of this
  22.  
  23.  
  24.  
  25.  
  26. from PIL import Image, PngImagePlugin
  27. import matplotlib.pyplot as plt
  28. import time
  29. import datetime
  30. import os  # For clearing the screen
  31. import platform  # To detect the OS
  32.  
  33. # Function to clear the screen based on the OS
  34. def clear_screen():
  35.     # Detect the OS and clear screen appropriately
  36.     if platform.system() == "Windows":
  37.         os.system('cls')
  38.     else:
  39.         os.system('clear')
  40.  
  41. def slow_print(text, color=None):
  42.     if color:
  43.         print(f"\033[{color}m", end="")  # Set the color (e.g., '91' for red)
  44.     for char in text:
  45.         print(char, end='', flush=True)
  46.         time.sleep(0.05)  # Slow print effect
  47.     if color:
  48.         print("\033[0m", end="")  # Reset color to default after text
  49.     print()  # New line after the text
  50.  
  51. # Function to read ASCII values from image metadata and reconstruct the original sentence
  52. def read_ascii_from_image(image_path):
  53.     try:
  54.         # Open the image and access the metadata
  55.         img = Image.open(image_path)
  56.  
  57.         # Display the image
  58.         plt.imshow(img)
  59.         plt.axis('off')  # Hide axis for better display
  60.         plt.title("Loaded Image with Embedded data")
  61.         plt.show()
  62.  
  63.         # Extract metadata
  64.         metadata = img.info
  65.  
  66.         # Extract the ASCII values stored as metadata
  67.         if "ASCII Values" in metadata:
  68.             ascii_values_str = metadata["ASCII Values"]
  69.             slow_print(f"Extracted data values: {ascii_values_str}", color="45")
  70.            
  71.             # Convert the ASCII values string back into a list of integers
  72.             ascii_values = list(map(int, ascii_values_str.split(',')))
  73.            
  74.             # Reconstruct the original message from the ASCII values
  75.             original_message = ''.join(chr(value) for value in ascii_values)
  76.             slow_print(f"Reconstructed message: {original_message}", color="96")
  77.         else:
  78.             slow_print("No ASCII values found in the image metadata.", color="90")
  79.    
  80.     except FileNotFoundError:
  81.         print(f"Error: Image file '{image_path}' not found.")
  82.     except Exception as e:
  83.         print(f"An error occurred: {e}")
  84.  
  85. if __name__ == "__main__":
  86.     current_time = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
  87.     slow_print(f"\n\nProgram that decodes the image thank you and now it's easy. There you go, MD Harrington. {current_time}", color="92")
  88.     # Provide the path to the image that contains the embedded ASCII values
  89.     image_path = "ascplot.png"  # Make sure the image path is correct
  90.     read_ascii_from_image(image_path)
  91.     slow_print(f"\n\nProgram exited gracefully, thank you MD Harrington. {current_time}", color="92")
  92.     time.sleep(3)
  93.     clear_screen()
  94.  
  95.  
  96. ##  Enjoy  as I have many more ideas  to put into place It pays to listen  to some people  and now  you see why
  97.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement