Skip to content

Commit 5653c7d

Browse files
akgroverLogan1x
authored andcommitted
Add Image Encoder (Logan1x#22)
1 parent 43057ca commit 5653c7d

File tree

3 files changed

+32
-0
lines changed

3 files changed

+32
-0
lines changed

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,11 @@ Encrypts or Decrypts any message you want, simply enter the message and the rota
152152
```bash
153153
python caesar_cipher.py
154154
```
155+
### Image Encoder
156+
It is a simple program to encode and decode images, which helps to reduce and handle images on server, as it is convertedto base64 address.
157+
```bash
158+
python image_encoder.py
159+
```
155160

156161
## Release History
157162

bin/ImageData/encodeData.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
{
2+
}

bin/image_encoder.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import base64
2+
import json
3+
import time
4+
5+
def b64_encode(source_filepath):
6+
with open(source_filepath, 'rb') as f:
7+
data = f.read()
8+
dest = open('ImageData/encodeData.json','r')
9+
flag = json.loads(dest.read())
10+
key = (str(int(time.time()))).decode('utf-8')
11+
d = {"data":base64.b64encode(data).decode('utf-8'),"ext":source_filepath[source_filepath.index('.'):]}
12+
flag[key] = d
13+
dest.close()
14+
dest = open('ImageData/encodeData.json','w')
15+
json.dump(flag,dest)
16+
return key
17+
18+
def b64_decode(key,dest_path):
19+
source = open('ImageData/encodeData.json','r')
20+
flag = json.loads(source.read())
21+
name = key+str(flag[key]["ext"])
22+
dest = open(dest_path+name,'wb')
23+
dest.write(base64.b64decode((flag[key]["data"]).encode('utf-8')))
24+
dest.close()
25+
return dest_path+name

0 commit comments

Comments
 (0)