📷 PyTorch 360° Image Conversion Toolkit

Overview
This PyTorch-based library provides powerful and differentiable image transformation utilities for converting between different panoramic image formats:
- Equirectangular (360°) Images
- Cubemap Representations
- Perspective Projections
Built as an improved PyTorch implementation of the original py360convert project, this library offers flexible, CPU & GPU-accelerated functions.
🔧 Requirements
📦 Installation
You can easily install the library using pip:
pip install pytorch360convert
Or you can install it from source like this:
pip install torch
Then clone the repository:
git clone https://github.com/ProGamerGov/pytorch360convert.git
cd pytorch360convert
pip install .
🚀 Key Features
- Lossless conversion between image formats.
- Supports different cubemap input formats (horizon, list, stack, dict, dice).
- Configurable sampling modes (bilinear, nearest).
- Supports different dtypes (float16, float32, float64, bfloat16).
- CPU support.
- GPU acceleration.
- Differentiable transformations for deep learning pipelines.
- TorchScript (JIT) support.
💡 Usage Examples
Helper Functions
First we'll setup some helper functions:
pip install torchvision pillow
import torch
from torchvision.transforms import ToTensor, ToPILImage
from PIL import Image
def load_image_to_tensor(image_path: str) -> torch.Tensor:
"""Load an image as a PyTorch tensor."""
return ToTensor()(Image.open(image_path).convert('RGB'))
def save_tensor_as_image(tensor: torch.Tensor, save_path: str) -> None:
"""Save a PyTorch tensor as an image."""
ToPILImage()(tensor).save(save_path)
Equirectangular to Cubemap Conversion
Converting equirectangular images into cubemaps is easy. For simplicity, we'll use the 'dice' format, which places all cube faces into a single 4x3 grid image.
from pytorch360convert import e2c
equi_image = load_image_to_tensor("examples/example_world_map_equirectangular.png")
face_w = equi_image.shape[2] // 4
cubemap = e2c(
equi_image,
face_w=face_w,
mode='bilinear',
cube_format='dice'
)
save_tensor_as_image(cubemap, "dice_cubemap.jpg")
Cubemap to Equirectangular Conversion
We can also convert cubemaps into equirectangular images, like so.
from pytorch360convert import c2e
cubemap = load_image_to_tensor("dice_cubemap.jpg")
equirectangular = c2e(
cubemap,
mode='bilinear',
cube_format='dice'
)
save_tensor_as_image(equirectangular, "equirectangular.jpg")
Equirectangular to Perspective Projection
from pytorch360convert import e2p
equi_image = load_image_to_tensor("examples/example_world_map_equirectangular.png")
perspective_view = e2p(
equi_image,
fov_deg=(70, 60),
h_deg=260,
v_deg=50,
out_hw=(512, 768),
mode='bilinear'
)
save_tensor_as_image(perspective_view, "perspective.jpg")
Equirectangular to Equirectangular
from pytorch360convert import e2e
equi_image = load_image_to_tensor("examples/example_world_map_equirectangular.png")
rotated_equi = e2e(
equi_image,
h_deg=90.0,
v_deg=200.0,
roll=45.0,
mode='bilinear'
)
save_tensor_as_image(rotated_equi, "rotated.jpg")
📚 Basic Functions
e2c(e_img, face_w=256, mode='bilinear', cube_format='dice')
Converts an equirectangular image to a cubemap projection.
c2e(cubemap, h, w, mode='bilinear', cube_format='dice')
Converts a cubemap projection to an equirectangular image.
- Parameters:
cubemap (torch.Tensor, list of torch.Tensor, or dict of torch.Tensor): Cubemap image tensor, list of tensors, or dict of tensors. Note that tensors should be in the shape of: CHW, except for when cube_format = 'stack', in which case a batch dimension is present. Inputs should match the corresponding cube_format.
h (int, optional): Output image height. If set to None, <cube_face_width> * 2 will be used. Default: None.
w (int, optional): Output image width. If set to None, <cube_face_width> * 4 will be used. Default: None.
mode (str, optional): Sampling interpolation mode. Options are bilinear, bicubic, and nearest. Default: bilinear
cube_format (str, optional): Input cubemap format. Options are dict, list, horizon, stack, and dice. Default: dice
stack (torch.Tensor): Stack of 6 faces, in the order of: ['Front', 'Right', 'Back', 'Left', 'Up', 'Down'].
list (list of torch.Tensor): List of 6 faces, in the order of: ['Front', 'Right', 'Back', 'Left', 'Up', 'Down'].
dict (dict of torch.Tensor): Dictionary with keys pointing to face tensors. Keys are expected to be: ['Front', 'Right', 'Back', 'Left', 'Up', 'Down'].
dice (torch.Tensor): A cubemap in a 'dice' layout.
horizon (torch.Tensor): A cubemap in a 'horizon' layout, a 1x6 grid in the order of: ['Front', 'Right', 'Back', 'Left', 'Up', 'Down'].
channels_first (bool, optional): Input cubemap channel format (CHW or HWC). Defaults to the PyTorch CHW standard of True.
- Returns: Equirectangular projection of the input cubemap as a tensor.
e2p(e_img, fov_deg, h_deg, v_deg, out_hw, in_rot_deg=0, mode='bilinear')
Extracts a perspective view from an equirectangular image.
e2e(e_img, h_deg, v_deg, roll=0, mode='bilinear')
Rotate an equirectangular image along one or more axes (roll, pitch, and yaw) to produce a horizontal shift, vertical shift, or to roll the image.
🤝 Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
🔬 Citation
If you use this library in your research or project, please refer to the included CITATION.cff file or cite it as follows:
BibTeX
@misc{egan2024pytorch360convert,
title={PyTorch 360° Image Conversion Toolkit},
author={Egan, Ben},
year={2024},
publisher={GitHub},
howpublished={\url{https://github.com/ProGamerGov/pytorch360convert}}
}
APA Style
Egan, B. (2024). PyTorch 360° Image Conversion Toolkit [Computer software]. GitHub. https://github.com/ProGamerGov/pytorch360convert