25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.
 
 
 
 
 
 

152 satır
6.5 KiB

  1. #!/usr/bin/env python3
  2. # Requires Python 3.2+
  3. # generate.py - A multires tile set generator for Pannellum
  4. # Copyright (c) 2014 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. import argparse
  24. from PIL import Image
  25. import os
  26. import math
  27. from distutils.spawn import find_executable
  28. import subprocess
  29. # find external programs
  30. nona = find_executable('nona')
  31. # Parse input
  32. parser = argparse.ArgumentParser(description='Generate a Pannellum multires tile set from an equirectangular panorama.',
  33. formatter_class=argparse.ArgumentDefaultsHelpFormatter)
  34. parser.add_argument('inputFile', metavar='INPUT',
  35. help='full equirectangular panorama to be processed')
  36. parser.add_argument('-o', '--output', dest='output', default='./output',
  37. help='output directory')
  38. parser.add_argument('-s', '--tilesize', dest='tileSize', default=512, type=int,
  39. help='tile size in pixels')
  40. parser.add_argument('--png', action='store_true',
  41. help='output PNG tiles instead of JPEG tiles')
  42. parser.add_argument('-n', '--nona', default=nona, required=nona is None,
  43. metavar='EXECUTABLE',
  44. help='location of the nona executable to use')
  45. args = parser.parse_args()
  46. # Create output directory
  47. os.makedirs(args.output)
  48. # Process input image information
  49. print('Processing input image information...')
  50. origWidth, origHeight = Image.open(args.inputFile).size
  51. cubeSize = 8 * int(origWidth / 3.14159265 / 8)
  52. levels = math.ceil(math.log(cubeSize / args.tileSize, 2)) + 1
  53. origHeight = str(origHeight)
  54. origWidth = str(origWidth)
  55. origFilename = os.path.join(os.getcwd(), args.inputFile)
  56. extension = '.jpg'
  57. if args.png:
  58. extension = '.png'
  59. # Generate PTO file for nona to generate cube faces
  60. # Face order: front, back, up, down, left, right
  61. faceLetters = ['f', 'b', 'u', 'd', 'l', 'r']
  62. text = []
  63. text.append('p E0 R0 f0 h' + str(cubeSize) + ' n"TIFF_m" u0 v90 w' + str(cubeSize))
  64. text.append('m g1 i0 m2 p0.00784314')
  65. text.append('i a0 b0 c0 d0 e0 f4 h' + origHeight + ' n"' + origFilename + '" p0 r0 v360 w' + origWidth + ' y0')
  66. text.append('i a0 b0 c0 d0 e0 f4 h' + origHeight + ' n"' + origFilename + '" p0 r0 v360 w' + origWidth + ' y180')
  67. text.append('i a0 b0 c0 d0 e0 f4 h' + origHeight + ' n"' + origFilename + '" p-90 r0 v360 w' + origWidth + ' y0')
  68. text.append('i a0 b0 c0 d0 e0 f4 h' + origHeight + ' n"' + origFilename + '" p90 r0 v360 w' + origWidth + ' y0')
  69. text.append('i a0 b0 c0 d0 e0 f4 h' + origHeight + ' n"' + origFilename + '" p0 r0 v360 w' + origWidth + ' y90')
  70. text.append('i a0 b0 c0 d0 e0 f4 h' + origHeight + ' n"' + origFilename + '" p0 r0 v360 w' + origWidth + ' y-90')
  71. text.append('v')
  72. text.append('*')
  73. text = '\n'.join(text)
  74. with open(os.path.join(args.output, 'cubic.pto'), 'w') as f:
  75. f.write(text)
  76. # Create cube faces
  77. print('Generating cube faces...')
  78. subprocess.check_call([args.nona, '-o', os.path.join(args.output, 'face'), os.path.join(args.output, 'cubic.pto')])
  79. faces = ['face0000.tif', 'face0001.tif', 'face0002.tif', 'face0003.tif', 'face0004.tif', 'face0005.tif']
  80. # Generate tiles
  81. print('Generating tiles...')
  82. for f in range(0, 6):
  83. size = cubeSize
  84. face = Image.open(os.path.join(args.output, faces[f]))
  85. for level in range(levels, 0, -1):
  86. os.makedirs(os.path.join(args.output, str(level)), exist_ok=True)
  87. tiles = math.ceil(size / args.tileSize)
  88. if (level < levels):
  89. face = face.resize([size, size], Image.ANTIALIAS)
  90. for i in range(0, tiles):
  91. for j in range(0, tiles):
  92. left = j * args.tileSize
  93. upper = i * args.tileSize
  94. right = min(j * args.tileSize + args.tileSize, size)
  95. lower = min(i * args.tileSize + args.tileSize, size)
  96. tile = face.crop([left, upper, right, lower])
  97. tile.load()
  98. tile.save(os.path.join(args.output, str(level), faceLetters[f] + str(i) + '_' + str(j) + extension))
  99. size = int(size / 2)
  100. # Generate fallback tiles
  101. print('Generating fallback tiles...')
  102. for f in range(0, 6):
  103. os.makedirs(os.path.join(args.output, 'fallback'), exist_ok=True)
  104. face = Image.open(os.path.join(args.output, faces[f]))
  105. face = face.resize([1024, 1024], Image.ANTIALIAS)
  106. # Create 1px border by duplicating border pixels
  107. out = Image.new(face.mode, (1026, 1026))
  108. out.paste(face, (0, 1))
  109. out.paste(face, (2, 1))
  110. out.paste(face, (1, 0))
  111. out.paste(face, (1, 2))
  112. out.putpixel((0, 0), out.getpixel((1, 0)))
  113. out.putpixel((1025, 0), out.getpixel((1025, 1)))
  114. out.putpixel((1025, 1025), out.getpixel((1024, 1025)))
  115. out.putpixel((0, 1025), out.getpixel((0, 1024)))
  116. out.paste(face, (1, 1))
  117. out.save(os.path.join(args.output, 'fallback', faceLetters[f] + extension))
  118. # Clean up temporary files
  119. os.remove(os.path.join(args.output, 'cubic.pto'))
  120. for face in faces:
  121. os.remove(os.path.join(args.output, face))
  122. # Generate config file
  123. text = []
  124. text.append('{')
  125. text.append(' "type": "multires",')
  126. text.append(' ')
  127. text.append(' "multiRes": {')
  128. text.append(' "path": "./%l/%s%y_%x",')
  129. text.append(' "fallbackPath": "./fallback/%s",')
  130. text.append(' "extension": "jpg",')
  131. text.append(' "tileResolution": ' + str(args.tileSize) + ',')
  132. text.append(' "maxLevel": ' + str(levels) + ',')
  133. text.append(' "cubeResolution": ' + str(cubeSize))
  134. text.append(' }')
  135. text.append('}')
  136. text = '\n'.join(text)
  137. with open(os.path.join(args.output, 'config.json'), 'w') as f:
  138. f.write(text)