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.
 
 
 
 
 
 

123 satır
5.3 KiB

  1. #!/usr/bin/env python
  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. # Parse input
  28. parser = argparse.ArgumentParser(description='Generate a Pannellum multires tile set from an equirectangular panorama.')
  29. parser.add_argument('inputFile', metavar='INPUT',
  30. help='full equirectangular panorama to be processed')
  31. parser.add_argument('-o', '--output', dest='output', default='./output',
  32. help='output directory (default: ./output)')
  33. parser.add_argument('-s', '--tilesize', dest='tileSize', default=512, type=int,
  34. help='tile size in pixels (default: 512)')
  35. parser.add_argument('--png', action='store_true',
  36. help='output PNG tiles instead of JPEG tiles')
  37. args = parser.parse_args()
  38. # Create output directory
  39. os.makedirs(args.output)
  40. # Process input image information
  41. print('Processing input image information...')
  42. origWidth, origHeight = Image.open(args.inputFile).size
  43. cubeSize = 8 * int(origWidth / 3.14159265 / 8)
  44. levels = math.ceil(math.log(cubeSize / args.tileSize, 2)) + 1
  45. origHeight = str(origHeight)
  46. origWidth = str(origWidth)
  47. origFilename = os.path.join(os.getcwd(), args.inputFile)
  48. extension = '.jpg'
  49. if args.png:
  50. extension = '.png'
  51. # Generate PTO file for nona to generate cube faces
  52. # Face order: front, back, up, down, left, right
  53. faceLetters = ['f', 'b', 'u', 'd', 'l', 'r']
  54. text = []
  55. text.append('p E0 R0 f0 h' + str(cubeSize) + ' n"TIFF_m" u0 v90 w' + str(cubeSize))
  56. text.append('m g1 i0 m2 p0.00784314')
  57. text.append('i a0 b0 c0 d0 e0 f4 h' + origHeight + ' n"' + origFilename + '" p0 r0 v360 w' + origWidth + ' y0')
  58. text.append('i a0 b0 c0 d0 e0 f4 h' + origHeight + ' n"' + origFilename + '" p0 r0 v360 w' + origWidth + ' y180')
  59. text.append('i a0 b0 c0 d0 e0 f4 h' + origHeight + ' n"' + origFilename + '" p-90 r0 v360 w' + origWidth + ' y0')
  60. text.append('i a0 b0 c0 d0 e0 f4 h' + origHeight + ' n"' + origFilename + '" p90 r0 v360 w' + origWidth + ' y0')
  61. text.append('i a0 b0 c0 d0 e0 f4 h' + origHeight + ' n"' + origFilename + '" p0 r0 v360 w' + origWidth + ' y90')
  62. text.append('i a0 b0 c0 d0 e0 f4 h' + origHeight + ' n"' + origFilename + '" p0 r0 v360 w' + origWidth + ' y-90')
  63. text.append('v')
  64. text.append('*')
  65. text = '\n'.join(text)
  66. with open(os.path.join(args.output, 'cubic.pto'), 'w') as f:
  67. f.write(text)
  68. # Create cube faces
  69. print('Generating cube faces...')
  70. os.system('nona -o ' + os.path.join(args.output, 'face') + ' ' + os.path.join(args.output, 'cubic.pto'))
  71. faces = ['face0000.tif', 'face0001.tif', 'face0002.tif', 'face0003.tif', 'face0004.tif', 'face0005.tif']
  72. # Generate tiles
  73. print('Generating tiles...')
  74. for f in range(0, 6):
  75. size = cubeSize
  76. face = Image.open(os.path.join(args.output, faces[f]))
  77. for level in range(levels, 0, -1):
  78. os.makedirs(os.path.join(args.output, str(level)), exist_ok=True)
  79. tiles = math.ceil(size / args.tileSize)
  80. if (level < levels):
  81. face = face.resize([size, size], Image.ANTIALIAS)
  82. for i in range(0, tiles):
  83. for j in range(0, tiles):
  84. left = j * args.tileSize
  85. upper = i * args.tileSize
  86. right = min(j * args.tileSize + args.tileSize, size)
  87. lower = min(i * args.tileSize + args.tileSize, size)
  88. tile = face.crop([left, upper, right, lower])
  89. tile.load()
  90. tile.save(os.path.join(args.output, str(level), faceLetters[f] + str(i) + '_' + str(j) + extension))
  91. size = int(size / 2)
  92. # Clean up temporary files
  93. os.remove(os.path.join(args.output, 'cubic.pto'))
  94. for face in faces:
  95. os.remove(os.path.join(args.output, face))
  96. # Generate config file
  97. text = []
  98. text.append('{')
  99. text.append(' "type": "multires",')
  100. text.append(' ')
  101. text.append(' "multiRes": {')
  102. text.append(' "path": "./%l/%s%y_%x",')
  103. text.append(' "extension": "jpg",')
  104. text.append(' "tileResolution": ' + str(args.tileSize) + ',')
  105. text.append(' "maxLevel": ' + str(levels) + ',')
  106. text.append(' "cubeResolution": ' + str(cubeSize))
  107. text.append(' }')
  108. text.append('}')
  109. text = '\n'.join(text)
  110. with open(os.path.join(args.output, 'config.json'), 'w') as f:
  111. f.write(text)