No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.
 
 
 
 
 
 

158 líneas
6.9 KiB

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