-
Notifications
You must be signed in to change notification settings - Fork 0
/
wallpaper.py
83 lines (64 loc) · 2.3 KB
/
wallpaper.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
"""
Sunday Morning Walls
Usage:
wallpaper.py [COOLORS_URL] [--x=<x> --y=<y>] [--outdir DIR]
wallpaper.py [--c=<c>] [--x=<x> --y=<y>] [--outdir DIR]
wallpaper.py (-h | --help)
wallpaper.py (-v | --version)
Options:
-h --help Show this screen
-v --version Print version information
--x=<x> Specify the width of the output image [default: 1800]
--y=<y> Specify the height of the output image [default: 1200]
-o DIR --outdir DIR The path of the output folder [default: out]
--c=<c> How many wallpapers to generate [default: 1]
"""
from PIL import Image, ImageDraw
from collections import namedtuple
from docopt import docopt
from time import sleep
import sys
import colourtools
Xy = namedtuple('Xy', ['x', 'y'])
def hexToDec(hex):
return int(hex, 16)
def parseCoolorsUrl(url):
start = url.rfind('/')
hexes = url[start+1:].split('-')
colours = []
for hex in hexes:
rgb = []
# convert each 2 character chunk in hex code to decimal
for i in range(0, len(hex), 2):
rgb.append(hexToDec(hex[i:i+2]))
colours.append(tuple(rgb))
return colours
def generate(arguments):
SIZE = Xy(int(arguments['--x']), int(arguments['--y']))
OUTFILE_EXT = 'PNG'
OUTDIR = arguments['--outdir']
COOLORS_URL = arguments['COOLORS_URL']
NUM_COLOURS = 5
im = Image.new('RGB', SIZE)
draw = ImageDraw.Draw(im)
if COOLORS_URL == None:
colours = colourtools.generatePalette()
TAG = '_'.join(['{}-{}-{}'.format(col.r, col.g, col.b) for col in colours])
else:
colours = parseCoolorsUrl(COOLORS_URL)
TAG = COOLORS_URL[COOLORS_URL.rfind('/')+1:]
OUTFILE = '{}/wallpaper-{}.{}'.format(OUTDIR, TAG, OUTFILE_EXT)
stripeWidth = SIZE.x / len(colours)
for i, colour in enumerate(colours):
topLeft = Xy(stripeWidth * i, 0)
bottomRight = Xy(topLeft.x + stripeWidth, SIZE.y)
rect = (topLeft, bottomRight)
draw.rectangle(rect, fill=colour)
im.save(OUTFILE, 'PNG')
if __name__ == '__main__':
print(sys.argv)
arguments = docopt(__doc__, version='Walls 2.0')
for c in range(int(arguments['--c'])):
print("Generating a new wallpaper...")
generate(arguments)
print('Done!')