我最近没有摸鱼,一直都在工做。只不过目前须要爬一点数据 python
作的,以后看机会分享一下。html
忙着忙着老大说要生成海报,有个活动要给每一个用户来个分享图。前端
这个方案,通过我调研行不通。(也有多是我菜)
对于批量打码,统一操做这类的还能够。
对于须要对应名字之类的比较无力python
老大说 python。py
使用 PIL
库 写起来还蛮简单的。py
使用这段时间以来就是感受这个编码格式极其难受。有没有大佬解救我一下。canvas
mask=img
要注意针对 png
来使用经过上面的类型,来增长咱们代码的扩展性。segmentfault
# -*- coding: utf-8 -*- from PIL import Image, ImageDraw, ImageFont import qrcode import time; import re def AddMoney(data): bg = Image.new('RGBA', (data['width'], data['height'])) for item in data['list']: print(item) if item['type'] == 'qrcode': qr_pil_obj = qrcode.make(item['url'], border=0).resize((item['width'], item['height']), Image.ANTIALIAS) bg.paste(qr_pil_obj,(item['x'],item['y'])) elif item['type'] == 'image': extname = re.search( r'(jpg|png|jpeg)$', item['url']).group() # 加载图片 img = Image.open(item['url']) # 缩放 if (item['width']!= '' and item['height'] != ''): img.resize((item['width'], item['height']), Image.ANTIALIAS) if extname == 'png': bg.paste(img,(item['x'],item['y']), mask=img) else: bg.paste(img,(item['x'],item['y'])) elif item['type'] == 'text': draw = ImageDraw.Draw(bg) ttfront = ImageFont.truetype(item['ttfrontUrl'],item['ttfrontSize']) draw.text((item['x'],item['y']),unicode(item['txt'],"UTF-8"), fill=(0,0,0), font=ttfront) else: print('------------------------------') bg.save(data['outputName']) data = { 'width': 750, 'height': 1334, 'outputName': './out-%s.png' % (str(time.time())), 'list':[{"type":"image","url":"./avatar.jpg","x":10,"y":360,"width":750,"height":1334},{"type":"image","url":"./bg.png","x":0,"y":0,"width":750,"height":750},{"type":"qrcode","url":"https://www.lilnong.top","x":0,"y":0,"width":120,"height":120},{"type":"text","txt":"你这个死宅说话","x":140,"y":560,"ttfrontUrl":"./abc.ttf","ttfrontSize":55},{"type":"text","txt":"还挺搞笑的","x":160,"y":630,"ttfrontUrl":"./abc.ttf","ttfrontSize":55}] } AddMoney(data)
这个也是极好,是以前一个 Node
的服务。省时省力。网上查也很多跨域
我堂堂前端,怎么也得露一手吧。
基于上面的页面已经成功。咱们想一下咱们须要干什么?把 html
转成 canvas
,而后 canvas.toBlob
,而后 download
美滋滋。微信
html2canvas
这是一个库,既然已经有写好的了,咱们就不费神了。(最难的一步。。。)cavnas
调用 API
, canvas.toBlob
。各类忘记发。补上补上工具
py
写代码是真的短。
前端处理须要注意跨域、URL空格等问题。字体