4
4
import requests
5
5
import numpy as np
6
6
from cv2 import cv2
7
+ from PIL import Image
7
8
from werkzeug .utils import secure_filename
8
9
import os
9
10
app = Flask (__name__ ,
@@ -30,7 +31,6 @@ def upload():
30
31
31
32
inputImage = cv2 .imread (name )
32
33
inputImageGray = cv2 .cvtColor (inputImage , cv2 .COLOR_BGR2GRAY )
33
-
34
34
edges = cv2 .Canny (inputImageGray ,150 ,200 ,apertureSize = 3 )
35
35
36
36
print (edges )
@@ -46,11 +46,28 @@ def upload():
46
46
47
47
font = cv2 .FONT_HERSHEY_SIMPLEX
48
48
cv2 .putText (inputImage ,"Tracks Detected" , (500 , 250 ), font , 0.5 , 255 )
49
-
50
- cv2 .imwrite (temp_path + tempname + '.jpeg' ,edges )
51
- cv2 .waitKey (0 )
52
-
49
+
53
50
os .remove (name )
54
51
55
- filename = tempname + '.jpeg'
56
- return send_file (temp_path + filename ,mimetype = 'image/jpeg' )
52
+ filename = tempname + '.png'
53
+
54
+ #Following converts white pixels to transparent
55
+ imagePIL = Image .fromarray (edges )
56
+ imagePIL = imagePIL .convert ("RGBA" )
57
+ datas = imagePIL .getdata ()
58
+
59
+ newData = []
60
+ for item in datas :
61
+ if item [0 ] == 255 and item [1 ] == 255 and item [2 ] == 255 :
62
+ newData .append ((255 , 255 , 255 , 0 ))
63
+ else :
64
+ if item [0 ] > 150 :
65
+ newData .append ((0 , 0 , 0 , 255 ))
66
+ else :
67
+ newData .append (item )
68
+
69
+
70
+ imagePIL .putdata (newData )
71
+ imagePIL .save (temp_path + filename , "PNG" )
72
+
73
+ return send_file (temp_path + filename ,mimetype = 'image/png' )
0 commit comments