Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python
- # -*- coding: utf-8 -*-
- # Filename: jpeg_byte_stuffing.py
- # Version: 1.0.0
- # Author: Jeoi Reqi
- """
- Description:
- 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.
- It utilizes the Python Imaging Library (PIL) for image manipulation.
- Functions:
- - create_blank_jpeg(width, height, color, filename):
- Creates a new blank JPEG image with the specified width, height, and color.
- - compare_images(original_image, modified_image):
- Compares two images and highlights their differences.
- - byte_stuffing_with_smiley(original_filename, modified_filename):
- Performs byte stuffing with a smiley face pattern centered inside a circle on the original image and saves the modified image.
- Requirements:
- - Python 3.x
- - Python Imaging Library (PIL)
- Usage:
- - To use this script, simply run it in a Python environment.
- - Ensure that the PIL library is installed.
- - Modify the parameters in the 'main' function to customize the behavior of the script.
- Expected Example Output:
- - Blank JPEG image created successfully!
- Original image saved as:
- [original_img_test.jpeg]
- - Byte stuffing completed successfully!
- Modified image saved as:
- [modified_img_test.jpeg]
- - Total bytes stuffed: 51
- [Images are different!]
- Difference found in pixels:
- [(287, 377), (287, 378), (287, 379), (287, 380), (287, 381), (287, 382), ...]
- Additional Notes:
- - This script is designed to demonstrate basic image manipulation techniques and may require modifications for production use.
- - Ensure that input image files exist in the specified locations before running the script.
- """
- from PIL import Image, ImageDraw
- import math
- def create_blank_jpeg(width, height, color, filename):
- """
- Create a new blank JPEG image with the specified width, height, and color.
- Parameters:
- width (int): The width of the image.
- height (int): The height of the image.
- color (tuple): A tuple representing the RGB color of the image.
- filename (str): The filename to save the image.
- Returns:
- None
- """
- image = Image.new("RGB", (width, height), color)
- try:
- image.save(filename)
- print(
- "\n- Blank JPEG image created successfully!\n\nOriginal image saved as:\n\t\t\t["
- + f"{filename}]\n"
- )
- except Exception as e:
- print(f"\nFailed to create blank JPEG image: {e}\n")
- def compare_images(original_image, modified_image):
- """
- Compare two images and highlight their differences.
- Parameters:
- original_image (str): The filename of the original image.
- modified_image (str): The filename of the modified image.
- Returns:
- None
- """
- try:
- original = Image.open(original_image)
- modified = Image.open(modified_image)
- except Exception as e:
- print(f"\nError opening images: {e}\n")
- return
- if original.size == modified.size and original.mode == modified.mode:
- diff = Image.new(original.mode, original.size)
- diff_pixels = []
- for x in range(original.width):
- for y in range(original.height):
- if original.getpixel((x, y)) != modified.getpixel((x, y)):
- diff.putpixel((x, y), (255, 0, 0)) # Highlight differences in red
- diff_pixels.append((x, y))
- diff.show()
- if not diff_pixels:
- print("\n\t\t\t[Images are identical!]\n")
- else:
- print(
- f"\n\t\t\t[Images are different!]\n\nDifference found in pixels:\n{diff_pixels}"
- )
- else:
- print("\nImages have different sizes or modes!\n")
- def byte_stuffing_with_smiley(original_filename, modified_filename):
- """
- Perform byte stuffing with a smiley face pattern centered inside a circle.
- Parameters:
- original_filename (str): The filename of the original image.
- modified_filename (str): The filename to save the modified image.
- Returns:
- int: The total number of bytes stuffed in the image.
- """
- try:
- original = Image.open(original_filename)
- except FileNotFoundError:
- print("\nOriginal image file not found!\n")
- return None
- # Create a copy of the original image
- modified = original.copy()
- draw = ImageDraw.Draw(modified)
- # Define the RGB values for red color
- red = (255, 0, 0)
- # Calculate the center coordinates for the circle
- center_x = original.width // 2
- center_y = original.height // 2
- radius = 100 # Larger radius for the circle
- # Draw the circle
- for angle in range(0, 360, 10): # Reduced angle step for smoother circle
- angle_rad = math.radians(angle)
- x = int(center_x + radius * math.cos(angle_rad))
- y = int(center_y + radius * math.sin(angle_rad))
- draw.point((x, y), fill=red)
- # Define the byte stuffing locations for the smiley face pattern
- smiley_face_locations = [
- (center_x - 40, center_y - 30),
- (center_x - 30, center_y - 30), # Right eye
- (center_x + 40, center_y - 30),
- (center_x + 30, center_y - 30), # Left Eye
- (center_x - 60, center_y + 30),
- (center_x + 60, center_y + 30), # Dimples
- (center_x - 45, center_y + 40),
- (center_x - 40, center_y + 40),
- (center_x - 30, center_y + 40),
- (center_x - 20, center_y + 40),
- (center_x - 10, center_y + 40), # Left side mouth
- (center_x, center_y + 40), # Center mouth
- (center_x + 10, center_y + 40),
- (center_x + 20, center_y + 40),
- (center_x + 30, center_y + 40),
- (center_x + 40, center_y + 40),
- (center_x + 45, center_y + 40), # Right side mouth
- ]
- # Draw the smiley face pattern centered inside the circle
- for x, y in smiley_face_locations:
- draw.point((x, y), fill=red)
- # Calculate total bytes stuffed
- total_bytes_stuffed = len(smiley_face_locations) * 3 # Each pixel is 3 bytes (RGB)
- # Save the modified image
- modified.save(modified_filename)
- print(
- "- Byte stuffing completed successfully!\n\nModified image saved as:\n\t\t\t["
- + f"{modified_filename}]"
- )
- print(f"\n- Total bytes stuffed: {total_bytes_stuffed}")
- return total_bytes_stuffed
- def main():
- """
- Main function to create a blank JPEG image, perform byte stuffing with a smiley face pattern
- inside a circle, and compare the original and modified images.
- """
- width = 800 # Doubled width
- height = 800 # Doubled height
- color = (255, 255, 255) # White color
- original_filename = "original_img_test.jpeg"
- modified_filename = "modified_img_test.jpeg"
- # Create the blank JPEG image
- create_blank_jpeg(width, height, color, original_filename)
- # Perform byte stuffing with a smiley face pattern inside a circle
- byte_stuffing_with_smiley(original_filename, modified_filename)
- # Compare the original and modified images
- compare_images(original_filename, modified_filename)
- if __name__ == "__main__":
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement