You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

157 lines
6.9 KiB

  1. #!/usr/bin/env python3
  2. # Requires Python 3.2+ (or Python 2.7) and nona (from Hugin)
  3. # generate.py - A multires tile set generator for Pannellum
  4. # Copyright (c) 2014-2015 Matthew Petroff
  5. #
  6. # Permission is hereby granted, free of charge, to any person obtaining a copy
  7. # of this software and associated documentation files (the "Software"), to deal
  8. # in the Software without restriction, including without limitation the rights
  9. # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. # copies of the Software, and to permit persons to whom the Software is
  11. # furnished to do so, subject to the following conditions:
  12. #
  13. # The above copyright notice and this permission notice shall be included in
  14. # all copies or substantial portions of the Software.
  15. #
  16. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  19. # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  22. # THE SOFTWARE.
  23. from __future__ import print_function
  24. import argparse
  25. from PIL import Image
  26. import os
  27. import sys
  28. import math
  29. from distutils.spawn import find_executable
  30. import subprocess
  31. # find external programs
  32. nona = find_executable('nona')
  33. # Parse input
  34. parser = argparse.ArgumentParser(description='Generate a Pannellum multires tile set from an full equirectangular panorama.',
  35. formatter_class=argparse.ArgumentDefaultsHelpFormatter)
  36. parser.add_argument('inputFile', metavar='INPUT',
  37. help='full equirectangular panorama to be processed')
  38. parser.add_argument('-o', '--output', dest='output', default='./output',
  39. help='output directory')
  40. parser.add_argument('-s', '--tilesize', dest='tileSize', default=512, type=int,
  41. help='tile size in pixels')
  42. parser.add_argument('-c', '--cubesize', dest='cubeSize', default=0, type=int,
  43. help='cube size in pixels, or 0 to retain all details')
  44. parser.add_argument('-q', '--quality', dest='quality', default=75, type=int,
  45. help='output JPEG quality 0-100')
  46. parser.add_argument('--png', action='store_true',
  47. help='output PNG tiles instead of JPEG tiles')
  48. parser.add_argument('-n', '--nona', default=nona, required=nona is None,
  49. metavar='EXECUTABLE',
  50. help='location of the nona executable to use')
  51. args = parser.parse_args()
  52. # Process input image information
  53. print('Processing input image information...')
  54. origWidth, origHeight = Image.open(args.inputFile).size
  55. if float(origWidth) / origHeight != 2:
  56. print('Error: the image width is not twice the image height.')
  57. print('Input image must be a full, not partial, equirectangular panorama!')
  58. sys.exit(1)
  59. if args.cubeSize != 0:
  60. cubeSize = args.cubeSize
  61. else:
  62. cubeSize = 8 * int(origWidth / 3.14159265 / 8)
  63. levels = int(math.ceil(math.log(float(cubeSize) / args.tileSize, 2))) + 1
  64. origHeight = str(origHeight)
  65. origWidth = str(origWidth)
  66. origFilename = os.path.join(os.getcwd(), args.inputFile)
  67. extension = '.jpg'
  68. if args.png:
  69. extension = '.png'
  70. # Create output directory
  71. os.makedirs(args.output)
  72. # Generate PTO file for nona to generate cube faces
  73. # Face order: front, back, up, down, left, right
  74. faceLetters = ['f', 'b', 'u', 'd', 'l', 'r']
  75. text = []
  76. text.append('p E0 R0 f0 h' + str(cubeSize) + ' n"TIFF_m" u0 v90 w' + str(cubeSize))
  77. text.append('m g1 i0 m2 p0.00784314')
  78. text.append('i a0 b0 c0 d0 e0 f4 h' + origHeight + ' n"' + origFilename + '" p0 r0 v360 w' + origWidth + ' y0')
  79. text.append('i a0 b0 c0 d0 e0 f4 h' + origHeight + ' n"' + origFilename + '" p0 r0 v360 w' + origWidth + ' y180')
  80. text.append('i a0 b0 c0 d0 e0 f4 h' + origHeight + ' n"' + origFilename + '" p-90 r0 v360 w' + origWidth + ' y0')
  81. text.append('i a0 b0 c0 d0 e0 f4 h' + origHeight + ' n"' + origFilename + '" p90 r0 v360 w' + origWidth + ' y0')
  82. text.append('i a0 b0 c0 d0 e0 f4 h' + origHeight + ' n"' + origFilename + '" p0 r0 v360 w' + origWidth + ' y90')
  83. text.append('i a0 b0 c0 d0 e0 f4 h' + origHeight + ' n"' + origFilename + '" p0 r0 v360 w' + origWidth + ' y-90')
  84. text.append('v')
  85. text.append('*')
  86. text = '\n'.join(text)
  87. with open(os.path.join(args.output, 'cubic.pto'), 'w') as f:
  88. f.write(text)
  89. # Create cube faces
  90. print('Generating cube faces...')
  91. subprocess.check_call([args.nona, '-o', os.path.join(args.output, 'face'), os.path.join(args.output, 'cubic.pto')])
  92. faces = ['face0000.tif', 'face0001.tif', 'face0002.tif', 'face0003.tif', 'face0004.tif', 'face0005.tif']
  93. # Generate tiles
  94. print('Generating tiles...')
  95. for f in range(0, 6):
  96. size = cubeSize
  97. face = Image.open(os.path.join(args.output, faces[f]))
  98. for level in range(levels, 0, -1):
  99. if not os.path.exists(os.path.join(args.output, str(level))):
  100. os.makedirs(os.path.join(args.output, str(level)))
  101. tiles = int(math.ceil(float(size) / args.tileSize))
  102. if (level < levels):
  103. face = face.resize([size, size], Image.ANTIALIAS)
  104. for i in range(0, tiles):
  105. for j in range(0, tiles):
  106. left = j * args.tileSize
  107. upper = i * args.tileSize
  108. right = min(j * args.tileSize + args.tileSize, size)
  109. lower = min(i * args.tileSize + args.tileSize, size)
  110. tile = face.crop([left, upper, right, lower])
  111. tile.load()
  112. tile.save(os.path.join(args.output, str(level), faceLetters[f] + str(i) + '_' + str(j) + extension), quality = args.quality)
  113. size = int(size / 2)
  114. # Generate fallback tiles
  115. print('Generating fallback tiles...')
  116. for f in range(0, 6):
  117. if not os.path.exists(os.path.join(args.output, 'fallback')):
  118. os.makedirs(os.path.join(args.output, 'fallback'))
  119. face = Image.open(os.path.join(args.output, faces[f]))
  120. face = face.resize([1024, 1024], Image.ANTIALIAS)
  121. face.save(os.path.join(args.output, 'fallback', faceLetters[f] + extension), quality = args.quality)
  122. # Clean up temporary files
  123. os.remove(os.path.join(args.output, 'cubic.pto'))
  124. for face in faces:
  125. os.remove(os.path.join(args.output, face))
  126. # Generate config file
  127. text = []
  128. text.append('{')
  129. text.append(' "type": "multires",')
  130. text.append(' ')
  131. text.append(' "multiRes": {')
  132. text.append(' "path": "/%l/%s%y_%x",')
  133. text.append(' "fallbackPath": "/fallback/%s",')
  134. text.append(' "extension": "' + extension[1:] + '",')
  135. text.append(' "tileResolution": ' + str(args.tileSize) + ',')
  136. text.append(' "maxLevel": ' + str(levels) + ',')
  137. text.append(' "cubeResolution": ' + str(cubeSize))
  138. text.append(' }')
  139. text.append('}')
  140. text = '\n'.join(text)
  141. with open(os.path.join(args.output, 'config.json'), 'w') as f:
  142. f.write(text)