Last active
March 6, 2025 18:09
-
-
Save Waffle1434/1f0f53e7def127f6eb560cad65531148 to your computer and use it in GitHub Desktop.
Convert 360 images to perspective cubemap. Requires py360convert
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
import argparse, os, sys | |
from pathlib import Path | |
from typing import get_args | |
from PIL import Image | |
import numpy as np | |
import py360convert | |
img_extensions = {".jpg", ".png"} | |
def ec2_dir(path, fov, size, mode): | |
for filename in os.listdir(path): | |
ext = os.path.splitext(filename)[1].lower() | |
if ext in img_extensions: | |
e2c_img(os.path.join(path, filename), fov, size, mode) | |
def e2c_img(path, fov, size, mode): | |
img = np.array(Image.open(path)) | |
(out_path, out_ext) = os.path.splitext(path) | |
e2p_img(img, fov, 0, 0, 0, size, mode, f"out/{out_path} Y+{out_ext}") | |
e2p_img(img, fov, 90, 0, 0, size, mode, f"out/{out_path} X+{out_ext}") | |
e2p_img(img, fov, 180, 0, 0, size, mode, f"out/{out_path} Y-{out_ext}") | |
e2p_img(img, fov, -90, 0, 0, size, mode, f"out/{out_path} X-{out_ext}") | |
e2p_img(img, fov, 0, +90, 0, size, mode, f"out/{out_path} Z+{out_ext}") | |
e2p_img(img, fov, 0, -90, 0, size, mode, f"out/{out_path} Z-{out_ext}") | |
def e2p_img(img, fov, yaw, pitch, roll, size, mode, out_path): | |
out = py360convert.e2p( | |
img, | |
fov_deg=(fov, fov), | |
u_deg=yaw, | |
v_deg=pitch, | |
out_hw=(size, size), | |
in_rot_deg=roll, | |
mode=mode | |
) | |
os.makedirs(os.path.dirname(out_path), exist_ok=True) | |
print(out_path) | |
Image.fromarray(out).save(out_path, format='JPEG', subsampling=0, quality=100) | |
def main(): | |
parser = argparse.ArgumentParser(description="Conversion between cubemap and equirectangular or equirectangular to perspective.", add_help=False) | |
parser.add_argument("--help", action="help", help="Show this help message and exit") | |
subparsers = parser.add_subparsers(dest="command", help="Convert command.") | |
e2p_parser = subparsers.add_parser("e2c", help="Convert equirectangular to perspective.", add_help=False) | |
e2p_parser.add_argument("--help", action="help", help="Show this help message and exit") | |
e2p_parser.add_argument("input", type=Path, help="Path to input images.") | |
e2p_parser.add_argument("--size", "-s", type=int, required=True, help="Output image size in pixels.") | |
e2p_parser.add_argument("--fov", "-f", type=float, default=90, help="FoV in degrees.") | |
e2p_parser.add_argument("--mode", "-m", default="bilinear", choices=get_args(py360convert.utils.InterpolationMode), help="Resampling method.") | |
args = parser.parse_args() | |
if args.command is None: | |
parser.print_help() | |
sys.exit(1) | |
elif args.command == "e2c": | |
ec2_dir(args.input, args.fov, args.size, args.mode) | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment