Não pode escolher mais do que 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 
 
 
 

172 linhas
7.3 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-2017 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. try:
  34. nona = find_executable('nona')
  35. except KeyError:
  36. # Handle case of PATH not being set
  37. nona = None
  38. # Parse input
  39. parser = argparse.ArgumentParser(description='Generate a Pannellum multires tile set from an full equirectangular panorama.',
  40. formatter_class=argparse.ArgumentDefaultsHelpFormatter)
  41. parser.add_argument('inputFile', metavar='INPUT',
  42. help='full equirectangular panorama to be processed')
  43. parser.add_argument('-o', '--output', dest='output', default='./output',
  44. help='output directory')
  45. parser.add_argument('-s', '--tilesize', dest='tileSize', default=512, type=int,
  46. help='tile size in pixels')
  47. parser.add_argument('-c', '--cubesize', dest='cubeSize', default=0, type=int,
  48. help='cube size in pixels, or 0 to retain all details')
  49. parser.add_argument('-q', '--quality', dest='quality', default=75, type=int,
  50. help='output JPEG quality 0-100')
  51. parser.add_argument('--png', action='store_true',
  52. help='output PNG tiles instead of JPEG tiles')
  53. parser.add_argument('-n', '--nona', default=nona, required=nona is None,
  54. metavar='EXECUTABLE',
  55. help='location of the nona executable to use')
  56. args = parser.parse_args()
  57. # Process input image information
  58. print('Processing input image information...')
  59. origWidth, origHeight = Image.open(args.inputFile).size
  60. if float(origWidth) / origHeight != 2:
  61. print('Error: the image width is not twice the image height.')
  62. print('Input image must be a full, not partial, equirectangular panorama!')
  63. sys.exit(1)
  64. if args.cubeSize != 0:
  65. cubeSize = args.cubeSize
  66. else:
  67. cubeSize = 8 * int(origWidth / math.pi / 8)
  68. levels = int(math.ceil(math.log(float(cubeSize) / args.tileSize, 2))) + 1
  69. origHeight = str(origHeight)
  70. origWidth = str(origWidth)
  71. origFilename = os.path.join(os.getcwd(), args.inputFile)
  72. extension = '.jpg'
  73. if args.png:
  74. extension = '.png'
  75. # Create output directory
  76. os.makedirs(args.output)
  77. # Generate PTO file for nona to generate cube faces
  78. # Face order: front, back, up, down, left, right
  79. faceLetters = ['f', 'b', 'u', 'd', 'l', 'r']
  80. text = []
  81. text.append('p E0 R0 f0 h' + str(cubeSize) + ' n"TIFF_m" u0 v90 w' + str(cubeSize))
  82. text.append('m g1 i0 m2 p0.00784314')
  83. text.append('i a0 b0 c0 d0 e0 f4 h' + origHeight + ' n"' + origFilename + '" p0 r0 v360 w' + origWidth + ' y0')
  84. text.append('i a0 b0 c0 d0 e0 f4 h' + origHeight + ' n"' + origFilename + '" p0 r0 v360 w' + origWidth + ' y180')
  85. text.append('i a0 b0 c0 d0 e0 f4 h' + origHeight + ' n"' + origFilename + '" p-90 r0 v360 w' + origWidth + ' y0')
  86. text.append('i a0 b0 c0 d0 e0 f4 h' + origHeight + ' n"' + origFilename + '" p90 r0 v360 w' + origWidth + ' y0')
  87. text.append('i a0 b0 c0 d0 e0 f4 h' + origHeight + ' n"' + origFilename + '" p0 r0 v360 w' + origWidth + ' y90')
  88. text.append('i a0 b0 c0 d0 e0 f4 h' + origHeight + ' n"' + origFilename + '" p0 r0 v360 w' + origWidth + ' y-90')
  89. text.append('v')
  90. text.append('*')
  91. text = '\n'.join(text)
  92. with open(os.path.join(args.output, 'cubic.pto'), 'w') as f:
  93. f.write(text)
  94. # Create cube faces
  95. print('Generating cube faces...')
  96. subprocess.check_call([args.nona, '-o', os.path.join(args.output, 'face'), os.path.join(args.output, 'cubic.pto')])
  97. faces = ['face0000.tif', 'face0001.tif', 'face0002.tif', 'face0003.tif', 'face0004.tif', 'face0005.tif']
  98. # Generate tiles
  99. print('Generating tiles...')
  100. for f in range(0, 6):
  101. size = cubeSize
  102. face = Image.open(os.path.join(args.output, faces[f]))
  103. if 'A' in face.mode:
  104. if face.mode == 'RGBA':
  105. face = face.convert('RGB')
  106. elif face.mode == 'LA':
  107. face = face.convert('L')
  108. for level in range(levels, 0, -1):
  109. if not os.path.exists(os.path.join(args.output, str(level))):
  110. os.makedirs(os.path.join(args.output, str(level)))
  111. tiles = int(math.ceil(float(size) / args.tileSize))
  112. if (level < levels):
  113. face = face.resize([size, size], Image.ANTIALIAS)
  114. for i in range(0, tiles):
  115. for j in range(0, tiles):
  116. left = j * args.tileSize
  117. upper = i * args.tileSize
  118. right = min(j * args.tileSize + args.tileSize, size)
  119. lower = min(i * args.tileSize + args.tileSize, size)
  120. tile = face.crop([left, upper, right, lower])
  121. tile.load()
  122. tile.save(os.path.join(args.output, str(level), faceLetters[f] + str(i) + '_' + str(j) + extension), quality = args.quality)
  123. size = int(size / 2)
  124. # Generate fallback tiles
  125. print('Generating fallback tiles...')
  126. for f in range(0, 6):
  127. if not os.path.exists(os.path.join(args.output, 'fallback')):
  128. os.makedirs(os.path.join(args.output, 'fallback'))
  129. face = Image.open(os.path.join(args.output, faces[f]))
  130. if 'A' in face.mode:
  131. if face.mode == 'RGBA':
  132. face = face.convert('RGB')
  133. elif face.mode == 'LA':
  134. face = face.convert('L')
  135. face = face.resize([1024, 1024], Image.ANTIALIAS)
  136. face.save(os.path.join(args.output, 'fallback', faceLetters[f] + extension), quality = args.quality)
  137. # Clean up temporary files
  138. os.remove(os.path.join(args.output, 'cubic.pto'))
  139. for face in faces:
  140. os.remove(os.path.join(args.output, face))
  141. # Generate config file
  142. text = []
  143. text.append('{')
  144. text.append(' "type": "multires",')
  145. text.append(' ')
  146. text.append(' "multiRes": {')
  147. text.append(' "path": "/%l/%s%y_%x",')
  148. text.append(' "fallbackPath": "/fallback/%s",')
  149. text.append(' "extension": "' + extension[1:] + '",')
  150. text.append(' "tileResolution": ' + str(args.tileSize) + ',')
  151. text.append(' "maxLevel": ' + str(levels) + ',')
  152. text.append(' "cubeResolution": ' + str(cubeSize))
  153. text.append(' }')
  154. text.append('}')
  155. text = '\n'.join(text)
  156. with open(os.path.join(args.output, 'config.json'), 'w') as f:
  157. f.write(text)