Skip to content

Commit 78eba22

Browse files
equalisation fonctionnelle
1 parent a69826a commit 78eba22

6 files changed

Lines changed: 129 additions & 10 deletions

File tree

imgprocess/img_api.py

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,12 @@
88
from .utils.utility import *
99
from PIL import Image
1010
import numpy as np
11+
from matplotlib import pyplot as plt
12+
13+
img_path="imgprocess/static/imageprocess/images/"
14+
hist_path=img_path+"hist/"
15+
hist_path_result=img_path+"histresult/"
16+
result_path=img_path+"result/"
1117

1218
def _constrate(request):
1319
image_file=request.FILES["imguploaded"]
@@ -35,4 +41,38 @@ def _constrate(request):
3541
return JsonResponse({"original_name":str(image_file),"extention":file_extension,"saved_name":saved_name})
3642

3743
def _egalisation_histogramme(request):
38-
return JsonResponse({"status":True})
44+
"""At the end we need to send the preview histogramme, the new histgramme
45+
and the final image"""
46+
image_info=save_image(request.FILES["imguploaded"],None)
47+
48+
#we get the name of the preview_histogramme
49+
preview_his_name=get_random_string(20)+image_info["extension"];
50+
plt.hist(image_info["list"]);
51+
52+
plt.savefig(hist_path+preview_his_name)
53+
plt.close()
54+
55+
56+
image_info["preview_hist"]=preview_his_name
57+
58+
#we call function of equalisation on the matrix of image
59+
egalisation_his_matrix=ImgLib().histogram_normalisation(image_info["matrix"])
60+
#we get now the new image
61+
62+
print(egalisation_his_matrix)
63+
64+
new_image=Image.fromarray(egalisation_his_matrix.astype(np.uint8))
65+
new_image_name=get_random_string(18)+image_info["extension"]
66+
new_image.save(result_path+new_image_name)
67+
68+
#now we reshape our egalisation_his_matirx at the list and we create the plot
69+
new_image_as_list=egalisation_his_matrix.ravel()
70+
new_his_name=get_random_string(24)+image_info["extension"]
71+
plt.plot(new_image_as_list)
72+
plt.savefig(hist_path_result+new_his_name);
73+
plt.close()
74+
image_info["new_hist"]=new_his_name
75+
image_info["saved_name"]=new_image_name
76+
image_info["matrix"]=None
77+
78+
return JsonResponse(image_info)

imgprocess/static/shared/js/img_process.js

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,38 @@ $(function(e){
2121
dataType: "json",
2222
method:"POST",
2323
success:function(result){
24-
console.log("bonjour")
24+
console.log(result);
2525
$("#result-box").html("<img src='/static/imageprocess/images/result/"+result["saved_name"]+"' width='100%' height='100%' />");
2626
},
2727
error:function(result){
28-
console.log("error")
28+
console.log("error");
2929
}
3030
});
3131
return false;
3232
});
33+
34+
35+
$("#form_equalize").submit(function(){
36+
37+
form_data=new FormData(this)
38+
$.ajax({
39+
url:$(this).attr("action"),
40+
data:form_data,
41+
contentType: false,
42+
cache: false,
43+
processData: false,
44+
dataType: "json",
45+
method:"POST",
46+
success:function(result){
47+
console.log(result);
48+
$("#result-box").html("<img src='/static/imageprocess/images/result/"+result["saved_name"]+"' width='100%' height='100%' />");
49+
$("#hist-preview").html("<img src='/static/imageprocess/images/hist/"+result["preview_hist"]+"' width='100%' height='100%' />");
50+
$("#hist-result-box").html("<img src='/static/imageprocess/images/histresult/"+result["new_hist"]+"' width='100%' height='100%' />");
51+
},
52+
error:function(result){
53+
console.log("error");
54+
}
55+
});
56+
return false;
57+
})
3358
});

