py360convert

repository·master·Indexed 20 days ago

https://github.com/sunset1995/py360convert

A 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.

Tokens
3.4K
Snippets
13
Records
14
Agent score
69%

What's inside py360convert

  1. Install py360convert

    master

    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 py360convert
  2. Extract Perspective image from Equirectangular with e2p()

    master

    Use 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))
  3. Convert Equirectangular to Cubemap with e2c()

    master

    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')
  4. Convert Cubemap to Equirectangular with c2e()

    master

    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')
  5. Convert between different cubemap formats

    master

    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)
  6. Convert equirectangular image to perspective with e2p()

    master

    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.

    Parameters

    • 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 `
  7. Use the convert360 command line tool

    master

    The convert360 CLI tool allows you to perform conversions directly from the terminal. Use convert360 -h to view detailed help information.

    Conversion Types:

    • Equirectangular to Cubemap (e2c)
    • Cubemap to Equirectangular (c2e)
    • Equirectangular to Perspective (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 23
  8. Manipulate Cubemap Data Formats with utils

    master

    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
    )
  9. Convert equirectangular image to cubemap with e2c()

    master

    The e2c() function converts an equirectangular image into various cubemap formats. It supports both 2D (grayscale) and 3D (color) input arrays.

    Parameters

    • 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.

    Return Types based on 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")
  10. Reference: e2c() cube_format options

    master

    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"]