forked from originalankur/maptoposter
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paththeme-swatches.py
More file actions
81 lines (67 loc) · 1.74 KB
/
Copy paththeme-swatches.py
File metadata and controls
81 lines (67 loc) · 1.74 KB
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#!/usr/bin/env python
# coding: utf-8
import json
import seaborn as sns
from os import listdir
from io import BytesIO
import matplotlib.pyplot as plt
from PIL import Image
def invert_hex(h):
"""
Take a hexadecimal color and return it's inverse.
Parameters:
h (str): hexadecimal color
Returns:
h_inv (str): inverted hexadecimal color
Examples:
>>> invert_hex('#000000')
#FFFFFF
>>> invert_hex('##E0E0E0')
#1F1F1F
"""
h_inv = "#{:02X}{:02X}{:02X}".format(*(255-int(h[i:i+2], 16) for i in (1,3,5)))
return h_inv
def draw_swatch(colors,ax):
#for color in colors:
# sns.palplot(color)
return sns.palplot(colors)
# Initialize seaborn
sns.set()
sw_width = 1084
sw_height = 100
theme_list = listdir('themes')
width = sw_width
height = sw_height*len(theme_list)
swatches = Image.new('RGB', (width, height))
y_offset = 0
# Loop through all themes, drawing a swatch for each theme
for theme_file in theme_list:
with open('themes/' + theme_file) as th:
# load theme
theme = json.load(th)
# extract theme name and delete
theme_name = theme.pop('name')
theme_description = theme.pop('description')
# Draw swatch
sns.palplot(theme.values())
# Add title to swatch
plt.title(theme_name)
# Add key and hexadecimal color
for i, key in enumerate(theme):
plt.text(
i, 0.2,
key+'\n'+theme[key],
ha='center',
va='bottom',
color=invert_hex(theme[key]),
fontsize=7
)
plt.tight_layout()
buf = BytesIO()
plt.savefig(buf)
buf.seek(0)
sw = Image.open(buf)
swatches.paste(sw, (0,y_offset))
y_offset += sw_height
plt.close()
swatches.save('theme_swatches.png')