imgprocess/templates/imgprocess/egalisation_histogramme.html

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
<h1>Welcome to histogramme egalisation</h1>
1010
<hr />
1111
<br />
12-
<form class="" id="form_upload_image" enctype="multipart/form-data" ajax method="post" action="{% url 'imageprocess:pluminecance' %}">
12+
<form class="" id="form_equalize" enctype="multipart/form-data" ajax method="post" action="{% url 'imageprocess:pegalisation' %}">
1313
{% csrf_token %}
1414
<div class="row">
1515
</div>
@@ -24,15 +24,33 @@ <h4>Preview</h4>
2424
</div>
2525
<div class="col-6">
2626
<h4>histogramme Preview</h4>
27+
<div class="card border-0 shadow-lg" id="hist-preview">
28+
29+
</div>
30+
<br />
31+
<button type="submit" class="btn btn-info rounded btn-lg second-color shadow-lg text-white">EQUALIZE</button>
32+
33+
</div>
34+
<!--result with histogramme -->
35+
<div class="col-6">
36+
<br />
37+
<h4>Obtained Image</h4>
2738
<div class="card border-0 shadow-lg" id="result-box">
2839

40+
</div>
41+
</div>
42+
<div class="col-6">
43+
<br />
44+
<h4>Obtained Histogramm</h4>
45+
<div class="card border-0 shadow-lg" id="hist-result-box">
46+
2947
</div>
3048
</div>
3149
</div>
3250
<br />
3351
<br />
3452
<label>After click look result down</label><br />
35-
<button type="submit" class="btn btn-info rounded btn-lg second-color shadow-lg text-white">EQUALIZE</button>
53+
3654
</form>
3755
</div>
3856
{% endblock %}

imgprocess/utils/__init__.py

Whitespace-only changes.

imgprocess/utils/img_lib.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -81,10 +81,15 @@ def histogram_normalisation(self,img_matrix):
8181
#normalisation of probality density
8282
c_normalize=[[],[]]
8383
new_image=np.zeros(img_matrix.shape,dtype=int)
84-
for i in list(range(1,256)):
85-
c_normalize[i-1]=0
86-
for j in list(range(i)):
87-
pass
84+
#Calcul des densite de probabablite
85+
c_normalize=np.cumsum(h_normalize)
86+
87+
for i in range(img_matrix.shape[0]):
88+
for j in range(img_matrix.shape[1]):
89+
img_matrix[i][j]=c_normalize[img_matrix[i][j]]*255
90+
91+
return img_matrix
92+
8893

8994

9095

imgprocess/utils/utility.py

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
import string
22
import random
3+
from PIL import Image
4+
from .img_lib import ImgLib
5+
import os
6+
import numpy as np
7+
38

49
def get_random_string(length):
510
letters = string.ascii_lowercase
@@ -14,4 +19,30 @@ def handle_uploaded_file(f,namepath_to_upload):
1419

1520
def move_upload_image(img_data,file_name):
1621
with open('imageprocess/static/imageprocess/images/'+file_name, 'wb+') as destination:
17-
destination.write(img_data)
22+
destination.write(img_data)
23+
24+
25+
def save_image(image_file,image_path):
26+
27+
""" That function return the saved name,
28+
the path and the matrix of image
29+
"""
30+
31+
file_name,file_extension=os.path.splitext(str(image_file))
32+
saved_name=get_random_string(15)+file_extension
33+
handle_uploaded_file(image_file,"imgprocess/static/imageprocess/images/"+saved_name)
34+
35+
imgglib=ImgLib()
36+
37+
img_final=Image.open("imgprocess/static/imageprocess/images/"+saved_name)
38+
39+
img_list_data=list(img_final.getdata())
40+
41+
img_matrix=np.reshape(img_list_data,img_final.size)
42+
43+
return {"matrix":img_matrix,
44+
"path":"imgprocess/static/imageprocess/images/"+saved_name,
45+
"name":saved_name,
46+
"list":img_list_data,
47+
"extension":file_extension}
48+

0 commit comments

Comments
 (0)