Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.
 
 
 
 
 
 

150 wiersze
6.5 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('--png', action='store_true',
  43. help='output PNG tiles instead of JPEG tiles')
  44. parser.add_argument('-n', '--nona', default=nona, required=nona is None,
  45. metavar='EXECUTABLE',
  46. help='location of the nona executable to use')
  47. args = parser.parse_args()
  48. # Process input image information
  49. print('Processing input image information...')
  50. origWidth, origHeight = Image.open(args.inputFile).size
  51. if float(origWidth) / origHeight != 2:
  52. print('Error: the image width is not twice the image height.')
  53. print('Input image must be a full, not partial, equirectangular panorama!')
  54. sys.exit(1)
  55. cubeSize = 8 * int(origWidth / 3.14159265 / 8)
  56. levels = int(math.ceil(math.log(float(cubeSize) / args.tileSize, 2))) + 1
  57. origHeight = str(origHeight)
  58. origWidth = str(origWidth)
  59. origFilename = os.path.join(os.getcwd(), args.inputFile)
  60. extension = '.jpg'
  61. if args.png:
  62. extension = '.png'
  63. # Create output directory
  64. os.makedirs(args.output)
  65. # Generate PTO file for nona to generate cube faces
  66. # Face order: front, back, up, down, left, right
  67. faceLetters = ['f', 'b', 'u', 'd', 'l', 'r']
  68. text = []
  69. text.append('p E0 R0 f0 h' + str(cubeSize) + ' n"TIFF_m" u0 v90 w' + str(cubeSize))
  70. text.append('m g1 i0 m2 p0.00784314')
  71. text.append('i a0 b0 c0 d0 e0 f4 h' + origHeight + ' n"' + origFilename + '" p0 r0 v360 w' + origWidth + ' y0')
  72. text.append('i a0 b0 c0 d0 e0 f4 h' + origHeight + ' n"' + origFilename + '" p0 r0 v360 w' + origWidth + ' y180')
  73. text.append('i a0 b0 c0 d0 e0 f4 h' + origHeight + ' n"' + origFilename + '" p-90 r0 v360 w' + origWidth + ' y0')
  74. text.append('i a0 b0 c0 d0 e0 f4 h' + origHeight + ' n"' + origFilename + '" p90 r0 v360 w' + origWidth + ' y0')
  75. text.append('i a0 b0 c0 d0 e0 f4 h' + origHeight + ' n"' + origFilename + '" p0 r0 v360 w' + origWidth + ' y90')
  76. text.append('i a0 b0 c0 d0 e0 f4 h' + origHeight + ' n"' + origFilename + '" p0 r0 v360 w' + origWidth + ' y-90')
  77. text.append('v')
  78. text.append('*')
  79. text = '\n'.join(text)
  80. with open(os.path.join(args.output, 'cubic.pto'), 'w') as f:
  81. f.write(text)
  82. # Create cube faces
  83. print('Generating cube faces...')
  84. subprocess.check_call([args.nona, '-o', os.path.join(args.output, 'face'), os.path.join(args.output, 'cubic.pto')])
  85. faces = ['face0000.tif', 'face0001.tif', 'face0002.tif', 'face0003.tif', 'face0004.tif', 'face0005.tif']
  86. # Generate tiles
  87. print('Generating tiles...')
  88. for f in range(0, 6):
  89. size = cubeSize
  90. face = Image.open(os.path.join(args.output, faces[f]))
  91. for level in range(levels, 0, -1):
  92. if not os.path.exists(os.path.join(args.output, str(level))):
  93. os.makedirs(os.path.join(args.output, str(level)))
  94. tiles = int(math.ceil(float(size) / args.tileSize))
  95. if (level < levels):
  96. face = face.resize([size, size], Image.ANTIALIAS)
  97. for i in range(0, tiles):
  98. for j in range(0, tiles):
  99. left = j * args.tileSize
  100. upper = i * args.tileSize
  101. right = min(j * args.tileSize + args.tileSize, size)
  102. lower = min(i * args.tileSize + args.tileSize, size)
  103. tile = face.crop([left, upper, right, lower])
  104. tile.load()
  105. tile.save(os.path.join(args.output, str(level), faceLetters[f] + str(i) + '_' + str(j) + extension))
  106. size = int(size / 2)
  107. # Generate fallback tiles
  108. print('Generating fallback tiles...')
  109. for f in range(0, 6):
  110. if not os.path.exists(os.path.join(args.output, 'fallback')):
  111. os.makedirs(os.path.join(args.output, 'fallback'))
  112. face = Image.open(os.path.join(args.output, faces[f]))
  113. face = face.resize([1024, 1024], Image.ANTIALIAS)
  114. face.save(os.path.join(args.output, 'fallback', faceLetters[f] + extension))
  115. # Clean up temporary files
  116. os.remove(os.path.join(args.output, 'cubic.pto'))
  117. for face in faces:
  118. os.remove(os.path.join(args.output, face))
  119. # Generate config file
  120. text = []
  121. text.append('{')
  122. text.append(' "type": "multires",')
  123. text.append(' ')
  124. text.append(' "multiRes": {')
  125. text.append(' "path": "/%l/%s%y_%x",')
  126. text.append(' "fallbackPath": "/fallback/%s",')
  127. text.append(' "extension": "' + extension[1:] + '",')
  128. text.append(' "tileResolution": ' + str(args.tileSize) + ',')
  129. text.append(' "maxLevel": ' + str(levels) + ',')
  130. text.append(' "cubeResolution": ' + str(cubeSize))
  131. text.append(' }')
  132. text.append('}')
  133. text = '\n'.join(text)
  134. with open(os.path.join(args.output, 'config.json'), 'w') as f:
  135. f.write(text)