Below is a Python script that performs the following tasks:
- Loads a grayscale image using PIL (Pillow).
- Applies a bitwise XOR operation to the entire image for each possible one-byte mask (values from 0 to 255).
- Displays the results in a 16x16 grid of subplots using Matplotlib (pyplot), where each subplot shows the image XORed with a specific mask.
Key Notes:
- Image Requirements: The script assumes you have a grayscale image file (e.g., PNG or JPG). If the image is not grayscale, it will be converted to grayscale.
- One-Byte Masks: These are integers from 0 to 255 (2^8 possibilities).
- Display: The results are shown in a grid. Each subplot displays one XORed image, with the mask value indicated in the title for clarity.
- Dependencies: You need to install the required libraries if you haven't already:
pip install pillow numpy matplotlib
- Performance: Processing and displaying 256 images might take some time and memory, depending on the image size. For larger images, you may need to resize them first.
Here's the code:
import numpy as np
from PIL import Image
import matplotlib.pyplot as plt
# Load the grayscale image
image_path = 'path_to_your_grayscale_image.png' # Replace with your image path (e.g., 'image.png')
try:
img = Image.open(image_path).convert('L') # 'L' mode ensures grayscale
img_array = np.array(img) # Convert to a numpy array for easy manipulation
except FileNotFoundError:
print(f"Error: Image file not found at {image_path}. Please check the path.")
exit()
# Create a 16x16 grid of subplots for the 256 masks (0 to 255)
fig, axes = plt.subplots(16, 16, figsize=(16, 16)) # Adjust figsize if needed for better visibility
for i in range(16):
for j in range(16):
mask = i * 16 + j # Generate mask values from 0 to 255
xor_image = img_array ^ mask # Perform bitwise XOR on the entire image array
# Display the XORed image in the corresponding subplot
axes[i, j].imshow(xor_image, cmap='gray') # Use 'gray' colormap for grayscale images
axes[i, j].set_title(f'Mask: {mask}') # Optional: Show the mask value in the title
axes[i, j].axis('off') # Hide axes for a cleaner look
plt.tight_layout() # Adjust the layout to prevent overlapping
plt.suptitle('XOR of Grayscale Image with All 256 One-Byte Masks', fontsize=16) # Main title for the figure
plt.show()
How to Run the Code:
- Replace
'path_to_your_grayscale_image.png' with the actual path to your grayscale image file.
- Save the script to a file (e.g.,
xor_image.py).
- Run it in your Python environment:
python xor_image.py
- A window will pop up displaying the 16x16 grid of images.
Explanation of the Code:
- Loading the Image:
Image.open() loads the image, and .convert('L') ensures it's in grayscale mode (luminance).
- Numpy Array: The image is converted to a numpy array for efficient bitwise operations. The XOR operation (
img_array ^ mask) is applied element-wise to every pixel.
- Loop Through Masks: The nested loops iterate over the 256 masks. For each mask, a new image is created and displayed in the grid.
- Subplots:
plt.subplots(16, 16) creates a 2D array of axes. Each XORed image is plotted on one axis.
- Bitwise XOR: This operation flips the bits of each pixel value based on the mask. For example, if a pixel is 100 (binary: 01100100) and the mask is 5 (binary: 00000101), the result is 101 (binary: 01100001).
- Display Options:
cmap='gray' ensures the images are displayed in grayscale. axis('off') removes unnecessary axes lines.
If you encounter any issues (e.g., with image loading or display), feel free to provide more details!