Displaying binary data ( images ) in Google Colab

We can collect binary data stored in Blob column from SQLite database. To display such data we have to use io ( Core tools for working with streams) and PIL ( Python Image Library ).
from PIL import Image
import io 
Our binary data is available in row[2]
image = Image.open(io.BytesIO(row[2]))
import matplotlib.pyplot as plt
plt.imshow(image)
plt.show()
Full code to manage SQLite Blob column data in google platform is here
# download student_blob.db SQLite Database from plus2net.com
!wget https://www.plus2net.com/python/download/student_blob.db
import sqlite3 # Connection library
my_conn = sqlite3.connect('student_blob.db') # connect to db
from PIL import Image
import io 
import matplotlib.pyplot as plt
query="pragma table_info([student_profile])"
my_data=list(my_conn.execute(query)) # structure 
print(my_data) # Listing structure of table 
r_set=my_conn.execute('SELECT * from student_profile ');

for row in r_set:
  image = Image.open(io.BytesIO(row[2]))
  plt.figure(figsize=(1,1))
  plt.axis(False)
  plt.imshow(image)
  print(row[0],row[1],'')
  plt.show()

Using SQLAlchemy connection

from sqlalchemy import create_engine,text
from sqlalchemy.exc import SQLAlchemyError
my_conn=create_engine("sqlite:///student_blob.db"+'?check_same_thread=False')
my_conn=my_conn.connect()
from PIL import Image
import io 
my_data=(1) # ID of the row to display 
q="SELECT * FROM  student_profile WHERE id=1" # query with place holders
try:
  my_cursor=my_conn.execute(text(q))
  r_set=my_cursor.fetchone()
except SQLAlchemyError as e:
  #error=str(e.__dict__['orig'])
  print(e)
else:
  student=str(r_set[0])+','+r_set[1]
  #print(student)
  image = Image.open(io.BytesIO(r_set[2]))
  import matplotlib.pyplot as plt
  plt.imshow(image)
  plt.show()
Managing SQLite Database in Google Colab platform .
From SQLite database table to Excel page and vice versa in Google colab platform


Subscribe to our YouTube Channel here


Subscribe

* indicates required
Subscribe to plus2net

    plus2net.com



    Post your comments , suggestion , error , requirements etc here





    Python Video Tutorials
    Python SQLite Video Tutorials
    Python MySQL Video Tutorials
    Python Tkinter Video Tutorials
    We use cookies to improve your browsing experience. . Learn more
    HTML MySQL PHP JavaScript ASP Photoshop Articles FORUM . Contact us
    ©2000-2024 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer