mirror of
https://github.com/metafy-social/python-scripts.git
synced 2024-11-27 14:01:12 +00:00
Added Python Script to Encode/Decode Images
This commit is contained in:
parent
f69b292703
commit
4f486214ce
17
scripts/encode-decode-images/README.md
Normal file
17
scripts/encode-decode-images/README.md
Normal 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>
|
||||||
|
```
|
26
scripts/encode-decode-images/main.py
Normal file
26
scripts/encode-decode-images/main.py
Normal 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"])
|
Loading…
Reference in New Issue
Block a user