-
Notifications
You must be signed in to change notification settings - Fork 0
/
draw.py
143 lines (111 loc) · 4.81 KB
/
draw.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# coding: utf-8
import contextlib
# import ssl
import json
import tempfile
import urllib2
from rexec import FileWrapper
import numpy as np
from PIL import Image
from PIL import ImageDraw
from settings import CINEMA_HALL, KINOHOD_API_KEY, SIGN_RUB
FILL_HARD_GREY = (138, 138, 138, 64)
FILL_GREY = (158, 158, 158, 64)
FILL_BLACK = (100, 100, 100, 64)
LIGHT_GREY = [221, 221, 221]
ORANGE = [255, 180, 0]
BLUE = [1, 137, 243]
def draw_seat_numbers(draw, section, x_offset, y_offset):
import sys
min_x, max_x = sys.maxint, -1
for seat in section['seats']:
seat_x = int(seat['x'])
if seat_x < min_x:
min_x = seat_x
if seat_x > max_x:
max_x = seat_x
for seat in section['seats']:
seat_width = int(seat['width'])
seat_height = int(seat['height'])
if int(seat['number']) == 1:
draw.text((x_offset + min_x - 1. * seat_width,
int(seat['y']) + y_offset - seat_height / 3),
str(seat['row']),
fill=(138, 138, 138, 64))
draw.text((x_offset + max_x + seat_width,
int(seat['y']) + y_offset - seat_height / 3),
str(seat['row']),
fill=FILL_HARD_GREY)
def draw_window(draw, picture_width, x_offset, y_offset):
draw.polygon([(2 * x_offset, y_offset - x_offset / 2),
(picture_width - 2 * x_offset, y_offset - x_offset / 2),
(picture_width - 2 * x_offset, y_offset),
(2 * x_offset, y_offset)],
fill=FILL_GREY)
def draw_cinemahall(schedule_id):
url = CINEMA_HALL.format(schedule_id, KINOHOD_API_KEY)
x_offset = 15
y_offset = 30
shift = 10
# context = ssl._create_unverified_context()
with contextlib.closing(urllib2.urlopen(url)) as jf:
html_data = json.loads(jf.read())
for info in html_data:
for section in info['sections']:
picture_width = int(section['width']) + 2 * x_offset
picture_height = int(section['height']) + y_offset
data = np.zeros((picture_height, picture_width, 3),
dtype=np.uint8)
data[:, :] = [242, 242, 242]
low_price, high_price = 0, 0
for counter, seat in enumerate(section['seats']):
x, y = (int(seat['x']) + x_offset + shift / 2,
int(seat['y']) + y_offset)
seat_height, seat_width = (int(seat['height']),
int(seat['width']))
if seat['status'] == 'vacant':
if seat['class'] == 'color1':
low_price = seat['price']
data[y - seat_height / 2: y + seat_height / 2,
x - seat_width / 2:
x + seat_width / 2] = BLUE
if seat['class'] == 'color2':
high_price = seat['price']
data[y - seat_height / 2: y + seat_height / 2,
x - seat_width / 2:
x + seat_width / 2] = ORANGE
else:
data[y - seat_height/2: y + seat_height/2,
x - seat_width/2: x + seat_width/2] = LIGHT_GREY
# draw price icons
x_base = x_offset + (shift / 2) + 2 * seat_width
data[picture_height - int(1.25 * seat_height):
picture_height - int(.25 * seat_height),
x_base - seat_width / 2:
x_base + seat_width / 2] = BLUE
x_base_orange = x_base + 4 * seat_width
if high_price > low_price:
data[picture_height - int(1.25 * seat_height):
picture_height - int(.25 * seat_height),
x_base_orange - seat_width / 2:
x_base_orange + seat_width / 2] = ORANGE
img = Image.fromarray(data)
draw = ImageDraw.Draw(img)
draw_seat_numbers(draw, section, x_offset, y_offset)
# draw price texts
draw.text((x_base + seat_width,
picture_height - int(1. * seat_height)),
'{} {}'.format(low_price, SIGN_RUB),
fill=FILL_BLACK)
if high_price > low_price:
high_str = '{} {}'.format(high_price, SIGN_RUB)
draw.text((x_base_orange + seat_width,
picture_height - int(1. * seat_height)),
high_str,
fill=FILL_BLACK)
draw_window(draw, picture_width, x_offset, y_offset)
tmpfile = tempfile.TemporaryFile()
img.save(tmpfile, format='bmp', quality=80)
tmpfile.seek(0)
wrapper = FileWrapper(tmpfile)
return wrapper