File tree Expand file tree Collapse file tree 1 file changed +65
-0
lines changed Expand file tree Collapse file tree 1 file changed +65
-0
lines changed Original file line number Diff line number Diff line change
1
+ def read_data ():
2
+ with open ('image.jpg' , 'rb' ) as f :
3
+ content = f .read ()
4
+ offset = content .index (bytes .fromhex ('FFD9' ))+ 2
5
+ f .seek (offset )
6
+ data = f .read ().decode ('ascii' )
7
+ return data
8
+
9
+ def insert_data ():
10
+ with open ('image.jpg' , 'ab' ) as f :
11
+ print ('Choose an option\n \
12
+ 1. Encode a file\n \
13
+ 2. Encode a text' )
14
+ ch = int (input ('Enter choice : ' ))
15
+ if ch == 1 :
16
+ file_to_encode = input ('Enter file to encode : ' )
17
+ with open (file_to_encode , 'rb' ) as fe :
18
+ data = fe .read ()
19
+ f .write (data )
20
+ if ch == 2 :
21
+ data = input ('Enter data to add : ' )
22
+ data += ' '
23
+ f .write (data .encode ('ascii' ))
24
+
25
+ def delete_data ():
26
+ with open ('image.jpg' , 'rb' ) as f :
27
+ content = f .read ()
28
+ offset = content .index (bytes .fromhex ('FFD9' ))+ 2
29
+ f .seek (0 )
30
+ original_data = f .read (offset )
31
+
32
+ with open ('image.jpg' , 'wb' ) as f :
33
+ f .write (original_data )
34
+ print ('Past data has been deleted' )
35
+
36
+
37
+
38
+
39
+
40
+ print ('Choices available\n \
41
+ 1. Input data into image\n \
42
+ 2. Read data from image\n \
43
+ 3. Delete extra data' )
44
+
45
+ choice = int (input ('Enter choice : ' ))
46
+
47
+ if choice == 1 :
48
+ past_data = read_data ()
49
+ if past_data != '' :
50
+ print (f'Past data exists : \' { past_data } \' ' )
51
+ c = input ('Delete past data [y/n] : ' )
52
+ if c == 'y' :
53
+ delete_data ()
54
+ insert_data ()
55
+ else :
56
+ insert_data ()
57
+ else :
58
+ insert_data ()
59
+ print ('Inserted data : \' ' , read_data (), '\' ' )
60
+
61
+ if choice == 2 :
62
+ print (read_data ())
63
+
64
+ if choice == 3 :
65
+ delete_data ()
You can’t perform that action at this time.
0 commit comments