forked from migurski/Extractotron
-
Notifications
You must be signed in to change notification settings - Fork 0
/
compose-city-previews.py
54 lines (39 loc) · 1.52 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
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.OpenStreetMap import Provider
from ModestMaps.Geo import Location
from ModestMaps.Core import Point
provider = Provider()
dimensions = Point(310, 200)
cities = list(DictReader(open('cities.txt'), dialect='excel-tab'))
try:
(previews, ) = argv[1:]
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))
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'])