-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlib.py
More file actions
163 lines (116 loc) · 4.31 KB
/
lib.py
File metadata and controls
163 lines (116 loc) · 4.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
# -*- coding: utf-8 -*-
"""
Created on Wed Nov 19 10:48:11 2014
@author: Thibault
"""
from PIL import Image
import glob, os
import matplotlib.pyplot as pp
def rgb_to_hex(rgb):
return '%02x%02x%02x' % rgb
def hex_to_rgb(hex_word):
if hex_word != "":
return tuple(ord(c) for c in hex_word.decode('hex'))
return (0,0,0)
def ImageToHex(image_path):
im = Image.open(image_path)
(l, h) = im.size
C = []
for y in range(h):
for x in range(l):
color = Image.Image.getpixel(im, (x, y))
C.append(rgb_to_hex(color))
return C
def hex_to_bin(hex_string):
bina = bin(int(hex_string, 16))[2:]
while len(bina) != 24:
bina = "0" + bina
return bina
def bin_to_hex(binary_string):
if binary_string != "":
hexa = hex(int(binary_string, 2))[2:]
while len(hexa) != 6:
hexa = "0" + hexa
return hexa
return ""
def ImageToBin(image_name):
C = ImageToHex("Images/" + image_name)
T = ""
for h in C:
T += hex_to_bin(h)
fichier = open('Files/' + image_name.split('.')[0] + ".txt", "w")
fichier.write(T + '\n')
def BinToImage(file_name, l, h):
fichier = open('Files/' + file_name + ".txt", "r")
C = fichier.read()
im = Image.new("RGB", (l, h), "white")
pix = im.load()
for y in range(h):
for x in range(l):
rang = (y * l + x) * 24
pix[x, y] = hex_to_rgb(bin_to_hex(C[rang : rang + 24]))
im.save("Images/" + file_name + ".png")
def generateImage(binary_code, h, l, comment = ""):
hexa_code = bin_to_hex(binary_code)
C = [hexa_code[6 * i: 6 * (i + 1)] for i in range(len(hexa_code / 6))] # On génère la liste contenant le couleur de chaque pixels
# Comparation libarie
def compare(file_name, pas):
fichier = open('Files/' + file_name + ".txt", "r")
coded = open('Files/' + file_name + "_CODED" + ".txt", "r")
fichier = fichier.read()
coded = coded.read()
X = []
Y = []
for i in range(1001)[::pas]:
print i
decoded_without = open('Files/' + file_name + "_" + str(i) + "_DECODED_NO_C" + ".txt", "r")
decoded_with = open('Files/' + file_name + "_" + str(i) + "_DECODED_WITH_C" + ".txt", "r")
decoded_without = decoded_without.read()
decoded_with = decoded_with.read()
nb_error, nb_error_finale = 0., 0.
for j in range(len(decoded_with)):
if decoded_without[j] != fichier[j]:
nb_error += 1
if decoded_with[j] != fichier[j]:
nb_error_finale += 1
if nb_error != 0:
X.append(i)
Y.append(float(nb_error_finale * 1000. / nb_error))
print "Nb Error : " + str(nb_error)
print X,Y
pp.plot(X,Y)
plt.xlabel("Taux d'erreur (en %.)")
plt.ylabel("Taux d'erreur final (en %.)")
# BLACK AND WHITE IMAGES
def ImageToHex_BW(image_path):
im = Image.open(image_path)
(l, h) = im.size
C = []
for y in range(h):
for x in range(l):
color = Image.Image.getpixel(im, (x, y))
C.append(rgb_to_hex(color)[0:2])
return C
def hex_to_bin_BW(hex_string):
bina = bin(int(hex_string, 16))[2:]
while len(bina) != 8:
bina = "0" + bina
return bina
def ImageToBin_BW(image_name): # Block and white images
C = ImageToHex_BW("Images/" + image_name)
T = ""
for h in C:
T += hex_to_bin_BW(h)
fichier = open('Files/' + image_name.split('.')[0] + ".txt", "w")
fichier.write(T + '\n')
def generate_comparation_BW(file_name_image_ref, file_name_image_satured, l, h):
file_image_ref = open('Files/' + file_name_image_ref + ".txt", "r")
file_image_corrupted = open('Files/' + file_name_image_satured + ".txt", "r")
C1, C2 = file_image_ref.read(), file_image_corrupted.read()
im = Image.new("RGB", (l, h), "white")
pix = im.load()
for y in range(h):
for x in range(l):
rang = (y * l + x) * 8
pix[x, y] = (255,0,0) if C1[rang : rang + 8] != C2[rang : rang + 8] else hex_to_rgb(bin_to_hex(C2[rang : rang + 8]*3))
im.save("Images/COMPARED_" + file_name_image_satured + ".png")