byteimg.py 508 B

12345678910111213141516171819
  1. from io import BytesIO
  2. from PIL import Image, ImageDraw, ImageFont
  3. def byte2pil(byte_data):
  4. img = Image.open(BytesIO(byte_data))
  5. return img
  6. def pil2byte(img):
  7. img_bytes = BytesIO()
  8. img.save(img_bytes, format='JPEG')
  9. img_bytes = img_bytes.getvalue()
  10. return img_bytes
  11. def draw(img, text, position):
  12. font = ImageFont.truetype("font/SIMHEI.TTF", size=128)
  13. # print(font)
  14. draw = ImageDraw.Draw(img)
  15. draw.text(position, text, fill = (255, 0 ,0), font=font)
  16. return img