Skip to content

Commit 7f16694

Browse files
txrp0x9ayushnagar123
authored andcommitted
Added image conversion to transparent (#47)
1 parent 969109d commit 7f16694

File tree

1 file changed

+24
-7
lines changed

1 file changed

+24
-7
lines changed

backend/run.py

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import requests
55
import numpy as np
66
from cv2 import cv2
7+
from PIL import Image
78
from werkzeug.utils import secure_filename
89
import os
910
app = Flask(__name__,
@@ -30,7 +31,6 @@ def upload():
3031

3132
inputImage = cv2.imread(name)
3233
inputImageGray = cv2.cvtColor(inputImage, cv2.COLOR_BGR2GRAY)
33-
3434
edges = cv2.Canny(inputImageGray,150,200,apertureSize = 3)
3535

3636
print(edges)
@@ -46,11 +46,28 @@ def upload():
4646

4747
font = cv2.FONT_HERSHEY_SIMPLEX
4848
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+
5350
os.remove(name)
5451

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

Comments
 (0)