Spaces:
Runtime error
Runtime error
| import cv2 | |
| import os | |
| def create_panorama(image_paths, output_file): | |
| """ | |
| Stitches multiple images into a panorama. | |
| Args: | |
| image_paths (list): List of paths to the input images. | |
| output_file (str): Path to save the output panorama image. | |
| Returns: | |
| np.ndarray: The stitched panorama image, or None if stitching fails. | |
| """ | |
| # Load images and handle missing files | |
| images = [] | |
| for path in image_paths: | |
| img = cv2.imread(path) | |
| if img is not None: | |
| images.append(img) | |
| else: | |
| print(f"Warning: Cannot read {path}. Skipping this image.") | |
| # Check if there are enough images for stitching | |
| if len(images) < 2: | |
| print("Error: Not enough images to create a panorama. At least 2 are required.") | |
| return None | |
| # Create a Stitcher object | |
| stitcher = cv2.Stitcher_create() | |
| # Stitch images together | |
| status, panorama = stitcher.stitch(images) | |
| # Handle the result of stitching | |
| if status == cv2.Stitcher_OK: | |
| print("Stitching successful. Saving panorama...") | |
| cv2.imwrite(output_file, panorama) | |
| print(f"Panorama saved as {output_file}") | |
| return panorama | |
| else: | |
| print(f"Error: Stitching failed with status code {status}.") | |
| return None | |