Python程序?qū)崿F(xiàn)向MySQL存放圖片
環(huán)境
Python 3.7.4 pymysql 8.0.11 MySQL Community Server
讀取圖片
以二進(jìn)制格式讀取圖片
with open("./test.jpg", "rb") as file:
image = file.read()
創(chuàng)建存放圖片的表
存放圖片字段的屬性為longblog,即long binary large object
def create_image_table(self):
sql = 'create table if not exists picture ( \
image longblob);'
try:
self.cursor.execute(sql)
self.connection.commit()
except pymysql.Error:
print(pymysql.Error)
存入MySQL
將二進(jìn)制格式的圖片數(shù)據(jù)存入MySQL
def insert_image(self, image):
sql = "insert into picture(image) values(%s)"
self.cursor.execute(sql, image)
self.connection.commit()
保存MySQL查詢得到的圖片數(shù)據(jù)為圖片
以二進(jìn)制的格式寫出圖片
def get_image(self, path):
sql = 'select * from picture'
try:
self.cursor.execute(sql)
image = self.cursor.fetchone()[0]
with open(path, "wb") as file:
file.write(image)
except pymysql.Error:
print(pymysql.Error)
except IOError:
print(IOError)
實(shí)現(xiàn)代碼
import pymysql
class Database():
'''
Description:
database demo to store image in MySQL RDBMS
Attributes:
None
'''
def __init__(self):
self.connection = pymysql.connect(host='<host name>',user='<user name>',passwd='<password>',db='<database name>',charset='utf8')
self.cursor = self.connection.cursor()
'''
Description:
create table to store images
Args:
None
Return:
None
'''
def create_image_table(self):
sql = 'create table if not exists picture ( \
image longblob);'
try:
self.cursor.execute(sql)
self.connection.commit()
except pymysql.Error:
print(pymysql.Error)
'''
Description:
insert image into table
Args:
image:
image to store
Returns:
None
'''
def insert_image(self, image):
sql = "insert into picture(image) values(%s)"
self.cursor.execute(sql, image)
self.connection.commit()
'''
Description:
get image from database
Args:
path:
path to save image
Returns:
None
'''
def get_image(self, path):
sql = 'select * from picture'
try:
self.cursor.execute(sql)
image = self.cursor.fetchone()[0]
with open(path, "wb") as file:
file.write(image)
except pymysql.Error:
print(pymysql.Error)
except IOError:
print(IOError)
'''
Description:
destruction method
Args:
None
Returns:
None
'''
def __del__(self):
self.connection.close()
self.cursor.close()
if __name__ == "__main__":
database = Database()
# read image from current directory
with open("./test.jpg", "rb") as file:
image = file.read()
database.create_image_table()
database.insert_image(image)
database.get_image('./result.jpg')
測試結(jié)果

總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Python?opencv進(jìn)行圓形識(shí)別(圓檢測)實(shí)例代碼
最近工作的項(xiàng)目上需要檢測圖像中是否有圓形,下面這篇文章主要給大家介紹了關(guān)于Python?opencv進(jìn)行圓形識(shí)別(圓檢測)的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-05-05
Python基于Google?Bard實(shí)現(xiàn)交互式聊天機(jī)器人
這篇文章主要為大家介紹了Python基于Google?Bard實(shí)現(xiàn)交互式聊天機(jī)器人示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-03-03
python實(shí)戰(zhàn)練習(xí)做一個(gè)隨機(jī)點(diǎn)名的程序
讀萬卷書不如行萬里路,只學(xué)書上的理論是遠(yuǎn)遠(yuǎn)不夠的,只有在實(shí)戰(zhàn)中才能獲得能力的提升,本篇文章手把手帶你用Python實(shí)現(xiàn)一個(gè)隨機(jī)點(diǎn)名的程序,大家可以在過程中查缺補(bǔ)漏,提升水平2021-10-10
Python使用open函數(shù)的buffering設(shè)置文件緩沖方式
這篇文章主要介紹了Python使用open函數(shù)的buffering設(shè)置文件緩沖方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-02-02
pandas時(shí)間序列之如何將int轉(zhuǎn)換成datetime格式
這篇文章主要介紹了pandas時(shí)間序列之如何將int轉(zhuǎn)換成datetime格式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-07-07
python游戲?qū)崙?zhàn)項(xiàng)目之童年經(jīng)典超級(jí)瑪麗
史上十大最經(jīng)典小霸王游戲中魂斗羅只能排在第二,那么第一是誰?最經(jīng)典最風(fēng)靡的當(dāng)屬超級(jí)瑪麗,那個(gè)戴帽子的大胡子穿著背帶褲的馬里奧哪個(gè)不認(rèn)得,小編帶你用python實(shí)現(xiàn)超級(jí)瑪麗緬懷童年2021-09-09
沒有安裝Python的電腦運(yùn)行Python代碼教程
你有沒有遇到過這種情況,自己辛苦碼完了代碼想發(fā)給別人運(yùn)行看效果,可是對(duì)方竟然沒安裝Python,這要怎么運(yùn)行呢?本篇文章帶你解決這個(gè)問題,需要的朋友快來看看2021-10-10

