Hellerick_Ferlibay

Bitmap_Rectangular_to_BorealTriaxial_4.py

Mar 11th, 2021
1,035
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.20 KB | None | 0 0
  1. from PIL import Image
  2. from math import atan2, pi, sqrt
  3. import os
  4. import platform
  5.  
  6. project_folder = 'Maps/TriaxialProjection/'
  7.  
  8. source_map_path = os.path.join(project_folder, 'Land_shallow_topo_2048.jpg')
  9.  
  10. output_map_path = os.path.join(project_folder, 'Generated_map_4_4400_shallow.jpg')
  11. output_map_size = 4400
  12. background = (255, 255, 255)
  13. deg = pi/180
  14. stretch = 0.8
  15.  
  16. sectors = [
  17.     {'interaxial': -22.5 * deg, 'axial': 20 * deg},  # 0
  18.     {'axial': 20 * deg, 'interaxial': 63 * deg},  # 1
  19.     {'interaxial': 63 * deg, 'axial': 130 * deg},  # 2
  20.     {'axial': 130 * deg, 'interaxial': (-150 + 360) * deg},  # 3
  21.     {'interaxial': (-150 + 360) * deg, 'axial': (-70 + 360) * deg},  # 4
  22.     {'axial': (-70 + 360) * deg, 'interaxial': (-22.5 + 360) * deg},  # 5
  23.     {'interaxial': (-22.5 + 360) * deg, 'axial': (20 + 360) * deg},  # 6
  24. ]
  25.  
  26. rotate = 20*deg
  27.  
  28.  
  29. def normalize(xy, size):
  30.     xy = list(xy)
  31.     if xy[0] >= size[0]:
  32.         xy[0] = xy[0] - size[0]
  33.     if xy[1] >= size[1]:
  34.         xy[1] = size[1]-1
  35.     return tuple(xy)
  36.  
  37.  
  38. def project_point(i, j, source_map, output_map, output_size):
  39.     x = (i + 0.5) - output_size / 2
  40.     y = (j + 0.5) - output_size / 2
  41.     a_p = (atan2(x, y) + rotate) % (2 * pi)
  42.     r = sqrt(x * x + y * y) / (output_size / 2) * pi
  43.     for s in sectors:
  44.         if s['range'][0] <= a_p <= s['range'][1]:
  45.             axial = s['axial']
  46.             interaxial = s['interaxial']
  47.     f_p = (a_p - interaxial) / (axial - interaxial)
  48.     if r < pi:
  49.         f_r = f_p ** (stretch*pi / (pi - r))
  50.     else:
  51.         f_r = 0
  52.     a_r = (axial - interaxial) * f_r + interaxial
  53.     lat = pi / 2 - r
  54.     lon = (a_r + pi) % (2*pi) - pi
  55.     point = (
  56.         (lon + pi) / (2 * pi) * source_map.size[0],
  57.         (pi / 2 - lat) / pi * source_map.size[1],
  58.     )
  59.     neighbors = [
  60.         {'coord': (int(point[0]) + 0, int(point[1]) + 0), 'close': (1 - point[0] % 1) * (1 - point[1] % 1)},
  61.         {'coord': (int(point[0]) + 0, int(point[1]) + 1), 'close': (1 - point[0] % 1) * (0 + point[1] % 1)},
  62.         {'coord': (int(point[0]) + 1, int(point[1]) + 0), 'close': (0 + point[0] % 1) * (1 - point[1] % 1)},
  63.         {'coord': (int(point[0]) + 1, int(point[1]) + 1), 'close': (0 + point[0] % 1) * (0 + point[1] % 1)},
  64.     ]
  65.     for n in neighbors:
  66.         n['coord'] = normalize(n['coord'], source_map.size)
  67.         n['color'] = source_map.getpixel(n['coord'])
  68.     color = tuple([int(sum([n['color'][channel]*n['close'] for n in neighbors])) for channel in range(3)])
  69.  
  70.     if point[0] < source_map.size[0] and point[1] < source_map.size[1]:
  71.         output_map.putpixel((i, j), color)
  72.  
  73.  
  74. def project_rectangular_bitmap_to_boreal_triaxial(source=source_map_path, output_size=360):
  75.     for s in sectors:
  76.         s['range'] = sorted([s['axial'], s['interaxial']])
  77.     source_map = Image.open(source).convert('RGB')
  78.     r, g, b = source_map.getpixel((0, 0))
  79.     output_map = Image.new('RGB', (output_size, output_size), background)
  80.     for i in range(output_size):
  81.         for j in range(output_size):
  82.             project_point(i, j, source_map, output_map, output_size)
  83.     output_map.save(output_map_path)
  84.  
  85.  
  86. project_rectangular_bitmap_to_boreal_triaxial(source_map_path, output_map_size)
  87.  
Advertisement
Add Comment
Please, Sign In to add comment