简体   繁体   English

如何将在我的html文件上单击的图像的URL传递给views.py中的python函数

[英]how to pass the url of image clicked on my html file to my python function in views.py Django

i want to pass the url of the image that was selected by the user in my python function views.py because my python function will process the image that was selected by the user. 我想传递用户在我的python函数views.py中选择的图像的url,因为我的python函数将处理用户选择的图像。 this is my html command 这是我的html命令

<!DOCTYPE html>
<html>
<head>
<link class="jsbin" href="http://ajax.googleapis.com/ajax/libs/jqueryui/1/themes/base/jquery-ui.css" rel="stylesheet" type="text/css" />
<script class="jsbin" src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<script class="jsbin" src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.0/jquery-ui.min.js"></script>
<meta charset=utf-8 />
<title>JS Bin</title>
<!--[if IE]>
  <script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
<style>
  article, aside, figure, footer, header, hgroup, 
  menu, nav, section { display: block; }
</style>
</head>
<body>
  <input type='file' onchange="readURL(this);" />
    <img id="blah" src="#" alt="your image" />
<script>
    function readURL(input) {
        if (input.files && input.files[0]) {
            var reader = new FileReader();

            reader.onload = function (e) {
                $('#blah')
                    .attr('src', e.target.result)
                    .width(150)
                    .height(200);
            };

            reader.readAsDataURL(input.files[0]);
        }
    }
</script>
</body>
</html>

and here is the function on my python 这是我的python上的功能

def predict():
    img = cv2.imread('C:/Users/HABITUS/Desktop/arvin files/download.jpg') #the url of the image selected must be here!!
    img = cv2.resize(img, (600, 600))
    enhancer = Image.fromarray(img)
    enhancer = ImageEnhance.Contrast(enhancer)
    enhanced = enhancer.enhance(1.5)
    enhancer1 = ImageEnhance.Brightness(enhanced).enhance(1.3)
    convert = scipy.misc.fromimage(enhancer1)
    imgM = cv2.medianBlur(convert, 5)
    # blurring and smoothening
    kernel = np.ones((5, 5), np.uint8)
    erosion = cv2.erode(imgM, kernel, iterations=1)
    dilation = cv2.dilate(erosion, kernel, iterations=1)
    blur = cv2.GaussianBlur(convert, (15, 15), 10)
    grayscaled = cv2.cvtColor(imgM, cv2.COLOR_BGR2GRAY)

    retval2, threshold2 = cv2.threshold(grayscaled, 200, 1, cv2.THRESH_BINARY_INV)

    gaus = cv2.adaptiveThreshold(grayscaled, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY, 115, 1)
    retval2, otsu = cv2.threshold(grayscaled, 140, 250, cv2.THRESH_BINARY + cv2.THRESH_OTSU)
    backtorgb = cv2.cvtColor(threshold2, cv2.COLOR_GRAY2RGB)
    mult = cv2.multiply(img, backtorgb)
    edges = cv2.Canny(threshold2, 120, 50)

the image that was selected by the user must be in cv2.imread() how can i do that? 用户选择的图像必须位于cv2.imread()中,我该怎么做? any advice or help will be appreciated thanks 任何建议或帮助将不胜感激谢谢

this is somewhat confusing. 这有点令人困惑。

you might want to start with reading https://docs.djangoproject.com/en/2.0/topics/forms/ 您可能要先阅读https://docs.djangoproject.com/en/2.0/topics/forms/

And here are two existing questions that should help you: 以下是两个可以帮助您的问题:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM