Install py360convert
masterInstall the library using pip. The project depends on numpy and scipy. If opencv is installed in your environment, py360convert will automatically use it to accelerate computations.
pip install py360convertrepository·master·Indexed 20 days ago
https://github.com/sunset1995/py360convertA pure Python library for converting between 360-degree image formats, including Equirectangular, Cubemap (dice, horizon, dict, or list layouts), and Perspective views. It utilizes NumPy and SciPy, with optional OpenCV acceleration. The package provides a Python API featuring functions like c2e(), e2c(), and e2p(), as well as a convert360 command-line interface for performing these conversions.
Install the library using pip. The project depends on numpy and scipy. If opencv is installed in your environment, py360convert will automatically use it to accelerate computations.
pip install py360convertUse py360convert.e2p() to extract a standard perspective (planar) view from an equirectangular image.
Parameters:
e_img: Numpy array with shape [H, W, C].fov_deg: Field of view. Can be an int or a tuple (h_fov_deg, v_fov_deg).u_deg: Horizontal viewing angle in range [-pi, pi] (- Left / + Right).v_deg: Vertical viewing angle in range [-pi/2, pi/2] (- Down / + Up).out_hw: Output image dimensions as a tuple (height, width).in_rot_deg: In-plane rotation.mode: Interpolation method ('bilinear' or 'nearest').import py360convert
# Example usage (conceptual)
py360convert.e2p(e_img, fov_deg=90, u_deg=0, v_deg=0, out_hw=(400, 600))Use py360convert.e2c() to convert an equirectangular image into a cubemap.
Parameters:
e_img: Numpy array with shape [H, W, C].face_w: Integer representing the width of each individual cube face.mode: Interpolation method (same options as c2e).cube_format: The desired output format ('dice', 'horizon', 'dict', or 'list').import py360convert
# Example usage (conceptual)
py360convert.e2c(equi_img, face_w=256, mode='bilinear', cube_format='dice')Use py360convert.c2e() to convert a cubemap image into an equirectangular format.
Parameters:
cubemap: Numpy array or a collection (list/dict) of numpy arrays depending on cube_format.h: Output equirectangular height.w: Output equirectangular width.mode: Interpolation method. Valid options: "nearest", "linear", "bilinear", "biquadratic", "quadratic", "quad", "bicubic", "cubic", "biquartic", "quartic", "biquintic", "quintic".cube_format: Defines the input structure:'dice' (default): A single numpy array (e.g., shape 1024 x 768 for 256x256 faces).'horizon': A single numpy array (e.g., shape 1536 x 256).'list': A list containing 6 numpy arrays (one per face).'dict': A dict with keys 'F', 'R', 'B', 'L', 'U', 'D' (Front, Right, Back, Left, Up, Down).import py360convert
# Example usage (conceptual)
py360convert.c2e(cubemap_array, h=400, w=800, cube_format='dice')The library provides utility functions to transform cubemap representations between 'dice', 'horizon', 'dict', and 'list' formats.
Available Utilities:
cube_dice2h(cubemap): Converts 'dice' format to 'horizon' format.cube_h2dice(cube_h): Converts 'horizon' format to 'dice' format.cube_h2dict(cube_h): Converts 'horizon' format to a dictionary of faces.cube_dict2h(cube_dict): Converts a dictionary of faces to 'horizon' format.cube_h2list(cube_h): Converts 'horizon' format to a list of 6 arrays.cube_list2h(cube_list): Converts a list of 6 arrays to 'horizon' format.import numpy as np
from PIL import Image
import py360convert
cube_dice = np.array(Image.open('assets/demo_cube.png'))
# Convert dice to horizon
cube_h = py360convert.cube_dice2h(cube_dice)
# Convert horizon to dict
cube_dict = py360convert.cube_h2dict(cube_h)
# Convert horizon to list
cube_list = py360convert.cube_h2list(cube_h)The e2p function converts an equirectangular image into a perspective (planar) image based on a specified field of view (FOV) and viewing angle. It supports both grayscale (2D) and color (3D) input images.
e_img: The input equirectangular image. Shape must be [H, W] or [H, W, C].fov_deg: The field of view in degrees. Can be a single scalar (applied to both axes) or a tuple (h_fov_deg, v_fov_deg).u_deg: Horizontal viewing angle (horizon) in degrees, in the range [-180, 180]. Negative values look left, positive values look right.v_deg: Vertical viewing angle in degrees, in the range [-90, 90]. Negative values look down, positive values look up.out_hw: A tuple (height, width) representing the desired output dimensions of the perspective image.in_rot_deg: (Optional) In-plane rotation in degrees.mode: (Optional) Interpolation mode. Supported values are `The convert360 CLI tool allows you to perform conversions directly from the terminal. Use convert360 -h to view detailed help information.
e2c)c2e)e2p)# Equirectangular to Cubemap
convert360 e2c assets/example_input.png out.png --size 200
# Cubemap to Equirectangular
convert360 c2e assets/example_e2c.png out.png --width 800 --height 400
# Equirectangular to Perspective
convert360 e2p assets/example_input.png out.png --width 300 --height 300 --yaw 120 --pitch 23Use the e2p function to convert an equirectangular image into a series of panorama images (often used for specific viewing formats).
from py360convert import e2p
# Example usage (parameters depend on implementation in e2p.py)
# panoramas = e2p(equirectangular_image)The utils module provides functions to transform cubemap data between different internal representations: dice (6 faces), dict (dictionary of faces), list (list of faces), and h (equirectangular/horizontal projection).
Available transformation functions:
cube_dice2h: Convert dice format to equirectangular.cube_dict2h: Convert dictionary format to equirectangular.cube_h2dice: Convert equirectangular to dice format.cube_h2dict: Convert equirectangular to dictionary format.cube_h2list: Convert equirectangular to list format.cube_list2h: Convert list format to equirectangular.from py360convert import (
cube_dice2h,
cube_dict2h,
cube_h2dice,
cube_h2dict,
cube_h2list,
cube_list2h
)Use the c2e function to convert a cubemap image back into an equirectangular projection.
from py360convert import c2e
# Example usage (parameters depend on implementation in c2e.py)
# equirectangular_image = c2e(cube_map)The e2c() function converts an equirectangular image into various cubemap formats. It supports both 2D (grayscale) and 3D (color) input arrays.
e_img (NDArray): The input equirectangular image. Expected shape is [H, W] or [H, W, C].face_w (int): The length (width/height) of each individual face of the cubemap. Defaults to 256.mode (Literal["bilinear", "nearest"]): The interpolation mode used for sampling. Defaults to "bilinear".cube_format (Literal["horizon", "list", "dict", "dice"]): Specifies the structure of the returned cubemap.cube_format"horizon" or "dice": Returns a single NDArray containing the combined cubemap data."list": Returns a list[NDArray], where each element is a face of the cubemap."dict": Returns a dict[str, NDArray], mapping face identifiers (e.g., 'front', 'back', etc.) to their respective face arrays.import numpy as np
from py360convert import e2c
# Load or create an equirectangular image (H, W, C)
e_img = np.random.rand(512, 1024, 3).astype(np.float32)
# Convert to a single 'dice' format array
cubemap_dice = e2c(e_img, face_w=256, mode="bilinear", cube_format="dice")
# Convert to a dictionary of faces
cubemap_dict = e2c(e_img, face_w=256, cube_format="dict")The cube_format parameter determines how the resulting cubemap data is structured and returned. Use the following literals:
"horizon": Returns a single array representing the cubemap."dice": Returns a single array representing the cubemap (often used for texture atlases)."list": Returns a list of arrays, one for each face."dict": Returns a dictionary where keys are face names and values are the face arrays.cube_format: Literal["horizon", "list", "dict", "dice"]