Storing OpenCV Image in SQLite3 with Python
OpenCV is a huge open-source library for computer vision, machine learning, and image processing. OpenCV supports a wide variety of programming languages like Python, C++, Java, etc. It can process images and videos to identify objects, faces, or even the handwriting of a human. When it is integrated with various libraries, such as Numpy. Which is a highly optimized library for numerical operations, then the number of weapons increases in your Arsenal i.e whatever operations one can do in Numpy can be combined with OpenCV.
SQLite is a self-contained, high-reliability, embedded, full-featured, public-domain, SQL database engine. It is the most used database engine on the World Wide Web. Python has a library to access SQLite databases, called sqlite3, intended for working with this database which has been included with Python package since version 2.5.
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning - Basic Level Course
In this article, we will store an OpenCV image in sqlite3 database with Python. Let’s take this image “gfg.png” as an example:
- First import the necessary libraries.
- Connect to the sqlite3 database.
- Create a cursor object and get the current cursor location :
- Create a new table and commit it to the database.
- Open the image with open() in read mode.
- Insert the image into the table.
The above statement opens the image and then converts it into a pattern by simply interpreting the binary BLOB context. Finally, it stores that pattern into the table.
- Commit to the database.
- Store the sqlite3 table as a CSV file with pandas.
The content is stored in the table variable and then it is converted to a CSV file and is saved into the system.
- Display the content of the table.
Below is the complete program: