-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathencode.py
156 lines (128 loc) · 4.98 KB
/
encode.py
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
import string
import cv2
import numpy as np
import sys
from PIL import Image
class encode_data(object):
def iter_bin(self,s):
sb = s.encode('ascii')
return (format(b, '08b') for b in sb)
def conv_ascii(cphr_txt):
obj=encode_data()
th=''
for s in obj.iter_bin(str(cphr_txt)):
th=th+s+'T'
tha = list(th)
tha[-1] = 'F'
th = str(tha)
th = th.replace('\'','')
th = th.replace(',','')
th = th.replace('[','')
th = th.replace(']','')
th = th.replace(' ','')
return th
def flip(self,v):
bv=list(bin(v)[2:])
#print(bv)
if(bv[7]!='1'):
bv[7]='1'
else:
bv[7]='0'
#print(bv)
dec=0
for dig in bv:
dec=dec*2 + int(dig)
#print(dec)
return int(dec)
def encode(selfc,rgb_list1,rgb_list2,len_counter,th):
# store_counter=(len(th)//8)
# print(rgb_list2)
f_obj=encode_data()
if len(rgb_list1)==len(rgb_list2): #if the length of both the list is same then only proceed
counter = 0
ef = 1
for i in range(len(rgb_list1)):
if(ef == 0):
break
for j in range(0,3):
if(counter >= len(th)):
ef == 0
break
len_counter+=1
d1=rgb_list1[i][j]%10
d2=rgb_list2[i][j]%10
mod=abs(d1-d2)%2
if(mod==0 and (th[counter])== '0'): #if difference is even and encoding is 0
#--------------do nothing---------------
# print('Data is 0 and mod is 0')
counter+=1
continue
elif(mod==1 and (th[counter]=='1')): #if difference is odd and encoding is 1
#--------------do nothing--------------------
# print('Data is 1 and mod is 1')
counter+=1
continue
elif(mod==0 and th[counter]=='1'):
#-----------flip------------------------
# print('Data is 0 and mod is 0')
counter+=1
rgb_list2[i][j]=f_obj.flip(rgb_list2[i][j])
#-----------------------#
elif(mod==1 and th[counter]== '0'):
#-------------flip---------------------
counter+=1
rgb_list2[i][j]=f_obj.flip(rgb_list2[i][j])
elif(th[counter] == 'F'):
print('no more data')
counter+=1
if(mod == 0):
#flip
rgb_list2[i][j]=f_obj.flip(rgb_list2[i][j])
else:
#do nothing
continue
elif(th[counter] == 'T'):
# print('there is data')
counter+=1
if(mod == 1):
#flip
# print('flipped')
rgb_list2[i][j]=f_obj.flip(rgb_list2[i][j])
else:
#do nothing
continue
#print(rgb_list2)
# print('store_counter:'+str(store_counter))
return len_counter
def extract_image(self,a,b): #-----extract image----#
img_path1='/home/akhi/Desktop/mp2/frames//frame'+str(a)+'.jpg'
img_file1 = img_path1
img = cv2.imread(img_file1, cv2.IMREAD_COLOR)
img_path2='/home/akhi/Desktop/mp2/frames/frame'+str(b)+'.jpg'
img_file2=img_path2
return img_file1,img_file2
def rgb_of_pixel(self,img_path, x, y):
im = Image.open(img_path).convert('RGB')
r, g, b = im.getpixel((x, y))
a = [r, g, b]
return a
def get_pixel(self,img_file1,img_file2,cphr_txt):
r_obj=encode_data()
#creating list 1 for 1st frame
rgb_list1=[]
for i in range(0,3*len(cphr_txt)):
rgb_list1.append(r_obj.rgb_of_pixel(img_file1,i,0))
#print(rgb_list1)
#creating list 2 for 2nd frame
rgb_list2=[]
for i in range(0,3*len(cphr_txt)):
rgb_list2.append(r_obj.rgb_of_pixel(img_file2,i,0))
#print(rgb_list2)
print('----------------------------encoding starts------------------------------------')
return rgb_list1,rgb_list2
def put_pixel(self,img_path,x,y,a):
im=Image.open(img_path)
im.putpixel((x,y),a)
def put_back(img2,rgblist2):
for i in range(0,3*len(cphr_txt)):
put_pixel(img2,i,0,tuple(rgb_list2[i]))