-
Notifications
You must be signed in to change notification settings - Fork 81
/
compose-city-previews.py
57 lines (41 loc) · 1.68 KB
/
compose-city-previews.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
from sys import argv, stderr
from csv import DictReader
try:
from PIL import Image
from PIL.ImageDraw import ImageDraw
except ImportError:
import Image
from ImageDraw import ImageDraw
from ModestMaps import mapByExtent
from ModestMaps.Providers import TemplatedMercatorProvider
from ModestMaps.Geo import Location
from ModestMaps.Core import Point
provider = TemplatedMercatorProvider('http://otile1.mqcdn.com/tiles/1.0.0/osm/{Z}/{X}/{Y}.jpg')
dimensions = Point(310, 200)
cities = list(DictReader(open('cities.txt'), dialect='excel-tab'))
try:
previews, wanted = argv[1], argv[2:]
except ValueError:
print >> stderr, 'Usage: compose-city-previews.py <previews directory>'
exit(1)
for city in cities:
if not city['name']:
raise Exception('Need a name for ' + str(city))
if wanted and city['slug'] not in wanted:
continue
print >> stderr, city['name'], '...',
north, west = float(city['top']), float(city['left'])
south, east = float(city['bottom']), float(city['right'])
mmap = mapByExtent(provider, Location(north, west), Location(south, east), dimensions)
ul = mmap.locationPoint(Location(north, west))
lr = mmap.locationPoint(Location(south, east))
bbox = [(p.x, p.y) for p in (ul, lr)]
img = mmap.draw()
mask = Image.new('L', img.size, 0x99)
ImageDraw(mask).rectangle(bbox, fill=0x00)
img.paste((0xFF, 0xFF, 0xFF), (0, 0), mask)
frame = Image.new('L', img.size, 0x00)
ImageDraw(frame).rectangle(bbox, outline=0x33)
img.paste((0x00, 0x00, 0x00), (0, 0), frame)
img.save('%s/%s.jpg' % (previews, city['slug']), quality=95)
print >> stderr, '%s/%s.jpg' % (previews, city['slug'])