Advertisement
Python253

jpeg_byte_stuffing

May 27th, 2024 (edited)
611
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 7.36 KB | None | 0 0
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. # Filename: jpeg_byte_stuffing.py
  4. # Version: 1.0.0
  5. # Author: Jeoi Reqi
  6.  
  7. """
  8. Description:
  9.    This script provides functionality to create a blank JPEG image, perform byte stuffing with a smiley face pattern centered inside a circle, and compare the original and modified images.
  10.    It utilizes the Python Imaging Library (PIL) for image manipulation.
  11.  
  12. Functions:
  13.    - create_blank_jpeg(width, height, color, filename):
  14.        Creates a new blank JPEG image with the specified width, height, and color.
  15.    - compare_images(original_image, modified_image):
  16.        Compares two images and highlights their differences.
  17.    - byte_stuffing_with_smiley(original_filename, modified_filename):
  18.        Performs byte stuffing with a smiley face pattern centered inside a circle on the original image and saves the modified image.
  19.  
  20. Requirements:
  21.    - Python 3.x
  22.    - Python Imaging Library (PIL)
  23.  
  24. Usage:
  25.    - To use this script, simply run it in a Python environment.
  26.    - Ensure that the PIL library is installed.
  27.    - Modify the parameters in the 'main' function to customize the behavior of the script.
  28.  
  29. Expected Example Output:
  30.  
  31.    - Blank JPEG image created successfully!
  32.  
  33.    Original image saved as:
  34.                [original_img_test.jpeg]
  35.  
  36.    - Byte stuffing completed successfully!
  37.  
  38.    Modified image saved as:
  39.                [modified_img_test.jpeg]
  40.  
  41.    - Total bytes stuffed: 51
  42.  
  43.                [Images are different!]
  44.  
  45.    Difference found in pixels:
  46.    [(287, 377), (287, 378), (287, 379), (287, 380), (287, 381), (287, 382), ...]
  47.  
  48. Additional Notes:
  49.    - This script is designed to demonstrate basic image manipulation techniques and may require modifications for production use.
  50.    - Ensure that input image files exist in the specified locations before running the script.
  51. """
  52.  
  53. from PIL import Image, ImageDraw
  54. import math
  55.  
  56. def create_blank_jpeg(width, height, color, filename):
  57.     """
  58.    Create a new blank JPEG image with the specified width, height, and color.
  59.  
  60.    Parameters:
  61.        width (int): The width of the image.
  62.        height (int): The height of the image.
  63.        color (tuple): A tuple representing the RGB color of the image.
  64.        filename (str): The filename to save the image.
  65.  
  66.    Returns:
  67.        None
  68.    """
  69.     image = Image.new("RGB", (width, height), color)
  70.  
  71.     try:
  72.         image.save(filename)
  73.         print(
  74.             "\n- Blank JPEG image created successfully!\n\nOriginal image saved as:\n\t\t\t["
  75.             + f"{filename}]\n"
  76.         )
  77.     except Exception as e:
  78.         print(f"\nFailed to create blank JPEG image: {e}\n")
  79.  
  80. def compare_images(original_image, modified_image):
  81.     """
  82.    Compare two images and highlight their differences.
  83.  
  84.    Parameters:
  85.        original_image (str): The filename of the original image.
  86.        modified_image (str): The filename of the modified image.
  87.  
  88.    Returns:
  89.        None
  90.    """
  91.     try:
  92.         original = Image.open(original_image)
  93.         modified = Image.open(modified_image)
  94.     except Exception as e:
  95.         print(f"\nError opening images: {e}\n")
  96.         return
  97.  
  98.     if original.size == modified.size and original.mode == modified.mode:
  99.         diff = Image.new(original.mode, original.size)
  100.         diff_pixels = []
  101.         for x in range(original.width):
  102.             for y in range(original.height):
  103.                 if original.getpixel((x, y)) != modified.getpixel((x, y)):
  104.                     diff.putpixel((x, y), (255, 0, 0))  # Highlight differences in red
  105.                     diff_pixels.append((x, y))
  106.         diff.show()
  107.         if not diff_pixels:
  108.             print("\n\t\t\t[Images are identical!]\n")
  109.         else:
  110.             print(
  111.                 f"\n\t\t\t[Images are different!]\n\nDifference found in pixels:\n{diff_pixels}"
  112.             )
  113.     else:
  114.         print("\nImages have different sizes or modes!\n")
  115.  
  116. def byte_stuffing_with_smiley(original_filename, modified_filename):
  117.     """
  118.    Perform byte stuffing with a smiley face pattern centered inside a circle.
  119.  
  120.    Parameters:
  121.        original_filename (str): The filename of the original image.
  122.        modified_filename (str): The filename to save the modified image.
  123.  
  124.    Returns:
  125.        int: The total number of bytes stuffed in the image.
  126.    """
  127.     try:
  128.         original = Image.open(original_filename)
  129.     except FileNotFoundError:
  130.         print("\nOriginal image file not found!\n")
  131.         return None
  132.  
  133.     # Create a copy of the original image
  134.     modified = original.copy()
  135.     draw = ImageDraw.Draw(modified)
  136.  
  137.     # Define the RGB values for red color
  138.     red = (255, 0, 0)
  139.  
  140.     # Calculate the center coordinates for the circle
  141.     center_x = original.width // 2
  142.     center_y = original.height // 2
  143.     radius = 100  # Larger radius for the circle
  144.  
  145.     # Draw the circle
  146.     for angle in range(0, 360, 10):  # Reduced angle step for smoother circle
  147.         angle_rad = math.radians(angle)
  148.         x = int(center_x + radius * math.cos(angle_rad))
  149.         y = int(center_y + radius * math.sin(angle_rad))
  150.         draw.point((x, y), fill=red)
  151.  
  152.     # Define the byte stuffing locations for the smiley face pattern
  153.     smiley_face_locations = [
  154.         (center_x - 40, center_y - 30),
  155.         (center_x - 30, center_y - 30),  # Right eye
  156.         (center_x + 40, center_y - 30),
  157.         (center_x + 30, center_y - 30),  # Left Eye
  158.         (center_x - 60, center_y + 30),
  159.         (center_x + 60, center_y + 30),  # Dimples
  160.         (center_x - 45, center_y + 40),
  161.         (center_x - 40, center_y + 40),
  162.         (center_x - 30, center_y + 40),
  163.         (center_x - 20, center_y + 40),
  164.         (center_x - 10, center_y + 40),  # Left side mouth
  165.         (center_x,      center_y + 40),  # Center mouth
  166.         (center_x + 10, center_y + 40),
  167.         (center_x + 20, center_y + 40),
  168.         (center_x + 30, center_y + 40),
  169.         (center_x + 40, center_y + 40),
  170.         (center_x + 45, center_y + 40),  # Right side mouth
  171.     ]
  172.  
  173.     # Draw the smiley face pattern centered inside the circle
  174.     for x, y in smiley_face_locations:
  175.         draw.point((x, y), fill=red)
  176.  
  177.     # Calculate total bytes stuffed
  178.     total_bytes_stuffed = len(smiley_face_locations) * 3  # Each pixel is 3 bytes (RGB)
  179.  
  180.     # Save the modified image
  181.     modified.save(modified_filename)
  182.     print(
  183.         "- Byte stuffing completed successfully!\n\nModified image saved as:\n\t\t\t["
  184.         + f"{modified_filename}]"
  185.     )
  186.     print(f"\n- Total bytes stuffed: {total_bytes_stuffed}")
  187.  
  188.     return total_bytes_stuffed
  189.  
  190. def main():
  191.     """
  192.    Main function to create a blank JPEG image, perform byte stuffing with a smiley face pattern
  193.    inside a circle, and compare the original and modified images.
  194.    """
  195.     width = 800  # Doubled width
  196.     height = 800  # Doubled height
  197.     color = (255, 255, 255)  # White color
  198.     original_filename = "original_img_test.jpeg"
  199.     modified_filename = "modified_img_test.jpeg"
  200.  
  201.     # Create the blank JPEG image
  202.     create_blank_jpeg(width, height, color, original_filename)
  203.  
  204.     # Perform byte stuffing with a smiley face pattern inside a circle
  205.     byte_stuffing_with_smiley(original_filename, modified_filename)
  206.  
  207.     # Compare the original and modified images
  208.     compare_images(original_filename, modified_filename)
  209.  
  210. if __name__ == "__main__":
  211.     main()
  212.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement