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')
測(cè)試結(jié)果

總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
python實(shí)現(xiàn)MD5進(jìn)行文件去重的示例代碼
工作中偶爾會(huì)收到一大堆文件,名稱各不相同,分析文件的時(shí)候發(fā)現(xiàn)有不少重復(fù)的文件,導(dǎo)致工作效率低下,那么,這里就寫了一個(gè)python腳本實(shí)現(xiàn)文件去重功能,感興趣的就一起來(lái)了解一下2021-07-07
跨平臺(tái)python異步回調(diào)機(jī)制實(shí)現(xiàn)和使用方法
這篇文章主要介紹了python異步回調(diào)機(jī)制的實(shí)現(xiàn)方法,提供了使用方法代碼2013-11-11
解決python問題 Traceback (most recent call&n
這篇文章主要介紹了解決python問題 Traceback (most recent call last),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-12-12
python 實(shí)現(xiàn)目錄復(fù)制的三種小結(jié)
今天小編就為大家分享一篇python 實(shí)現(xiàn)目錄復(fù)制的三種小結(jié),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-12-12
Python實(shí)現(xiàn)的個(gè)人所得稅計(jì)算器示例
這篇文章主要介紹了Python實(shí)現(xiàn)的個(gè)人所得稅計(jì)算器,涉及Python條件判斷與數(shù)值運(yùn)算相關(guān)操作技巧,需要的朋友可以參考下2018-06-06
Python機(jī)器學(xué)習(xí)NLP自然語(yǔ)言處理基本操作新聞分類
本文是Python機(jī)器學(xué)習(xí)NLP自然語(yǔ)言處理系列文章,開始我們自然語(yǔ)言處理 (NLP) 的學(xué)習(xí)旅程. 本文主要學(xué)習(xí)NLP自然語(yǔ)言處理基本操作新聞分類2021-09-09
Python中不同進(jìn)制間的轉(zhuǎn)換實(shí)現(xiàn)
在計(jì)算機(jī)科學(xué)中,需要進(jìn)行不同進(jìn)制之間的轉(zhuǎn)換,本文主要介紹了Python中不同進(jìn)制間的轉(zhuǎn)換,具有一定的參考價(jià)值,感興趣的可以了解一下2023-10-10
Python 文件批量處理操作的實(shí)現(xiàn)示例
Python提供了豐富的工具來(lái)處理文件批量操作,包括批量重命名文件、移動(dòng)文件和修改文件內(nèi)容,具有一定的參考價(jià)值,感興趣的可以了解一下2024-12-12
python使用代理ip訪問網(wǎng)站的實(shí)例
今天小編就為大家分享一篇python使用代理ip訪問網(wǎng)站的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-05-05

