How to encode and store images in PysonDB.

How to encode and store images in PysonDB.

·

4 min read

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

We are going to encode it.

  • We need to import base64 module first.
  • It handles the base64 encoding of image.
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.
    x=db.getDb("fs.json")
    
  • This creates or connects a Json database named fs.json
  • If you dont know pysonDB read this

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.
raw_data=base64.b64encode(dataimg.read())
  • This encodes all data in dataimg in base64 format.
 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

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")
img_data=x.getBy({"name":name})
  • This piece of code gets the base64 data from fs.json with the provided name.
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.
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

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.