text: import asyncio
  import os
  import shlex
  from os import getcwd
  from os.path import basename, join
  from textwrap import wrap
  from typing import Optional, Tuple
  
  import numpy as np
  
  try:
      from colour import Color as asciiColor
  except:
      os.system("pip install colour")
  from PIL import Image, ImageDraw, ImageFont
  from telethon.errors.rpcerrorlist import YouBlockedUserError
  from wand.color import Color
  from wand.drawing import Drawing
  from wand.image import Image as catimage
  
  from . import unzip
  
  MARGINS = [50, 150, 250, 350, 450]
  
  
  def asciiart(in_f, SC, GCF, out_f, color1, color2, bgcolor="black"):
      chars = np.asarray(list(" .,:irs?@9B&#"))
      font = ImageFont.load_default()
      letter_width = font.getsize("x")[0]
      letter_height = font.getsize("x")[1]
      WCF = letter_height / letter_width
      img = Image.open(in_f)
      widthByLetter = round(img.size[0] * SC * WCF)
      heightByLetter = round(img.size[1] * SC)
      S = (widthByLetter, heightByLetter)
      img = img.resize(S)
      img = np.sum(np.asarray(img), axis=2)
      img -= img.min()
      img = (1.0 - img / img.max()) ** GCF * (chars.size - 1)
      lines = ("\n".join(("".join(r) for r in chars[img.astype(int)]))).split("\n")
      nbins = len(lines)
      colorRange = list(asciiColor(color1).range_to(asciiColor(color2), nbins))
      newImg_width = letter_width * widthByLetter
      newImg_height = letter_height * heightByLetter
      newImg = Image.new("RGBA", (newImg_width, newImg_height), bgcolor)
      draw = ImageDraw.Draw(newImg)
      leftpadding = 0
      y = 0
      for lineIdx, line in enumerate(lines):
          color = colorRange[lineIdx]
          draw.text((leftpadding, y), line, color.hex, font=font)
          y += letter_height
      if newImg.mode != "RGB":
          newImg = newImg.convert("RGB")
      newImg.save(out_f)
  
  
  def get_warp_length(width):
      return int((20.0 / 1024.0) * (width + 0.0))
  
  
  async def cat_meme(topString, bottomString, filename, endname):
      img = Image.open(filename)
      imageSize = img.size
      # find biggest font size that works
      fontSize = int(imageSize[1] / 5)
      font = ImageFont.truetype("ub/helpers/styles/impact.ttf", fontSize)
      topTextSize = font.getsize(topString)
      bottomTextSize = font.getsize(bottomString)
      while topTextSize[0] > imageSize[0] - 20 or bottomTextSize[0] > imageSize[0] - 20:
          fontSize -= 1
          font = ImageFont.truetype("ub/helpers/styles/impact.ttf", fontSize)
          topTextSize = font.getsize(topString)
          bottomTextSize = font.getsize(bottomString)
  
      # find top centered position for top text
      topTextPositionX = (imageSize[0] / 2) - (topTextSize[0] / 2)
      topTextPositionY = 0
      topTextPosition = (topTextPositionX, topTextPositionY)
  
      # find bottom centered position for bottom text
      bottomTextPositionX = (imageSize[0] / 2) - (bottomTextSize[0] / 2)
      bottomTextPositionY = imageSize[1] - bottomTextSize[1]
      bottomTextPosition = (bottomTextPositionX, bottomTextPositionY)
      draw = ImageDraw.Draw(img)
      # draw outlines
      # there may be a better way
      outlineRange = int(fontSize / 15)
      for x in range(-outlineRange, outlineRange + 1):
          for y in range(-outlineRange, outlineRange + 1):
              draw.text(
                  (topTextPosition[0] + x, topTextPosition[1] + y),
                  topString,
                  (0, 0, 0),
                  font=font,
              )
              draw.text(
                  (bottomTextPosition[0] + x, bottomTextPosition[1] + y),
                  bottomString,
                  (0, 0, 0),
                  font=font,
              )
      draw.text(topTextPosition, topString, (255, 255, 255), font=font)
      draw.text(bottomTextPosition, bottomString, (255, 255, 255), font=font)
      img.save(endname)
  
  
  async def cat_meeme(upper_text, lower_text, picture_name, endname):
      main_image = catimage(filename=picture_name)
      main_image.resize(
          1024, int(((main_image.height * 1.0) / (main_image.width * 1.0)) * 1024.0)
      )
      upper_text = "\n".join(wrap(upper_text, get_warp_length(main_image.width))).upper()
      lower_text = "\n".join(wrap(lower_text, get_warp_length(main_image.width))).upper()
      lower_margin = MARGINS[lower_text.count("\n")]
      text_draw = Drawing()
      text_draw.font = join(getcwd(), "ub/helpers/styles/impact.ttf")
      text_draw.font_size = 100
      text_draw.text_alignment = "center"
      text_draw.stroke_color = Color("black")
      text_draw.stroke_width = 3
      text_draw.fill_color = Color("white")
      if upper_text:
          text_draw.text((main_image.width) // 2, 80, upper_text)
      if lower_text:
          text_draw.text(
              (main_image.width) // 2, main_image.height - lower_margin, lower_text
          )
      text_draw(main_image)
      main_image.save(filename=endname)
  
  
  # executing of terminal commands
  
  
  async def runcmd(cmd: str) -> Tuple[str, str, int, int]:
      args = shlex.split(cmd)
      process = await asyncio.create_subprocess_exec(
          *args, stdout=asyncio.subprocess.PIPE, stderr=asyncio.subprocess.PIPE
      )
      stdout, stderr = await process.communicate()
      return (
          stdout.decode("utf-8", "replace").strip(),
          stderr.decode("utf-8", "replace").strip(),
          process.returncode,
          process.pid,
      )
  
  
  # For using gif , animated stickers and videos in some parts , this
  # function takes  take a screenshot and stores ported from userge
  
  
  async def take_screen_shot(
      video_file: str, duration: int, path: str = ""
  ) -> Optional[str]:
      print(
          "[[[Extracting a frame from %s ||| Video duration => %s]]]",
          video_file,
          duration,
      )
      ttl = duration // 2
      thumb_image_path = path or os.path.join("./temp/", f"{basename(video_file)}.jpg")
      command = f"ffmpeg -ss {ttl} -i '{video_file}' -vframes 1 '{thumb_image_path}'"
      err = (await runcmd(command))[1]
      if err:
          print(err)
      return thumb_image_path if os.path.exists(thumb_image_path) else None
  
  
  async def make_gif(event, file):
      chat = "@tgstogifbot"
      async with event.client.conversation(chat) as conv:
          try:
              await silently_send_message(conv, "/start")
              await event.client.send_file(chat, file)
              response = await conv.get_response()
              await event.client.send_read_acknowledge(conv.chat_id)
              if response.text.startswith("Send me an animated sticker!"):
                  return "`This file is not supported`"
              response = response if response.media else await conv.get_response()
              catresponse = response if response.media else await conv.get_response()
              await event.client.send_read_acknowledge(conv.chat_id)
              catfile = await event.client.download_media(catresponse, "./temp")
              return await unzip(catfile)
          except YouBlockedUserError:
              return "Unblock @tgstogifbot"
  
  
  async def silently_send_message(conv, text):
      await conv.send_message(text)
      response = await conv.get_response()
      await conv.mark_read(message=response)
      return response
  
  
  async def thumb_from_audio(audio_path, output):
      await runcmd(f"ffmpeg -i {audio_path} -filter:v scale=500:500 -an {output}")
  