-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathgenerate.py
More file actions
430 lines (381 loc) · 14.8 KB
/
Copy pathgenerate.py
File metadata and controls
430 lines (381 loc) · 14.8 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
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
# coding: utf-8
from datetime import datetime
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 PIL import Image, ImageColor, ImageFont, ImageDraw, ImageFilter
from io import BytesIO
from textwrap import *
import requests
import re
import leancloud
import urllib
import json
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
import os
import configparser
cf = configparser.ConfigParser()
cf.read("config.conf")
os.environ["WXA_APP_ID"] = cf.get("wxa", "app_id")
os.environ["WXA_APP_SECRET"] = cf.get("wxa", "app_secret")
os.environ["QINIU_ACCESS_KEY"] = cf.get("qiniu", "access_key")
os.environ["QINIU_SECRET_KEY"] = cf.get("qiniu", "secret_key")
os.environ["QINIU_ACCESS_URL"] = cf.get("qiniu", "access_url")
APP_ID = '7C7MfP24LboNSLeOnbh112nT-gzGzoHsz'
MASTER_KEY = 'bIEoNy5pSWoqvC3qq0vpGMT1'
leancloud.init(APP_ID, master_key=MASTER_KEY)
class Card(Object):
pass
class Photo(Object):
pass
class _User(Object):
pass
'''
print('getting total count:')
query = Query(Card)
query.does_not_exist('photo')
count = query.count()
progress = 0
print('Total:'+str(count))
cardlist = query.find()
for card in cardlist:
id = card.get('objectId')
print('generating:'+id)
result = generateCard(id)
print('result:'+result)
print('progess:('+str(progress)+'/'+str(count)+')')
progress = progress+1
else:
print('no data')
'''
def generateCard(id):
try:
card = Query(Card).get(id)
tid = card.get('temlate')
if(card.get('photo') is None):
msstream = BytesIO()
if tid == 1:
template(card,msstream)
elif tid == 2:
template2(card,msstream)
elif tid == 3:
template3(card,msstream)
elif tid == 4:
template4(card,msstream)
else:
template(card,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(id)
user = _User.create_without_data('590be679ac502e006cdc63c0')
update.set('user',user)
update.set('username','569dil3zuypnrpjqwe97l3qkw')
update.set('photo',photo)
update.save()
return 'ok'
else:
return 'failed'
else:
return 'already'
except LeanCloudError as e:
if e.code == 101: # 服务端对应的 Class 还没创建
card = ''
return 'no class'
else:
raise e
return 'error'
def template(card,msstream):
w = 640
h = 862
iw = 600
ih = 340
title = card.get('name')
content = card.get('content')
url = card.get('img_url')
spacing = 20
content = fill(content, 15)
author = '- 天天码图 -'
copyright = '微信小程序「天天码图」'
title_fnt = ImageFont.truetype('font/zh/YueSong.ttf', 35)
content_fnt = ImageFont.truetype('font/zh/YueSong.ttf', 30)
author_fnt = ImageFont.truetype('font/zh/YueSong.ttf', 25)
copyright_fnt = ImageFont.truetype('font/zh/YueSong.ttf', 25)
base = Image.new('RGBA',(w,h),(255,255,255,255))
draw = ImageDraw.Draw(base)
tw,th = draw.multiline_textsize(title, font=title_fnt)
aw,ah = draw.multiline_textsize(author, font=author_fnt)
cw,ch = draw.multiline_textsize(content, font=content_fnt, spacing=spacing)
crw,crh = draw.multiline_textsize(copyright, font=copyright_fnt)
h = 635+th+ch+crh+ah;
base = Image.new('RGBA',(w,h),(255,255,255,255))
draw = ImageDraw.Draw(base)
print(url)
#print(r.content)
file = BytesIO(requests.get(url).content)
photo = Image.open(file).convert('RGBA')
(pw, ph) = photo.size
if pw/ph>iw/ih:
box = ((pw-ph*iw/ih)/2,0,(pw+ph*iw/ih)/2,ph)
else:
box = (0,(ph-pw*ih/iw)/2,pw,(ph+pw*ih/iw)/2)
photo = photo.crop(box)
photo = photo.resize((iw,ih))
base.paste(photo,box=(20,20))
# get a drawing context
draw = ImageDraw.Draw(base)
# draw text in the middle of the image, half opacity
draw.multiline_text((w/2-tw/2,420), title, font=title_fnt, fill=(0,0,0,255), align='center')
draw.multiline_text((w/2-cw/2,420+th+45), content, font=content_fnt, fill=(0,0,0,255), align='center', spacing=spacing)
draw.multiline_text((w/2-aw/2,420+th+45+ch+115), author, font=author_fnt, fill=(0,0,0,255), align='center')
draw.multiline_text((w-crw,420+th+45+ch+115+ah+50), copyright, font=copyright_fnt, fill=(189,189,189,255), align='center')
# save image data to output stream
base.save(msstream,"jpeg")
# release memory
base.close()
def template2(card,msstream):
w = 640
h = 1020
iw = 600
ih = 340
title = card.get('name')
content = card.get('content')
url = card.get('img_url')
spacing = 20
padding = 2
author = '- 天天码图 -'
copyright = '微信小程序「天天码图」'
title_fnt = ImageFont.truetype('font/zh/YueSong.ttf', 35)
content_fnt = ImageFont.truetype('font/zh/YueSong.ttf', 30)
author_fnt = ImageFont.truetype('font/zh/YueSong.ttf', 25)
copyright_fnt = ImageFont.truetype('font/zh/YueSong.ttf', 25)
base = Image.new('RGBA',(w,h),(255,255,255,255))
draw = ImageDraw.Draw(base)
aw,ah = draw.multiline_textsize(author, font=author_fnt)
crw,crh = draw.multiline_textsize(copyright, font=copyright_fnt)
file = BytesIO(urllib.request.urlopen(url).read())
photo = Image.open(file).convert('RGBA')
(pw, ph) = photo.size
if pw/ph>iw/ih:
box = ((pw-ph*iw/ih)/2,0,(pw+ph*iw/ih)/2,ph)
else:
box = (0,(ph-pw*ih/iw)/2,pw,(ph+pw*ih/iw)/2)
photo = photo.crop(box)
photo = photo.resize((iw,ih))
base.paste(photo,box=(20,20))
# get a drawing context
draw = ImageDraw.Draw(base)
# split the title
tlines = wrap(title, 1)
# current title height
tnh = 420
# get width and height of single title word
stw,sth = title_fnt.getsize("已")
for tline in tlines:
draw.text((w-115-stw,tnh), tline, fill=(0,0,0,255), font=title_fnt)
tnh = tnh+sth
# get width and height of single content word
scw,sch = content_fnt.getsize("已")
clines = wrap(content, 14)
# current width of content
cnw = w-115-stw-115-scw
for cline in clines:
# current height of content
cnh = 420
cwords = wrap(cline, 1)
for cword in cwords:
pattern = re.compile("[,。、]+")
if pattern.search(cword):
draw.text((cnw,cnh), cword, fill=(0,0,0,255), font=content_fnt)
# draw.text((cnw+30-12,cnh-30+12), cword, fill=(0,0,0,255), font=content_fnt)
else:
draw.text((cnw,cnh), cword, fill=(0,0,0,255), font=content_fnt)
cnh = cnh+sch+padding
cnw = cnw-scw-spacing
# draw text in the middle of the image, half opacity
# draw.multiline_text((w/2-tw/2,420), title, font=title_fnt, fill=(0,0,0,255), align='center')
# draw.multiline_text((w/2-cw/2,420+th+45), content, font=content_fnt, fill=(0,0,0,255), align='center', spacing=spacing)
draw.multiline_text((w/2-aw/2,h-50-15-crh-ah), author, font=author_fnt, fill=(0,0,0,255), align='center')
draw.multiline_text((w-crw,h-15-crh), copyright, font=copyright_fnt, fill=(189,189,189,255), align='center')
# save image data to output stream
base.save(msstream,"jpeg")
# release memory
base.close()
def template3(card,msstream):
w = 640
h = 862
iw = 600
ih = 340
bw = 300
bh = 300
title = card.get('name')
content = card.get('content')
url = card.get('img_url')
spacing = 20
content = fill(content, 15)
author = '- 天天码图 -'
copyright = '微信小程序「天天码图」'
title_fnt = ImageFont.truetype('font/zh/YueSong.ttf', 35)
content_fnt = ImageFont.truetype('font/zh/YueSong.ttf', 30)
author_fnt = ImageFont.truetype('font/zh/YueSong.ttf', 25)
copyright_fnt = ImageFont.truetype('font/zh/YueSong.ttf', 25)
base = Image.new('RGBA',(w,h),(255,255,255,255))
draw = ImageDraw.Draw(base)
tw,th = draw.multiline_textsize(title, font=title_fnt)
aw,ah = draw.multiline_textsize(author, font=author_fnt)
cw,ch = draw.multiline_textsize(content, font=content_fnt, spacing=spacing)
crw,crh = draw.multiline_textsize(copyright, font=copyright_fnt)
h = 695+th+ch+crh+ah;
base = Image.new('RGBA',(w,h),(255,255,255,255))
draw = ImageDraw.Draw(base)
file = BytesIO(urllib.request.urlopen(url).read())
photo = Image.open(file).convert('RGBA')
pw, ph = photo.size
if pw > ph:
box = ((pw-ph*bw/bh)/2,0,(pw+ph*bw/bh)/2,ph)
else:
box = (0,(ph-pw*bh/bw)/2,pw,(ph+pw*bh/bw)/2)
photo = photo.crop(box)
photo = photo.resize((bw*4,bh*4))
circle = Image.new('L', (bw*4, bh*4), 0)
draw = ImageDraw.Draw(circle)
draw.ellipse((0, 0, bw*4, bh*4), fill=255)
alpha = Image.new('L', (bw*4, bh*4), 255)
alpha.paste(circle, (0, 0))
photo.putalpha(alpha)
photo = photo.resize((bw,bh),Image.ANTIALIAS)
base.paste(photo,box=(170,120),mask=photo)
# get a drawing context
draw = ImageDraw.Draw(base)
# draw text in the middle of the image, half opacity
draw.multiline_text((w/2-tw/2,480), title, font=title_fnt, fill=(0,0,0,255), align='center')
draw.multiline_text((w/2-cw/2,480+th+45), content, font=content_fnt, fill=(0,0,0,255), align='center', spacing=spacing)
draw.multiline_text((w/2-aw/2,480+th+45+ch+115), author, font=author_fnt, fill=(0,0,0,255), align='center')
draw.multiline_text((w-crw,480+th+45+ch+115+ah+50), copyright, font=copyright_fnt, fill=(189,189,189,255), align='center')
# save image data to output stream
base.save(msstream,"jpeg")
# release memory
base.close()
def template4(card,msstream):
w = 640
h = 1080
iw = 600
ih = 340
bw = 300
bh = 300
padding = 2
title = card.get('name')
content = card.get('content')
url = card.get('img_url')
spacing = 20
content = fill(content, 15)
author = '- 天天码图 -'
copyright = '微信小程序「天天码图」'
title_fnt = ImageFont.truetype('font/zh/YueSong.ttf', 35)
content_fnt = ImageFont.truetype('font/zh/YueSong.ttf', 30)
author_fnt = ImageFont.truetype('font/zh/YueSong.ttf', 25)
copyright_fnt = ImageFont.truetype('font/zh/YueSong.ttf', 25)
base = Image.new('RGBA',(w,h),(255,255,255,255))
draw = ImageDraw.Draw(base)
aw,ah = draw.multiline_textsize(author, font=author_fnt)
crw,crh = draw.multiline_textsize(copyright, font=copyright_fnt)
file = BytesIO(urllib.request.urlopen(url).read())
photo = Image.open(file).convert('RGBA')
pw, ph = photo.size
if pw > ph:
box = ((pw-ph*bw/bh)/2,0,(pw+ph*bw/bh)/2,ph)
else:
box = (0,(ph-pw*bh/bw)/2,pw,(ph+pw*bh/bw)/2)
photo = photo.crop(box)
photo = photo.resize((bw*4,bh*4))
circle = Image.new('L', (bw*4, bh*4), 0)
draw = ImageDraw.Draw(circle)
draw.ellipse((0, 0, bw*4, bh*4), fill=255)
alpha = Image.new('L', (bw*4, bh*4), 255)
alpha.paste(circle, (0, 0))
photo.putalpha(alpha)
photo = photo.resize((bw,bh),Image.ANTIALIAS)
base.paste(photo,box=(170,120),mask=photo)
# get a drawing context
draw = ImageDraw.Draw(base)
# split the title
tlines = wrap(title, 1)
# current title height
tnh = 480
# get width and height of single title word
stw,sth = title_fnt.getsize("已")
for tline in tlines:
draw.text((w-115-stw,tnh), tline, fill=(0,0,0,255), font=title_fnt)
tnh = tnh+sth
# get width and height of single content word
scw,sch = content_fnt.getsize("已")
clines = wrap(content, 14)
# current width of content
cnw = w-115-stw-115-scw
for cline in clines:
# current height of content
cnh = 480
cwords = wrap(cline, 1)
for cword in cwords:
pattern = re.compile("[,。、]+")
if pattern.search(cword):
draw.text((cnw,cnh), cword, fill=(0,0,0,255), font=content_fnt)
# draw.text((cnw+30-12,cnh-30+12), cword, fill=(0,0,0,255), font=content_fnt)
else:
draw.text((cnw,cnh), cword, fill=(0,0,0,255), font=content_fnt)
cnh = cnh+sch+padding
cnw = cnw-scw-spacing
# draw text in the middle of the image, half opacity
# draw.multiline_text((w/2-tw/2,420), title, font=title_fnt, fill=(0,0,0,255), align='center')
# draw.multiline_text((w/2-cw/2,420+th+45), content, font=content_fnt, fill=(0,0,0,255), align='center', spacing=spacing)
draw.multiline_text((w/2-aw/2,h-50-15-crh-ah), author, font=author_fnt, fill=(0,0,0,255), align='center')
draw.multiline_text((w-crw,h-15-crh), copyright, font=copyright_fnt, fill=(189,189,189,255), align='center')
# save image data to output stream
base.save(msstream,"jpeg")
# release memory
base.close()
print('getting total count:')
query = Query(Card)
query.does_not_exist('photo')
#query.exists('db_num')
query.limit(1000)
count = query.count()
progress = 0
print('Total:'+str(count))
cardlist = query.find()
for card in cardlist:
id = card.get('objectId')
print('generating:'+id)
result = generateCard(id)
print('result:'+result)
print('progess:('+str(progress)+'/'+str(count)+')')
progress = progress+1
else:
print('end')