2017-12-27 300 views
0

我想傳遞用戶在我的python函數views.py中選擇的圖像的url,因爲我的python函數將處理用戶選擇的圖像。這是我的HTML命令如何將點擊我的html文件的圖像url傳遞給view.py中的python函數Django

<!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> 

這裏是一個由用戶選擇必須在cv2.imread()我怎樣才能做到這一點對我的蟒蛇

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) 

圖像的功能?任何意見或幫助將不勝感激

+0

我不閱讀了Django 2.0。但這在django中是一個非常好的問題。我的想法是,你所描述的用例分爲前端和後端。我確信這會產生一些分歧,但是Django並沒有明確說明Django與JS交談的框架。所以如果快速和骯髒的只是使用Django的形式。但是,如果對於更多的生產用例,我會使用js框架來監聽哪個img是onClicked,然後將該帖子發送到django rest框架端點。 – sahutchi

回答

相關問題