async def generate_image_online(latex, colour_back = None, colour_text = '000000'): payload = { 'format': 'png', 'code': latex.strip(), } async with aiohttp.ClientSession() as session: try: async with session.post(LATEX_SERVER_URL, json = payload, timeout = 8) as loc_req: loc_req.raise_for_status() jdata = await loc_req.json() # print('LOG:\n', jdata.get('log')) # print(jdata.get('status')) # print(jdata.get('description')) if jdata['status'] == 'error': # print(json.dumps(jdata)) raise RenderingError filename = jdata['filename'] # Now actually get the image async with session.get(LATEX_SERVER_URL + '/' + filename, timeout = 3) as img_req: img_req.raise_for_status() fo = io.BytesIO(await img_req.read()) image = PIL.Image.open(fo).convert('RGBA') except aiohttp.client_exceptions.ClientResponseError: raise RenderingError if colour_back is not None: colour_back = imageutil.hex_to_tuple(colour_back) back = imageutil.new_monocolour(image.size, colour_back) back.paste(image, (0, 0), image) image = imageutil.add_border(back, 4, colour_back) return image
async def generate_image_online(latex, colour_back): payload = { 'format': 'png', 'code': latex.strip(), } async with aiohttp.ClientSession() as session: try: async with session.post(LATEX_SERVER_URL, json=payload, timeout=8) as loc_req: loc_req.raise_for_status() jdata = await loc_req.json() if jdata['status'] == 'error': raise RenderingError(jdata.get('log')) filename = jdata['filename'] # Now actually get the image async with session.get(LATEX_SERVER_URL + '/' + filename, timeout=3) as img_req: img_req.raise_for_status() fo = io.BytesIO(await img_req.read()) image = PIL.Image.open(fo).convert('RGBA') except aiohttp.client_exceptions.ClientResponseError: raise RenderingError(None) if image.width <= 2 or image.height <= 2: raise RenderingError(None) border_size = 4 colour_back = imageutil.hex_to_tuple(colour_back) width, height = image.size backing = imageutil.new_monocolour( (width + border_size * 2, height + border_size * 2), colour_back) backing.paste(image, (border_size, border_size), image) fobj = io.BytesIO() backing.save(fobj, format='PNG') fobj = io.BytesIO(fobj.getvalue()) return fobj
async def generate_image_online(latex, colour_back=None, colour_text='000000'): latex = latex.strip() payload = TEX_PAYLOAD.format(latex=semiquote(latex), preamble=semiquote(PREAMBLE), colour=colour_text) # Query the server, it will return the url to get the actual image from. # url = 'http://quicklatex.com/latex3.f' + payload async with aiohttp.ClientSession() as session: async with session.post('http://quicklatex.com/latex3.f', data=payload, timeout=8) as loc_req: loc_req.raise_for_status() text = await loc_req.text() # print(text) # img_url = text.split('\n')[1].split(' ')[0] # for i in blobs.groups(): # print('GROUP:', i) blobs = re.match(RESPONSE_PARSING_REGEX, text) status, img_url, valign, imgw, imgh, errmsg = blobs.groups() if status != '0': raise RenderingError(errmsg) if int(imgw) < 4 or int(imgh) < 4: raise EmptyImageError # var status = regs[1]; # var imgurl = regs[2]; # var valign = regs[3]; # var imgw = regs[4]; # var imgh = regs[5]; # var errmsg = regs[6]; # Now actually get the image async with session.get(img_url, timeout=3) as img_req: img_req.raise_for_status() fo = io.BytesIO(await img_req.read()) image = PIL.Image.open(fo).convert('RGBA') if colour_back is not None: colour_back = imageutil.hex_to_tuple(colour_back) back = imageutil.new_monocolour(image.size, colour_back) back.paste(image, (0, 0), image) image = imageutil.add_border(back, 4, colour_back) return image