-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathmusic.py
More file actions
410 lines (340 loc) · 14.6 KB
/
music.py
File metadata and controls
410 lines (340 loc) · 14.6 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
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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
# coding: utf-8
from django.http import HttpResponse
from django.http import HttpResponseRedirect
from django.http import HttpResponseServerError
from django.shortcuts import render
from django.urls import reverse
from django.views import View
from leancloud import Object
from leancloud import Query
from leancloud.errors import LeanCloudError
from qiniu import Auth, set_default, etag, PersistentFop, build_op, op_save, Zone
from qiniu import put_data, put_file, put_stream
from qiniu import BucketManager, build_batch_copy, build_batch_rename, build_batch_move, build_batch_stat, build_batch_delete
from qiniu import urlsafe_base64_encode, urlsafe_base64_decode
from PIL import Image, ImageColor, ImageFont, ImageDraw, ImageFilter
from io import BytesIO
from util import Util
from textwrap import *
import requests
from haishoku.haishoku import Haishoku
import re
import os
import base64
import json
class Card(Object):
pass
class Photo(Object):
pass
class User(Object):
pass
class Music():
@staticmethod
def generate(card):
try:
if(card.get('photo') is None):
msstream = BytesIO()
data = {'title':card.get('name'),'content':card.get('content'),'author':card.get('author'),'url':card.get('img_url')}
Music.music(data,msstream)
url = os.environ["QINIU_ACCESS_URL"]
access_key = os.environ["QINIU_ACCESS_KEY"]
secret_key = os.environ["QINIU_SECRET_KEY"]
#构建鉴权对象
q = Auth(access_key, secret_key)
#要上传的空间
bucket_name = 'card'
key = card.get('objectId')
token = q.upload_token(bucket_name)
ret, info = put_data(token, key, msstream.getvalue())
if(info.ok()):
metaData = {'owner':card.get('username')}
photo = Photo()
photo.set('mine_type','image/jpeg')
photo.set('key',key)
photo.set('name',key)
photo.set('url',url+'/'+key)
photo.set('provider','qiniu')
photo.set('metaData',metaData)
photo.set('bucket',bucket_name)
photo.save()
update = Card.create_without_data(key)
update.set('photo',photo)
update.save()
return 'ok'
else:
return 'failed'
return 'already'
except LeanCloudError as e:
if e.code == 101: # 服务端对应的 Class 还没创建
card = ''
return HttpResponse(e,content_type="text/plain")
else:
raise e
return HttpResponse(e,content_type="text/plain")
@staticmethod
def music(data,msstream):
w = 490*2
h = 740*2
banner_w = 490*2
banner_h = 235*2
cover_w = 200*2
cover_h = 200*2
cover_top = 120*2
cover_left = 70*2
cover_border = 5*2
block_w = 32*2
block_h = 12*2
block_left = 97*2
block_top = 160*2 + banner_h
max_content_w = 300*2
max_title_w = 200*2
title_left = cover_w + cover_left + 10 *2
title = '成都'
if 'title' in data:
title = data['title']
title_font = ImageFont.truetype('font/zh/YueSong.ttf',28*2)
single_title_w,single_title_h= title_font.getsize("已")
titles = list(title)
title_formated = ''
temp = ''
for word in titles:
temp += word
temp_w,temp_h = title_font.getsize(temp)
title_formated += word
if temp_w+ single_title_w > max_title_w :
title_formated += '\n'
temp = ''
tlines = len(title_formated.split('\n'))
if tlines > 1:
title_h = tlines * single_title_h + (tlines -1) * 15*2
else:
title_h = tlines * single_title_h
title_top = banner_h - title_h - 10 *2
max_author_w = 200*2
author_left = cover_w + cover_left+ 10 *2
author_top = banner_h + 10 *2
author = '赵雷'
if 'author' in data:
author = data['author']
author_font = ImageFont.truetype('font/zh/YueSong.ttf',14*2)
single_author_w,single_author_h = author_font.getsize("已")
authors = list(author)
author_formated = ''
temp = ''
for word in authors:
temp += word
temp_w,temp_h = author_font.getsize(temp)
author_formated += word
if temp_w + single_author_w > max_author_w :
author_formated += '\n'
temp = ''
alines = len(author_formated.split('\n'))
author_h = alines * single_author_h + (alines -1) * 14*2
content_left = cover_left
content_top = banner_h + 150*2
content = '让我掉下眼泪的\n不止昨夜的酒\n让我依依不舍的\n不止你的温柔\n余路还要走多久\n你攥着我的手\n让我感到为难的\n是挣扎的自由'
if 'content' in data:
content = data['content']
content_formated = ''
content_font = ImageFont.truetype('font/zh/YueSong.ttf',18*2)
single_content_w,single_content_h = content_font.getsize("已")
lines = content.split('\n')
for line in lines:
contents = list(line)
line_formated = ''
temp = ''
for word in contents:
temp += word
temp_w,temp_h = content_font.getsize(temp)
line_formated += word
if temp_w + single_content_w > max_content_w :
line_formated += '\n'
temp = ''
if temp != '':
line_formated += '\n'
content_formated += line_formated
print(content_formated)
clines = len(content_formated.split('\n'))
content_h = clines * single_author_h + (clines -1) * 14*2
h = max(h,content_top + content_h + 100*2)
base = Image.new('RGBA',(w,h),(255,255,255,255))
url = "https://y.gtimg.cn/music/photo_new/T001R150x150M000003CNC9D00CaVx.jpg"
if 'url' in data:
url = data['url']
file = BytesIO(requests.get(url).content)
photo = Image.open(file).convert('RGBA')
(pw, ph) = photo.size
#r,g,b = Haishoku.getDominant(file)
if pw/ph>banner_w/banner_h:
bbox = ((pw-ph*banner_w/banner_h)/2,0,(pw+ph*banner_w/banner_h)/2,ph)
else:
bbox = (0,(ph-pw*banner_h/banner_w)/2,pw,(ph+pw*banner_h/banner_w)/2)
draw = ImageDraw.Draw(base)
banner_cover = photo.crop(bbox)
banner_cover = banner_cover.resize((banner_w,banner_h),Image.ANTIALIAS)
banner_blur = banner_cover.filter(ImageFilter.GaussianBlur(40))
banner_wrap = Image.new('RGBA',(banner_w,banner_h),(0, 0, 0, 153))
banner_mask = Image.alpha_composite(banner_blur,banner_wrap)
base.paste(banner_mask,box=(0,0))
if pw/ph>cover_w/cover_h:
box = ((pw-ph*cover_w/cover_h)/2,0,(pw+ph*cover_w/cover_h)/2,ph)
else:
box = (0,(ph-pw*cover_h/cover_w)/2,pw,(ph+pw*cover_h/cover_w)/2)
cover = photo.crop(box)
cover = cover.resize((cover_w-cover_border*2,cover_h-cover_border*2),Image.ANTIALIAS)
draw.rectangle((cover_left,cover_top,cover_left+cover_w,cover_top+cover_h),fill=(255,255,255,255))
base.paste(cover,box=(cover_left+cover_border,cover_top+cover_border))
draw.multiline_text((title_left,title_top), title_formated, font=title_font, fill=(255,255,255,255), align='left',spacing=15*2)
draw.multiline_text((author_left,author_top), author_formated, font=author_font, fill=(0,0,0,255), align='left',spacing=15*2)
draw.multiline_text((content_left,content_top), content_formated, font=content_font, fill=(0,0,0,255), align='left',spacing=12*2)
#base.show()
# save image data to output stream
base.save(msstream,"jpeg")
# release memory
base.close()
@staticmethod
def music_v2(data,msstream):
w = 490*2
h = 740*2
banner_w = 490*2
banner_h = 235*2
cover_w = 140*2
cover_h = 140*2
cover_top = 120*2
cover_left = 100*2
block_w = 32*2
block_h = 12*2
block_left = 97*2
block_top = 160*2 + banner_h
max_content_w = 300*2
max_title_w = 190*2
title_left = cover_w + cover_left + 10 *2
title = '成都'
if data['title']:
title = data['title']
title_font = ImageFont.truetype('font/zh/YueSong.ttf',28*2)
single_title_w,single_title_h= title_font.getsize("已")
titles = list(title)
title_formated = ''
temp = ''
for word in titles:
temp += word
temp_w,temp_h = title_font.getsize(temp)
title_formated += word
if temp_w > max_title_w + single_title_w:
title_formated += '\n'
temp = ''
tlines = len(title_formated.split('\n'))
title_h = tlines * single_title_h + (tlines -1) * 28*2
title_top = banner_h - title_h - 10 *2
max_author_w = 190*2
author_left = cover_w + cover_left+ 10 *2
author_top = banner_h + 10 *2
author = '赵雷'
if data['author']:
author = data['author']
author_font = ImageFont.truetype('font/zh/YueSong.ttf',14*2)
single_author_w,single_author_h = author_font.getsize("已")
authors = list(author)
author_formated = ''
temp = ''
for word in authors:
temp += word
temp_w,temp_h = author_font.getsize(temp)
author_formated += word
if temp_w > max_author_w + single_author_w:
author_formated += '\n'
temp = ''
alines = len(author_formated.split('\n'))
author_h = alines * single_author_h + (alines -1) * 14*2
content_left = 95*2
content_top = banner_h + 150*2
content = '让我掉下眼泪的\n不止昨夜的酒\n让我依依不舍的\n不止你的温柔\n余路还要走多久\n你攥着我的手\n让我感到为难的\n是挣扎的自由'
if data['content']:
content = data['content']
content_formated = ''
content_font = ImageFont.truetype('font/zh/YueSong.ttf',18*2)
single_content_w,single_content_h = content_font.getsize("已")
lines = content.split('\n')
for line in lines:
contents = list(line)
line_formated = ''
temp = ''
for word in contents:
temp += word
temp_w,temp_h = content_font.getsize(temp)
line_formated += word
if temp_w > max_content_w + single_content_w:
line_formated += '\n'
temp = ''
if temp != '':
line_formated += '\n'
content_formated += line_formated
print(content_formated)
clines = len(content_formated.split('\n'))
content_h = clines * single_author_h + (clines -1) * 14*2
h = max(h,content_top + content_h + 100*2)
base = Image.new('RGBA',(w,h),(255,255,255,255))
url = "https://y.gtimg.cn/music/photo_new/T001R150x150M000003CNC9D00CaVx.jpg"
if data['url']:
url = data['url']
file = BytesIO(requests.get(url).content)
photo = Image.open(file).convert('RGBA')
(pw, ph) = photo.size
#r,g,b = Haishoku.getDominant(file)
if pw/ph>w/h:
bbox = ((pw-ph*w/h)/2,0,(pw+ph*w/h)/2,ph)
else:
bbox = (0,(ph-pw*h/w)/2,pw,(ph+pw*h/w)/2)
txt = Image.new('L', (w,h), 255)
draw = ImageDraw.Draw(txt)
alpha = Image.new('L', (w,h), 0)
draw.multiline_text((title_left,title_top), title_formated, font=title_font, fill=0, align='left',spacing=15*2)
draw.multiline_text((author_left,author_top), author_formated, font=author_font, fill=0, align='left',spacing=15*2)
draw.multiline_text((content_left,content_top), content_formated, font=content_font, fill=0, align='left',spacing=12*2)
alpha.paste(txt, (0, 0))
banner_cover = photo.crop(bbox)
banner_cover = banner_cover.resize((w,h),Image.ANTIALIAS)
banner_blur = banner_cover.filter(ImageFilter.GaussianBlur(80))
banner_wrap = Image.new('RGBA',(w,h),(255, 255, 255, 193))
banner_mask = Image.alpha_composite(banner_blur,banner_wrap)
banner_mask.putalpha(alpha)
banner_blur.paste(banner_mask,box=(0,0),mask=banner_mask)
base.paste(banner_blur,box=(0,0))
if pw/ph>cover_w/cover_h:
box = ((pw-ph*cover_w/cover_h)/2,0,(pw+ph*cover_w/cover_h)/2,ph)
else:
box = (0,(ph-pw*cover_h/cover_w)/2,pw,(ph+pw*cover_h/cover_w)/2)
cover = photo.crop(box)
cover = cover.resize((cover_w,cover_h),Image.ANTIALIAS)
base.paste(cover,box=(cover_left,cover_top))
# save image data to output stream
base.save(msstream,"jpeg")
# release memory
base.close()
@staticmethod
def preview(request,param):
try:
#param = reuest.GET.get('data')
json_str = base64.b64decode(param).decode('utf-8')
data = json.loads(json_str)
url = data['url']
title = data['title']
author = data['author']
content = data['content']
print(content)
data = {'url':url,'title':title,'author':author,'content':content}
msstream = BytesIO()
Music.music(data,msstream)
return HttpResponse(msstream.getvalue(),content_type="image/png")
except LeanCloudError as e:
if e.code == 101: # 服务端对应的 Class 还没创建
card = ''
return HttpResponse(e,content_type="text/plain")
else:
raise e
return HttpResponse(e,content_type="text/plain")
if __name__ == "__main__":
data = {}
msstream = BytesIO()
Music.music(data,msstream)