Added Python Script to Encode/Decode Images

This commit is contained in:
Varun Tiwari 2022-10-10 00:51:10 +05:30
parent f69b292703
commit 4f486214ce
2 changed files with 43 additions and 0 deletions

View File

@ -0,0 +1,17 @@
# Encode/Decode Images
This is a Python script to encode/decode images. It uses base64 format to encode/decode images
## Usage
#### Encode
```bash
python main.py -e <IMAGE_PATH>
```
#### Decode
```bash
python main.py -d <DESTINATION_PATH>
```

View File

@ -0,0 +1,26 @@
import argparse
import base64
def encode(filepath):
image = open(filepath, 'rb')
img_encoded = base64.b64encode(image.read())
dest = open('encoded.txt', 'wb')
dest.write(img_encoded)
def decode(dest_path):
img_encoded = open('encoded.txt', 'rb')
img_decoded = base64.b64decode(img_encoded.read())
dest = open(dest_path, 'wb')
dest.write(img_decoded)
parser = argparse.ArgumentParser()
parser.add_argument("-e", "--encode", required=False, help="Encode image.")
parser.add_argument("-d", "--decode", required=False, help="Decode image.")
args = vars(parser.parse_args())
if args["encode"]:
encode(args["encode"])
else:
decode(args["decode"])