Skip to content
This repository was archived by the owner on Dec 22, 2023. It is now read-only.

Added Image to base64 convertor and vice-versa #113

Merged
merged 1 commit into from
Oct 2, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
.idea/
.DS_Store
32 changes: 32 additions & 0 deletions Scripts/Miscellaneous/ImageStr_ViceVersa/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# Python script that converts Image to base64 encoding and vice versa.


### Prerequisites
* Required modules:
```base64```, ```PIL```, ```skimage``` and ```cv2```
* The ```base64``` is an inbuilt module.
* Install ```PIL``` by running ```pip install Pillow```.
* Install ```skimage``` by running ```pip install scikit-image```.
* Install OpenCV by running ```pip install opencv-python```.


### How to run the script
First you need to go to the ImageStr_ViceVersa directory.

```cd Scripts/Miscellaneous/ImageStr_ViceVersa```
* Select the image from your PC by providing its path to ```im_path``` variable.

And then run the following command once you are in project directory.

```python3 ImageStr_ViceVersa.py```


### Screenshot

![Screenshot](Screenshot.png)




## *Author Name*
Pranav Natekar
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

Large diffs are not rendered by default.

28 changes: 28 additions & 0 deletions Scripts/Miscellaneous/ImageStr_ViceVersa/converter.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import base64
from PIL import Image
import io
from skimage.metrics import structural_similarity
import cv2

im_path = "test.jpg"
# Convert to base64 encoding
print("Encoding....")
with open(im_path, "rb") as imageFile:
base64_encoding = base64.b64encode(imageFile.read())
# print(base64_encoding)

print("Decoding and writing to image...")
# Write to the image
f = io.BytesIO(base64.b64decode(base64_encoding))
pilimage = Image.open(f)
pilimage.save('image_from_encoding.jpg')
print("Image saved!")

# Let's check for the structural similarity between original and generated image.
original = cv2.imread('test.jpg', 0)
generated = cv2.imread('image_from_encoding.jpg', 0)
# Similarity index should be greater than 0.90
similarity_index = structural_similarity(original, generated)
print(F"Similarity index is {similarity_index}")


Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.