3 # Copyright (C) 2014 Antonio Ospite <ao2@ao2.it>
5 # This program is free software: you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation, either version 3 of the License, or
8 # (at your option) any later version.
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
15 # You should have received a copy of the GNU General Public License
16 # along with this program. If not, see <http://www.gnu.org/licenses/>.
24 def polar2cartesian(r, theta, offset_angle=0):
26 theta expected in degrees
28 theta = radians(theta) - offset_angle
35 def usage(program_name):
36 sys.stderr.write("usage: %s <input file> <output file>\n" % program_name)
39 if __name__ == "__main__":
48 width = (map_height + internal_radius) * 2 * scale
51 img = Image.open(sys.argv[1]).convert('RGBA')
53 if w != width or h != height:
54 sys.stderr.write("Error: image must be 640x640")
56 colors = img.getcolors(w*h)
57 palette = img.getpalette()
58 pixels = img.getdata()
60 output_image = Image.new("RGB", (map_width, map_height), "black")
62 external_radius = width / 2. - 1
63 radius = external_radius - internal_radius
65 for i in range(0, map_height):
66 for j in range(0, map_width):
67 r = radius / map_height * (i + 0.5) + internal_radius
68 theta = (360. / map_width) * (j + 0.5)
69 x, y = polar2cartesian(r, theta, -pi/2.)
70 px, py = int(x + radius), int(y + radius)
71 sample = img.getpixel((px, py))
73 sample = (0, 0, 0, 255)
74 output_image.putpixel((j, i), sample)
76 output_image.save(sys.argv[2], "PNG")