How to encode and store images in PysonDB.
Tags: database, python, datascience, productivity
Published: 2020-12-04 19:14:18

Many of us find it difficult to add image to a database. PysonDB solves most of your problems now i will be showing how to store image in PysonDB

## What are we doing today:
* We are going to encode a image and then decode it.
* Then we are going to store it in PysonDB.

## Prerequisites
* Install pysondb via pip
```
pip install pysondb
```

## __Lets get started.__

We are having a image as below.
***
file2son.png
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/za4myeq7iw7dqq3q5297.png)

We are going to encode it.

* We need to import base64 module first.
* It handles the base64 encoding of image.
 
```python
from pysondb import db 
import base64
from PIL import Image
from io import BytesIO

x=db.getDb("fs.json")
def addimage(fi,name):
  with open(fi,"rb") as dataimg:
    raw_data=base64.b64encode(dataimg.read())
    x.add({"data":raw_data.decode('utf-8'),"name":name})

addimage("file2son.png","image12")
```
* We are importing pysondb,bas64 
* Other two will be handled soon.
```python
x=db.getDb("fs.json")
```
* This creates or connects a Json database named fs.json
* If you dont know pysonDB read [this](https://dev.to/fredysomy/pysondb-a-json-based-lightweight-database-for-python-ija)

***

```python
def addimage(fi,name):
  with open(fi,"rb") as dataimg:
    raw_data=base64.b64encode(dataimg.read())
    x.add({"data":raw_data.decode('utf-8'),"name":name})

addimage("file2son.png","image12")
```

* We are opening a file ( here file2son.png ) in read,binary mode as dataimg.

```python
raw_data=base64.b64encode(dataimg.read())
```

* This encodes all data in dataimg in base64 format.

```python
 x.add({"data":raw_data.decode('utf-8'),"name":name})
```
* This piece of code stores data in pysondb database .
* See that the raw_data after encoding is decoded in 'utf-8' format.
* Else error is shown.

So finally we have the data in fs.json database.

## __Lets get back the image__

```python
def getimage(filename,name):
  img_data=x.getBy({"name":name})
  img=Image.open(BytesIO(base64.b64decode(img_data[0]['data2'])))
  img.save(filename,"PNG")

getimage("file.png","image12")
```

```python
img_data=x.getBy({"name":name})
```

* This piece of code gets the base64 data from fs.json with the provided name.

```python
img=Image.open(BytesIO(base64.b64decode(img_data[0]['data2'])))
```
* Here we are Using Image from pillow (PIL) module.
* What this code does is:
  * Opens the base64 data from fs.json 
  * And BytesIO converts the data into a byte stream which is caught by the Image function and assigned to img variable.
 
```python
img.save(filename,"PNG")
```
* This saves the img in filename (here file.png ) in PNG format
* You have file.png in the directory which was encoded and decoded.

### __Whole Code__
```python
from pysondb import db
import base64
from PIL import Image
from io import BytesIO

x=db.getDb("fs.json")
def addimage(fi,name):
  with open(fi,"rb") as data:
    raw_data=base64.b64encode(data.read())
    x.add({"data":raw_data.decode('utf-8'),"name":name})


addimage("file2son.png","image12")
# addimage() is used to add file2son.png image in database with name as image12

def getimage(filename,name):
  img_data=x.getBy({"name":name})
  img=img_data[0]['data2'].decode('base64'))
  img.save(filename,"PNG")
getimage("file.png","image12")
# getimage is used to retrieve the stored image of name image12 and get it as file.png
```

#### Thats for today,Follow for more blogs.
#### Have any doubts or am i wrong anywhere,please comment here. So that i can correct it.