-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathimages.py
More file actions
1022 lines (886 loc) · 35.6 KB
/
images.py
File metadata and controls
1022 lines (886 loc) · 35.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
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# 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
from haishoku.haishoku import Haishoku
import re
import os
from weixin import weixin
app_id = os.environ["WXA_APP_ID"]
app_secret = os.environ["WXA_APP_SECRET"]
wx = weixin(app_id,app_secret)
# 模糊
def filter_blur(request):
image_data = Image.open("photo.jpg")
fliter_data = image_data.filter(ImageFilter.BLUR)
msstream=BytesIO()
fliter_data.save(msstream,"jpeg")
fliter_data.close()
return HttpResponse(msstream.getvalue(),content_type="image/jpeg")
# 轮廓
def filter_contour(request):
image_data = Image.open("photo.jpg")
fliter_data = image_data.filter(ImageFilter.CONTOUR)
msstream=BytesIO()
fliter_data.save(msstream,"jpeg")
fliter_data.close()
return HttpResponse(msstream.getvalue(),content_type="image/jpeg")
# 细节
def filter_detail(request):
image_data = Image.open("photo.jpg")
fliter_data = image_data.filter(ImageFilter.DETAIL)
msstream=BytesIO()
fliter_data.save(msstream,"jpeg")
fliter_data.close()
return HttpResponse(msstream.getvalue(),content_type="image/jpeg")
# 边缘增强
def filter_edge_enhance(request):
image_data = Image.open("photo.jpg")
fliter_data = image_data.filter(ImageFilter.EDGE_ENHANCE)
msstream=BytesIO()
fliter_data.save(msstream,"jpeg")
fliter_data.close()
return HttpResponse(msstream.getvalue(),content_type="image/jpeg")
# 边缘增强
def filter_edge_enhance_more(request):
image_data = Image.open("photo.jpg")
fliter_data = image_data.filter(ImageFilter.EDGE_ENHANCE_MORE)
msstream=BytesIO()
fliter_data.save(msstream,"jpeg")
fliter_data.close()
return HttpResponse(msstream.getvalue(),content_type="image/jpeg")
# 浮雕
def filter_emboss(request):
image_data = Image.open("photo.jpg")
fliter_data = image_data.filter(ImageFilter.EMBOSS)
msstream=BytesIO()
fliter_data.save(msstream,"jpeg")
fliter_data.close()
return HttpResponse(msstream.getvalue(),content_type="image/jpeg")
#寻找边缘
def filter_find_edges(request):
image_data = Image.open("photo.jpg")
fliter_data = image_data.filter(ImageFilter.FIND_EDGES)
msstream=BytesIO()
fliter_data.save(msstream,"jpeg")
fliter_data.close()
return HttpResponse(msstream.getvalue(),content_type="image/jpeg")
#柔化
def filter_smooth(request):
image_data = Image.open("photo.jpg")
fliter_data = image_data.filter(ImageFilter.SMOOTH)
msstream=BytesIO()
fliter_data.save(msstream,"jpeg")
fliter_data.close()
return HttpResponse(msstream.getvalue(),content_type="image/jpeg")
#柔化
def filter_smooth_more(request):
image_data = Image.open("photo.jpg")
fliter_data = image_data.filter(ImageFilter.SMOOTH_MORE)
msstream=BytesIO()
fliter_data.save(msstream,"jpeg")
fliter_data.close()
return HttpResponse(msstream.getvalue(),content_type="image/jpeg")
# 锐化
def filter_sharpen(request):
image_data = Image.open("photo.jpg")
fliter_data = image_data.filter(ImageFilter.SHARPEN)
msstream=BytesIO()
fliter_data.save(msstream,"jpeg")
fliter_data.close()
return HttpResponse(msstream.getvalue(),content_type="image/jpeg")
# 高斯模糊
def filter_gaussian_blur(request):
image_data = Image.open("photo.jpg")
fliter_data = image_data.filter(ImageFilter.GaussianBlur(4))
msstream=BytesIO()
fliter_data.save(msstream,"jpeg")
fliter_data.close()
return HttpResponse(msstream.getvalue(),content_type="image/jpeg")
# 反遮罩锐化
def filter_unsharp_mask(request):
image_data = Image.open("photo.jpg")
fliter_data = image_data.filter(ImageFilter.UnsharpMask())
msstream=BytesIO()
fliter_data.save(msstream,"jpeg")
fliter_data.close()
return HttpResponse(msstream.getvalue(),content_type="image/jpeg")
def wxacode(request):
msstream = BytesIO(wx.get_wxacode_unlimit('593351b361ff4b389e37087e'));
return HttpResponse(msstream.getvalue(),content_type="image/jpeg")
def template(request):
w = 640
h = 862
iw = 600
ih = 340
title = '每日一言'
content = '觉得最失落的,大概是你还在为你们的未来出谋划策,他却已慢慢后退不再与你并肩。'
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)
photo = Image.open("photo.jpg").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')
wxacodestream = wx.get_wxacode_unlimit('123456');
wxacode = Image.open(BytesIO(wxacodestream)).convert('RGBA')
wxacode = wxacode.resize((140,140),Image.ANTIALIAS)
base.paste(wxacode,box=(0,h-140))
# get BytesIO
msstream = BytesIO()
# save image data to output stream
base.save(msstream,"png")
# release memory
base.close()
return HttpResponse(msstream.getvalue(),content_type="image/png")
def template2(request):
w = 640
h = 1020
iw = 600
ih = 340
title = '每日一言'
content = '觉得最失落的,大概是你还在为你们的未来出谋划策,他却已慢慢后退不再与你并肩。'
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)
photo = Image.open("photo.jpg").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')
# get BytesIO
msstream = BytesIO()
# save image data to output stream
base.save(msstream,"png")
# release memory
base.close()
return HttpResponse(msstream.getvalue(),content_type="image/png")
def template3(request):
w = 640
h = 862
iw = 600
ih = 340
bw = 300
bh = 300
title = '每日一言'
content = '觉得最失落的,大概是你还在为你们的未来出谋划策,他却已慢慢后退不再与你并肩。'
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)
photo = Image.open("photo.jpg").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')
# get BytesIO
msstream = BytesIO()
# save image data to output stream
base.save(msstream,"png")
# release memory
base.close()
return HttpResponse(msstream.getvalue(),content_type="image/png")
def template4(request):
w = 640
h = 1080
iw = 600
ih = 340
bw = 300
bh = 300
padding = 2
title = '每日一言'
content = '觉得最失落的,大概是你还在为你们的未来出谋划策,他却已慢慢后退不再与你并肩。'
spacing = 20
content = fill(content, 15)
author = '- 天天码图 -'
copyright = '微信小程序「天天码图」'
title_fnt = ImageFont.truetype('font/zh/WangQingHua.ttf', 35)
content_fnt = ImageFont.truetype('font/zh/WangQingHua.ttf', 30)
author_fnt = ImageFont.truetype('font/zh/WangQingHua.ttf', 25)
copyright_fnt = ImageFont.truetype('font/zh/WangQingHua.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)
photo = Image.open("photo.jpg").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')
# get BytesIO
msstream = BytesIO()
# save image data to output stream
base.save(msstream,"png")
# release memory
base.close()
return HttpResponse(msstream.getvalue(),content_type="image/png")
def template6(request):
w = 640
h = 1080
padding = 2
spacing = 5
title = '你还要我怎样'
content = '我后来\n都会选择绕过那条街\n又多希望在另一条街能遇见'
singer = '薛之谦'
title_fnt = ImageFont.truetype('font/zh/JunYa.otf', 35)
singer_fnt = ImageFont.truetype('font/zh/JunYa.otf', 30)
content_fnt = ImageFont.truetype('font/zh/WangQingHua.ttf', 30)
clines = content.split('\n')
mcw = 0
for cline in clines:
clw,clh = content_fnt.getsize(cline)
if clw > mcw:
mcw = clw
ssw,ssh = singer_fnt.getsize("已")
stw,sth = title_fnt.getsize("已")
slines = wrap(singer, 1)
tw,wh = title_fnt.getsize(title)
base = Image.open("640.jpg").convert('RGBA')
base = base.filter(ImageFilter.GaussianBlur(4))
#base = base.filter(ImageFilter.SMOOTH)
w, h = base.size
draw = ImageDraw.Draw(base)
draw.multiline_text(((w-tw)/2,100), title, font=title_fnt, fill=(255,255,255,255), align='center')
line_height = 2
line_width = 50
line_padding = 10
left_line_x = (w-tw)/2 - line_width - line_padding
left_line_y = 100 + (sth-line_height)/2
left_line_x2 = (w-tw)/2 - line_padding
left_line_y2 = left_line_y
right_line_x = (w-tw)/2 + tw + line_padding
right_line_y = left_line_y
right_line_x2 = (w-tw)/2 + tw + line_padding + line_width
right_line_y2 = left_line_y
draw.line([(left_line_x,left_line_y),(left_line_x2,left_line_y2)],fill=(255,255,255,255),width=line_height)
draw.line([(right_line_x,right_line_y),(right_line_x2,right_line_y2)],fill=(255,255,255,255),width=line_height)
el = int((len(slines)+1)*ssh)
ew = el*4
eh = el*4
ellipse = Image.new('RGBA',(ew,eh),(255,255,255,0))
draw2 = ImageDraw.Draw(ellipse)
esize = 10
for i in range(0,esize):
draw2.ellipse([(0+i,0+i),(ew-i,eh-i)], outline=(255,255,255,255))
ellipse = ellipse.resize((el,el),Image.ANTIALIAS)
base.paste(ellipse,box=(int((w-el)/2),100+sth+50),mask=ellipse)
snh = 100+sth+50+ssh/2
for sline in slines:
draw.text(((w-ssw)/2,snh), sline, fill=(255,255,255,255), font=singer_fnt)
snh = snh+ssh
draw.multiline_text(((w-mcw)/2,320), content, font=content_fnt, fill=(255,255,255,255), align='center', spacing=spacing)
# get BytesIO
msstream = BytesIO()
# save image data to output stream
base.save(msstream,"png")
# release memory
base.close()
return HttpResponse(msstream.getvalue(),content_type="image/png")
def template5(request,font):
w = 640
h = 1080
iw = 600
ih = 340
bw = 300
bh = 300
padding = 2
title = '西江月·夜行黄沙道中'
author = '辛弃疾'
category = '#婉约#豪放#夏天#'
content = '''帘外雨潺潺,
春意阑珊。
罗衾不耐五更寒。
梦里不知身是客,
一晌贪欢。
独自莫凭栏,
无限江山,
别时容易见时难。
流水落花春去也,
天上人间。'''
spacing = 20
#content = content.replace(',','')
#content = content.replace('。','')
#content = content.replace('\r','。')
#content = fill(content, 14)
copyright = '微信小程序「天天码图」'
title_fnt = ImageFont.truetype('font/zh/'+font+'.ttf', 35)
author_fnt = ImageFont.truetype('font/zh/'+font+'.ttf', 25)
content_fnt = ImageFont.truetype('font/zh/'+font+'.ttf', 30)
copyright_fnt = ImageFont.truetype('font/zh/YueSong.ttf', 15)
clines = content.split('\n')
tlines = wrap(title, 1)
alines = wrap(author, 1)
# get width and height of single title word
stw,sth = title_fnt.getsize("已")
# get width and height of single content word
saw,sah = author_fnt.getsize("已")
scw,sch = content_fnt.getsize("已")
scrw,scrh = copyright_fnt.getsize("已")
wmh = len(tlines)*(sth+padding)
wmw = len(clines)*(scw+spacing)
for cline in clines:
clineh = len(cline)*sch
if clineh > wmh:
wmh = clineh
w = wmw+115+115+stw+115
h = wmh+80+80+scrh+15
base = Image.new('RGBA',(w,h),(255,255,255,255))
draw = ImageDraw.Draw(base)
# get a drawing context
draw = ImageDraw.Draw(base)
# split the title
# current title height
tnh = 80
for tline in tlines:
draw.text((w-115-stw,tnh), tline, fill=(0,0,0,255), font=title_fnt)
tnh = tnh+sth
anh = 80+sah
for aline in alines:
draw.text((w-115-stw-saw-10,anh), aline, fill=(0,0,0,255), font=author_fnt)
anh = anh+sah
#clines = wrap(content, 14)
# current width of content
cnw = w-115-stw-115-scw
lnh = 80
for cline in clines:
# current height of content
cnh = 80
cwords = wrap(cline, 1)
for cword in cwords:
if(cword != ',' and cword !='。'):
draw.text((cnw,cnh), cword, fill=(0,0,0,255), font=content_fnt)
cnh = cnh+sch+padding
else:
#draw.text((cnw,cnh), cword, fill=(0,0,0,255), font=content_fnt)
cnh = cnh+sch+padding
lnh = cnh
cnw = cnw-scw-spacing
copyrihtW,copyrightH = draw.multiline_textsize(copyright, font=copyright_fnt)
draw.multiline_text((w-copyrihtW,h-15-copyrightH), copyright, font=copyright_fnt, fill=(189,189,189,255), align='center')
stamp = Image.open("stamp.png").convert('RGBA')
stamp = stamp.resize((50,50))
base.paste(stamp,box=(cnw+scw+int((50-scw)/2),lnh-25),mask=stamp)
# get BytesIO
msstream = BytesIO()
# save image data to output stream
base.save(msstream,"png")
# release memory
base.close()
return HttpResponse(msstream.getvalue(),content_type="image/png")
def book(request):
w = 490*2
h = 740*2
banner_w = 490*2
banner_h = 265*2
cover_w = 135*2
cover_h = 200*2
cover_top = 120*2
cover_left = int((w-cover_w)/2)
block_w = 32*2
block_h = 12*2
block_left = 97*2
block_top = 160*2 + banner_h
max_content_w = 310*2
title_left = 97*2
title_top = block_top+block_h+20*2
title = '高窗'
title_font = ImageFont.truetype('font/zh/YueSong.ttf',28*2)
single_title_w,single_title_h= title_font.getsize("已")
titles = wrap(title, 1)
title_formated = ''
temp = ''
for word in titles:
temp += word
temp_w,temp_h = title_font.getsize(temp)
title_formated += word
if temp_w > max_content_w + single_title_w:
title_formated += '\n'
temp = ''
tlines = len(title_formated.split('\n'))
title_h = tlines * single_title_h + (tlines -1) * 28*2
division_left = 97*2
division_top = title_top+single_title_h+12*2
division = '╱'
division_font = ImageFont.truetype('font/zh/PingFang.ttf',20*2)
single_division_w,single_division_h = division_font.getsize("已")
author_left = 97*2
author_top = division_top + title_h
author = '作者:雷蒙德.钱德勒'
author_font = ImageFont.truetype('font/zh/YueSong.ttf',14*2)
single_author_w,single_author_h = author_font.getsize("已")
authors = wrap(author, 1)
author_formated = ''
temp = ''
for word in authors:
temp += word
temp_w,temp_h = author_font.getsize(temp)
author_formated += word
if temp_w > max_content_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 = 97*2
content_top = author_top + author_h + 12*2
content = '故事原型:加州石油大亨爱德华.多赫尼之子被杀案,及蒂波特山油田丑闻'
content_formated = ''
content_font = ImageFont.truetype('font/zh/YueSong.ttf',14*2)
single_content_w,single_content_h = content_font.getsize("已")
contents = wrap(content, 1)
temp = ''
for word in contents:
temp += word
temp_w,temp_h = content_font.getsize(temp)
content_formated += word
if temp_w > max_content_w + single_content_w:
content_formated += '\n'
temp = ''
print(content_formated)
clines = len(content_formated.split('\n'))
content_h = clines * single_author_h + (clines -1) * 14*2
h = content_top + content_h + 150*2
base = Image.new('RGBA',(w,h),(255,255,255,255))
draw = ImageDraw.Draw(base)
draw.rectangle([(0,0),(banner_w,banner_h)],(26, 26, 26, 255))
url = "https://img3.doubanio.com/lpic/s27028282.jpg"
file = BytesIO(requests.get(url).content)
photo = Image.open(file).convert('RGBA')
(pw, ph) = photo.size
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-cover_w*cover_h/cover_w)/2,pw,(ph+pw*cover_h/cover_w)/2)
photo = photo.crop(box)
photo = photo.resize((cover_w,cover_h),Image.ANTIALIAS)
base.paste(photo,box=(cover_left,cover_top))
dominant = Haishoku.getDominant(file)
draw.rectangle([(block_left,block_top),(block_left+block_w,block_top+block_h)],dominant)
draw.multiline_text((title_left,title_top), title_formated, font=title_font, fill=(70,70,70), align='left',spacing=15*2)
draw.multiline_text((division_left,division_top), division, font=division_font, fill=(70,70,70), align='left',spacing=0)
draw.multiline_text((author_left,author_top), author_formated, font=author_font, fill=(90,90,90), align='left',spacing=15*2)
draw.multiline_text((content_left,content_top), content_formated, font=content_font, fill=(90,90,90), align='left',spacing=12*2)
print(dominant)
#base.show()
# get BytesIO
msstream = BytesIO()
# save image data to output stream
base.save(msstream,"jpeg")
# release memory
base.close()
return HttpResponse(msstream.getvalue(),content_type="image/jpeg")
def music():
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 = '成都'
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 = '赵雷'
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 = 'Just let time go on\n让我 掉下眼泪的\n不止昨夜的酒\n让我依依不舍的\n不止你的温柔\n余路还要走多久\n你攥着我的手\n让我感到为难的\n是挣扎的自由'
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/T002R300x300M000001qHmKU29WX7K.jpg"
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))
base.show()
def image_text(request):
fontSize = 40
w = 640
h = 640
text = '当一艘船沉入海底\n当一个人成了谜\n你不知道\n他们为何离去\n那声再见竟是他最后一句'
meta = '后会无期·G.E.M.邓紫棋'
copyright = '— 微信小程序 : 天天码图 —'
# 按长度(字数)换行
# text = fill(text,11)
# make a blank image as the background
base = Image.new('RGBA',(w,h),(255,255,255,255))
txt = Image.new('RGBA',(w,h),(255,255,255,0))
# get an image
photo = Image.open("photo.jpg").convert('RGBA')
(pw, ph) = photo.size
if pw/ph>w/h:
box = ((pw-ph)/2,0,(pw+ph)/2,ph)
else:
box = (0,(ph-pw)/2,pw,(pw+ph)/2)
photo = photo.crop(box)
photo = photo.resize((w,h))
# blur filter
photo = photo.filter(ImageFilter.GaussianBlur())
base.paste(photo)
# make a blank image for text, initailized to half-transparent text color
txt = Image.new('RGBA', (w, h), (0,0,0,100))
# get a font
fnt = ImageFont.truetype('font/zh/LiJin.ttf',fontSize)
meta_fnt = ImageFont.truetype('font/zh/PingFang.ttf',20)
copyright_fnt = ImageFont.truetype('font/zh/TongXin.ttf',14)
# get size of the text
# (tw, th) = fnt.getsize(text)
# get a drawing context
draw = ImageDraw.Draw(txt)
tw,th = draw.multiline_textsize(text, fnt)
mw,mh = draw.multiline_textsize(meta, meta_fnt)
cpw,cph =draw.multiline_textsize(copyright, copyright_fnt)
# draw text in the middle of the image, half opacity
draw.multiline_text(((w-tw)/2,(h-th)/2), text, font=fnt, fill=(255,255,255,255), align='center',spacing=15)
draw.multiline_text(((w-mw)/2,h-mh-30), meta, font=meta_fnt, fill=(255,255,255,150), align='center')
draw.multiline_text(((w-cpw)/2,h-cph-10), copyright, font=copyright_fnt, fill=(255,255,255,150), align='center')
# composite base image and text image
out = Image.alpha_composite(base, txt)
# get BytesIO
msstream = BytesIO()
# save image data to output stream
out.save(msstream,"png")
# release memory
out.close()
return HttpResponse(msstream.getvalue(),content_type="image/png")
def test():
w = 640
h = 862
iw = 600
ih = 340
bw = 300
bh = 300
title = '每日一言'
content = '觉得最失落的,大概是你还在为你们的未来出谋划策,他却已慢慢后退不再与你并肩。'
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)
photo = Image.open("640.jpg").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)
title = '成都.薛之谦'
title_font = ImageFont.truetype('font/zh/YueSong.ttf',28*8)
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.multiline_text((100,100), title, font=title_font, fill=255, align='left',spacing=15*2)
#draw.ellipse((0, 0, bw*4, bh*4), fill=255)
alpha = Image.new('L', (bw*4, bh*4), 255)
#alpha.show()
alpha.paste(circle, (0, 0))
photo.putalpha(alpha)
#photo.show()
photo = photo.resize((bw,bh),Image.ANTIALIAS)
base.paste(photo,box=(170,120),mask=photo)
base.convert('L')
base.putalpha(255)
bg = Image.open("640.jpg").convert('RGBA')
bg.paste(base,box=(170,120),mask=base)
bg.show()
def blend():
img1 = Image.open('640.jpg')
img2 = Image.open('photo.jpg')
out = Image.blend(img1,img2,0.5)
out.show()
def alphacomposite():
photo = Image.open("640.jpg").convert('RGBA')
bw = 300
bh = 300
box = (0,0,bw,bh)
photo = photo.crop(box)
circle = Image.new('L', (bw, bh), 0)
draw = ImageDraw.Draw(circle)
draw.ellipse((0, 0, bw, bh), fill=255)
circle.show()
alpha = Image.new('L', (bw, bh), 255)
alpha.paste(circle, (0, 0))
alpha.show()
photo.putalpha(alpha)
photo.show()
base = Image.new('RGBA',(bw,bh),(255,255,255,255))
base.paste(photo,box=(0,0),mask=photo)
out = Image.alpha_composite(base,photo)
def circle_new():
ima = Image.open("photo.jpg").convert("RGBA")