
from PIL import Image
import json
import sys
import os
import argparse

# WARNING do not run this in the 'pano' directly, it will wipe out index.html there.
# instead, run it like this: `[.../something]$ python ../gen_cub2.py something.jpg`

p = argparse.ArgumentParser (description="generate multires pannellum")
p.add_argument ('imagefile',  help='image file')
p.add_argument ('--title',    help='image title', required=True)

args = p.parse_args()

path = args.imagefile

cmd = 'python3.11 /home/rushing/src/pannellum-2.5.6/utils/multires/generate.py %s' % (path,)
print (cmd)
os.system (cmd)
config = json.loads (open ('output/config.json', 'rb').read())
config['multiRes']['path'] = 'output/' + config['multiRes']['path']
config['multiRes']['fallbackPath'] = 'output/' + config['multiRes']['fallbackPath']
config['pitch'] = -20
config['hfov'] = 90
config['autoLoad'] = True
config['autoRotate'] = -1

template = """
<!DOCTYPE HTML>
<html>
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>%s</title>
    <link rel="stylesheet" href="https://cdn.pannellum.org/2.2/pannellum.css"/>
    <script type="text/javascript" src="https://cdn.pannellum.org/2.2/pannellum.js"></script>
    <style>
      #panorama {
      width: 100%%;
      height: 700px;
      }
    </style>
  </head>
  <body>
    <div id="panorama"></div>
    <script>
      pannellum.viewer('panorama', %s);
    </script>
    </div>
  </body>
  </html>
"""

open ('index.html', 'w').write (template % (args.title, json.dumps (config, sort_keys=True, indent=4)))

