Create a gist now

Instantly share code, notes, and snippets.

Japanese prefecture emoji exporter
# -*- coding: utf-8 -*-
# License: Public Domain
from __future__ import unicode_literals, print_function
import math
import cairosvg
from PIL import Image
from kartograph import Kartograph
prefs = [
'hokkaido',
'aomori',
'iwate',
'miyagi',
'akita',
'yamagata',
'fukushima',
'ibaraki',
'tochigi',
'gunma',
'saitama',
'chiba',
'tokyo',
'kanagawa',
'niigata',
'toyama',
'ishikawa',
'fukui',
'yamanashi',
'nagano',
'gifu',
'shizuoka',
'aichi',
'mie',
'shiga',
'kyoto',
'osaka',
'hyogo',
'nara',
'wakayama',
'tottori',
'shimane',
'okayama',
'hiroshima',
'yamaguchi',
'tokushima',
'kagawa',
'ehime',
'kochi',
'fukuoka',
'saga',
'nagasaki',
'kumamoto',
'oita',
'miyazaki',
'kagoshima',
'okinawa'
]
style = '''
#japan {
fill: #36A536;
stroke: none;
}
'''
def filter_factory(name):
def fn(data):
if data['adm0_a3'] == 'JPN':
if data['name'].decode(encoding='utf-8') == name:
return True
elif data['woe_label'].decode(encoding='utf-8').startswith(name):
return True
return False
return fn
for pref in prefs:
pref_cap = pref.capitalize()
print('Extracting %s...' % pref_cap)
config = {
'layers': {
'japan': {
'src': 'ne_10m_admin_1_states_provinces.shp',
'filter': filter_factory(pref_cap)
}
},
'proj': {
'id': 'mercator',
'lon0': 140
}
}
out_name = 'pref_%s' % pref
k = Kartograph()
k.generate(config, outfile='%s.svg' % out_name, stylesheet=style)
cairosvg.svg2png(url='%s.svg' % out_name, write_to='orig_%s.png' % out_name)
png = Image.open('orig_%s.png' % out_name)
longest = max(png.size)
sq = Image.new(png.mode, (longest, longest))
new_x = int(math.floor((longest - png.width) / 2))
new_y = int(math.floor((longest - png.height) / 2))
sq.paste(png, (new_x, new_y, new_x + png.width, new_y + png.height))
sq = sq.resize((128, 128), Image.ANTIALIAS)
sq.save('pref_%s.png' % pref)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment