Merge pull request #288 from varunKT001/encode-decode-images

Added Python Script to Encode/Decode Images
This commit is contained in:
Agnish Ghosh 2022-10-10 11:23:52 +05:30 committed by GitHub
commit 8764d390da
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
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"])