Created
October 29, 2023 17:56
-
-
Save alekrutkowski/b11bee95aebfdb18f1fed9570bc3ec0c to your computer and use it in GitHub Desktop.
Panoramic Image (JPEG) to panning video (MP4) converter
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Made with ChatGPT | |
| # Required packages: | |
| # - Pillow | |
| # - imageio | |
| # - imageio-ffmpeg | |
| # - numpy | |
| # Usage example: | |
| # create_panning_video('My_Pano_image.jpg', 'My_Pano_video.mp4', 90, 60) | |
| from PIL import Image | |
| import imageio | |
| import numpy as np | |
| def create_panning_video(input_image_path, output_video_path, duration_in_seconds, fps): | |
| # Load the image using Pillow | |
| image = Image.open(input_image_path) | |
| image.load() # Load pixel data into memory | |
| # Calculate new width while maintaining the aspect ratio | |
| aspect_ratio = image.width / image.height | |
| new_width = int(1080 * aspect_ratio) | |
| # Resize the image using the LANCZOS filter (equivalent to older ANTIALIAS) | |
| image = image.resize((new_width, 1080), Image.Resampling.LANCZOS) | |
| # Image dimensions after resizing | |
| width, height = image.size | |
| # Define panning speed | |
| total_frames = duration_in_seconds * fps | |
| frames_per_direction = total_frames // 2 | |
| total_distance = width - 1920 | |
| pan_speed = total_distance / frames_per_direction | |
| # Open video writer using imageio | |
| with imageio.get_writer(output_video_path, fps=fps) as writer: | |
| # Panning from left to right | |
| for frame_num in range(frames_per_direction): | |
| x_offset = int(frame_num * pan_speed) | |
| frame = image.crop((x_offset, 0, x_offset + 1920, 1080)) | |
| writer.append_data(np.asarray(frame)) | |
| # Panning from right to left | |
| for frame_num in range(frames_per_direction): | |
| x_offset = int((frames_per_direction - frame_num) * pan_speed) | |
| frame = image.crop((x_offset, 0, x_offset + 1920, 1080)) | |
| writer.append_data(np.asarray(frame)) | |
| print("Video creation completed!") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment