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