forked from ringcentral/slate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreate_product_resolution_tables.py
299 lines (250 loc) · 8.2 KB
/
create_product_resolution_tables.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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
from math import ceil
import requests
__author__ = 'dbotha'
import json
CM_TO_INCHES_RATIO = 1 / 2.54
PRINT_DPI = 300
API_KEY = "2778e89d274cf8ecbbad3fbba180cc5e62d0997c"
# GROUPS AND PRODUCT ORDERING IN THIS NEXT LIST DRIVES OUTPUT TABLES
PRODUCT_ORDER = [
# Print Products
("OPTIMAL PRINT PRODUCT ASSET DIMENSIONS", [
"a1_poster",
"a2_poster",
"a3_poster",
"a4_poster",
"photos_4x6",
"magnets",
"squares",
"squares_mini",
"polaroids",
"polaroids_mini",
"greeting_cards",
"greeting_cards_a5",
"greeting_cards_7x5",
"stickers_square",
"stickers_circle",
"frames_50cm",
"postcard"
#"a2_poster_24",
#"a1_poster_70",
#"frames_50cm_4x4",
#"frames_50cm_3x3",
#"a2_poster_54",
#"a5_flyer_500",
#"greeting_cards_a5_10pack",
#"greeting_cards_7x5_10pack",
#"a2_poster_35",
#"a1_poster_54",
#"a1_poster_35",
#"a5_flyer_1000",
#"frames_50cm_2x2",
]),
# WOYC
("OPTIMAL PHONE CASE ASSET DIMENSIONS", [
"i6splus_tough_case",
"i6splus_case",
"i6s_case",
"i6s_tough_case",
"i6plus_tough_case",
"i6plus_case",
"i6plus_folio_case",
"i6_tough_case",
"i6_case",
"i6_folio_case",
"i6_bakpak_1_case",
"i6_bakpak_3_case",
"i5_tough_case",
"i5_case",
"i5_clik_case",
"i5c_tough_case",
"i5c_case",
"i4_tough_case",
"i4_case",
"samsung_s6e_tough_case",
"samsung_s6e_case",
"samsung_s6_tough_case",
"samsung_s6_case",
"samsung_s5_tough_case",
"samsung_s5_case",
"samsung_s5_mini_case",
"samsung_s4_tough_case",
"samsung_s4_case",
"samsung_s4_mini_tough_case",
"samsung_s4_mini_case",
"samsung_s3_tough_case",
"samsung_s3_case",
"samsung_s3_mini_case",
"samsung_n4_tough_case",
"samsung_n4_case",
"samsung_n3_case",
"sony_x_z1_case",
"sony_x_c_case",
"lg_g2_case",
"moto_g_case",
"nexus_5_case",
]),
("OPTIMAL TABLET CASE DIMENSIONS", [
"ipad_mini_case",
"ipad_case",
"ipad_air_case",
"nexus_7_case",
]),
# Apparel
("OPTIMAL APPAREL ASSET DIMENSIONS", [
#"gildan_hoodie",
#"gildan_hoodie_zipped",
#"gildan_tshirt"
]),
# Photo Books
# RPI Products
("OPTIMAL PHOTOBOOK ASSET DIMENSIONS", [
# "photobook_small_landscape",
# "photobook_small_portrait",
"rpi_wrap_321x270_sm",
"rpi_wrap_210x280_sm",
#"rpi_wrap_280x210_sm_40pg",
"rpi_wrap_280x210_sm",
#"rpi_wrap_280x210_sm_100pg",
"rpi_wrap_300x300_sm",
"rpi_wrap_210x210_sm",
#"rpi_soft_210x210_s",
"rpi_wrap_140x140_sm",
#"rpi_soft_280x210_s",
#"rpi_soft_210x280_s"
]),
# Photobox Products
("OPTIMAL PHOTOBOX ASSET DIMENSIONS", [
"i6ptc7",
"i6cc7",
"i5c4",
"iphone5c1",
"i5ccc1",
"stickeri5c1",
"i4c9",
"i4ccc1",
"stickeri4c1",
"sgs5c1",
"sgs4c3",
"sgs3c4",
"sgs2c1",
"ipad_case_1",
"ipadminic1",
"ipadsmartc6",
"ipadminismartc1",
"pbx_canvas_20x30",
"pbx_canvas_30x20",
"pbx_canvas_30x30",
"pbx_canvas_40x40",
"pbx_polaroids_12",
"pbx_polaroids_24",
"pbx_polaroids_36",
"pbx_a3",
"pbx_6x4",
"pbx_7x5",
"pbx_squares_5x5",
"pbx_squares_8x8",
"pbx_magnets_8x8",
"pbx_magnets_13x9",
"pbx_magnets_15x10",
"pbx_stickers_13x9",
"pbx_cards_a6_10pack",
"mgth10",
"mug_bone_wrap",
"pbx_frame_30x30",
"pbx_canvas_30x40",
"23540-3mbook_1"
]),
# S9 Products
("OPTIMAL STICKY9 ASSET DIMENSIONS", [
# "s9_polaroids_12",
# "s9_magnets",
# "s9_magnets_mini",
# "s9_squares",
# "s9_squares_single",
# "s9_squares_mini_single",
# "s9_poster",
# "s9_greetingcard",
# #"s9_packframe",
# "s9_jigsaw",
# "s9_squares_mini"
]),
# Albumprinter Products
("OPTIMAL ALBELLI ASSET DIMENSIONS", [
"ap_aluminium_400x400",
"ap_aluminium_300x400",
"ap_aluminium_400x300",
"ap_mounted_400x400",
"ap_mounted_400x300",
"ap_mounted_300x400",
"ap_canvas_400x300",
"ap_canvas_300x400",
"ap_canvas_400x400",
"ap_wood_400x300",
"ap_wood_300x400",
"ap_wood_400x400",
"ap_acrylic_300x400",
"ap_acrylic_400x300",
"ap_acrylic_400x400",
#"ap_album_210x210",
"metallic_8x8"
])
]
url = 'https://api.kite.ly/v2.0/template/?limit=200'
headers = {'Authorization': ('ApiKey %s:' % API_KEY)}
r = requests.get(url, headers=headers)
r_json = r.json()
def get_dimension_safe(size, unit, side, default):
if not size.get(unit, False):
return default
return size.get(unit).get(side, default)
def calculate_sensible_sizes(size):
width_px = get_dimension_safe(size, "px", "width", 0)
height_px = get_dimension_safe(size, "px", "height", 0)
width_cm = get_dimension_safe(size, "cm", "width", 0)
height_cm = get_dimension_safe(size, "cm", "height", 0)
width_inch = get_dimension_safe(size, "inch", "width", 0)
height_inch = get_dimension_safe(size, "inch", "height", 0)
# convert cm & inches into px @ 300dpi then go with the largest
width_cm_in_px = width_cm * CM_TO_INCHES_RATIO * PRINT_DPI
height_cm_in_px = height_cm * CM_TO_INCHES_RATIO * PRINT_DPI
width_inch_in_px = width_inch * PRINT_DPI
height_inch_in_px = height_inch * PRINT_DPI
base_width_px = width_inch_in_px
base_height_px = height_inch_in_px
base_area_px = base_width_px * base_height_px
ROUNDING_ERROR_FUDGE_FACTOR_PX = 2 # Favour using pixels as they're likely "pretty" as rounding errors may have bumped inch_px or cm_px slightly larger
if (width_px + ROUNDING_ERROR_FUDGE_FACTOR_PX) * (height_px + ROUNDING_ERROR_FUDGE_FACTOR_PX) > base_area_px:
base_width_px = width_px
base_height_px = height_px
base_area_px = (width_px + ROUNDING_ERROR_FUDGE_FACTOR_PX) * (height_px + ROUNDING_ERROR_FUDGE_FACTOR_PX)
if width_cm_in_px * height_inch_in_px > base_area_px:
base_width_px = width_cm_in_px
base_height_px = height_cm_in_px
if base_width_px < 500 or base_height_px < 500:
return 0, 0, 0, 0, 0, 0
base_width_px = ceil(base_width_px)
base_height_px = ceil(base_height_px)
return (base_width_px, base_height_px,
(base_width_px / PRINT_DPI) / CM_TO_INCHES_RATIO, (base_height_px / PRINT_DPI) / CM_TO_INCHES_RATIO,
base_width_px / PRINT_DPI, base_height_px / PRINT_DPI)
def fn(num):
return format(num, '.1f').rstrip('0').rstrip('.')
def find_product_with_template_id(products, template_id):
for product in products:
if product["template_id"] == template_id:
return product
for (group_name, templates) in PRODUCT_ORDER:
if len(templates) == 0:
continue
print("### %s" % group_name)
print('<table class="apparel-positions"><thead><tr><th>product</th><th>pixels</th><th>cm</th><th>inches</th></tr></thead><tbody>')
for template_id in templates:
product = find_product_with_template_id(r_json["objects"], template_id)
name = product["name"]
template_id = product["template_id"]
size = product["product"]["size"]
(width_px, height_px, width_cm, height_cm, width_inch, height_inch) = calculate_sensible_sizes(size)
print('<tr><td>%s<span class="optional-argument">%s</span></td><td><code class="prettyprint">%s×%s</code></td><td><code class="prettyprint">%s×%s</code></td><td><code class="prettyprint">%s×%s</code></td></tr>' % (name, template_id, fn(width_px), fn(height_px), fn(width_cm), fn(height_cm), fn(width_inch), fn(height_inch)))
print("</tbody></table>")
print("")