Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import numpy as np
- from PIL import Image
- import matplotlib.pyplot as plt
- from matplotlib.animation import FuncAnimation
- # Load the images
- original = Image.open('1.png')
- enhanced = Image.open('2.png')
- # Ensure both images are the same size
- assert original.size == enhanced.size, "Images must be the same size"
- # Get image dimensions
- width, height = original.size
- # Convert images to RGB mode
- original = original.convert('RGB')
- enhanced = enhanced.convert('RGB')
- # Convert images to numpy arrays
- original_array = np.array(original)
- enhanced_array = np.array(enhanced)
- # Calculate figure size (dividing by 100 to convert pixels to inches)
- figsize = (width/100, height/100)
- # Create the figure and axis
- fig, ax = plt.subplots(figsize=figsize)
- # Remove all margins
- plt.subplots_adjust(left=0, right=1, top=1, bottom=0)
- # Initialize the plot with the original image
- im = ax.imshow(original_array, aspect='auto')
- # Remove axis ticks and labels
- ax.set_xticks([])
- ax.set_yticks([])
- ax.axis('off')
- # Function to update the frame
- def update(frame):
- # Create a mask for the enhanced portion
- mask = np.zeros(original_array.shape[:2], dtype=bool)
- mask[:, :frame] = True
- # Combine the original and enhanced images
- combined = np.where(mask[:, :, np.newaxis], enhanced_array, original_array)
- # Update the image data
- im.set_array(combined)
- # Add a vertical line to show the split
- ax.clear() # Clear previous lines
- ax.imshow(combined, aspect='auto')
- ax.axvline(x=frame, color='white', linewidth=1)
- ax.set_xticks([])
- ax.set_yticks([])
- ax.axis('off')
- return [ax]
- # Create the animation
- anim = FuncAnimation(fig, update, frames=width, interval=50, blit=False)
- # Save the animation as a gif
- anim.save('comparison.gif', writer='pillow', fps=20, dpi=100)
- plt.close(fig)
- print("Animation saved as 'comparison.gif'")
Advertisement
Add Comment
Please, Sign In to add comment