HTML通过Ajax上传图像至Django后端并转换为Mat图像

1、index.html


    
......


2、views.py

class Index(View):
    def get(self, request):
        return render(request, 'index.html')


class OCRView(View):
    def post(self, request):
        img = request.FILES.get('img')
        parser = ImageFile.Parser()
        for chunk in img.chunks():
            parser.feed(chunk)
        img = parser.close()
        img = cv2.cvtColor(np.asarray(img), cv2.COLOR_RGB2BGR)
        result = "图像的尺寸:" + img.shape[1] + "_" + img.shape[0]
        return HttpResponse(result)

3、 urls.py

urlpatterns = [
    path('', Index.as_view(), name='index'),
    path('test/', OCRView.as_view(), name='test'),
]

 

你可能感兴趣的:(Django,前端,ajax,html,django,python)