url.py
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from Demo.view import * # 项目叫Demo
from django.urls import path
from django.urls.conf import re_path
urlpatterns = [
path('', index),
path('text', text), # 接口路径
]
re_path('', index)
view.py
import json
import base64
import PIL.Image as Image
import matplotlib.pyplot as plt
from io import BytesIO
from django.http import *
from django.shortcuts import *
from django.views.decorators.http import require_http_methods
from Demo.util.common import *
@require_http_methods(["POST"])
def test(request):
body = json.loads(request.body)
img = body['img']
img = base64.b64decode(img)
img = Image.open(BytesIO(img))
plt.imshow(img)
plt.show() # 图片将完好展示出来
return JsonResponse({}, status=200)
index.html
document