欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Python程序?qū)崿F(xiàn)向MySQL存放圖片

 更新時間:2023年03月14日 08:44:17   作者:I_am_overflow  
這篇文章主要介紹了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)行圓形識別(圓檢測)實例代碼

    Python?opencv進(jìn)行圓形識別(圓檢測)實例代碼

    最近工作的項目上需要檢測圖像中是否有圓形,下面這篇文章主要給大家介紹了關(guān)于Python?opencv進(jìn)行圓形識別(圓檢測)的相關(guān)資料,文中通過實例代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2023-05-05
  • python全棧知識點總結(jié)

    python全棧知識點總結(jié)

    在本篇文章里小編給大家整理了關(guān)于python全棧的知識點以及學(xué)習(xí)路線的總結(jié),需要的朋友們參考下。
    2019-07-07
  • Numpy 改變數(shù)組維度的幾種方法小結(jié)

    Numpy 改變數(shù)組維度的幾種方法小結(jié)

    今天小編就為大家分享一篇Numpy 改變數(shù)組維度的幾種方法小結(jié),具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-08-08
  • python爬蟲爬取幽默笑話網(wǎng)站

    python爬蟲爬取幽默笑話網(wǎng)站

    這篇文章主要介紹了python爬蟲爬取幽默笑話網(wǎng)站,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2019-10-10
  • Python基于Google?Bard實現(xiàn)交互式聊天機(jī)器人

    Python基于Google?Bard實現(xiàn)交互式聊天機(jī)器人

    這篇文章主要為大家介紹了Python基于Google?Bard實現(xiàn)交互式聊天機(jī)器人示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-03-03
  • python實戰(zhàn)練習(xí)做一個隨機(jī)點名的程序

    python實戰(zhàn)練習(xí)做一個隨機(jī)點名的程序

    讀萬卷書不如行萬里路,只學(xué)書上的理論是遠(yuǎn)遠(yuǎn)不夠的,只有在實戰(zhàn)中才能獲得能力的提升,本篇文章手把手帶你用Python實現(xiàn)一個隨機(jī)點名的程序,大家可以在過程中查缺補(bǔ)漏,提升水平
    2021-10-10
  • Python使用open函數(shù)的buffering設(shè)置文件緩沖方式

    Python使用open函數(shù)的buffering設(shè)置文件緩沖方式

    這篇文章主要介紹了Python使用open函數(shù)的buffering設(shè)置文件緩沖方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-02-02
  • pandas時間序列之如何將int轉(zhuǎn)換成datetime格式

    pandas時間序列之如何將int轉(zhuǎn)換成datetime格式

    這篇文章主要介紹了pandas時間序列之如何將int轉(zhuǎn)換成datetime格式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-07-07
  • python游戲?qū)崙?zhàn)項目之童年經(jīng)典超級瑪麗

    python游戲?qū)崙?zhàn)項目之童年經(jīng)典超級瑪麗

    史上十大最經(jīng)典小霸王游戲中魂斗羅只能排在第二,那么第一是誰?最經(jīng)典最風(fēng)靡的當(dāng)屬超級瑪麗,那個戴帽子的大胡子穿著背帶褲的馬里奧哪個不認(rèn)得,小編帶你用python實現(xiàn)超級瑪麗緬懷童年
    2021-09-09
  • 沒有安裝Python的電腦運行Python代碼教程

    沒有安裝Python的電腦運行Python代碼教程

    你有沒有遇到過這種情況,自己辛苦碼完了代碼想發(fā)給別人運行看效果,可是對方竟然沒安裝Python,這要怎么運行呢?本篇文章帶你解決這個問題,需要的朋友快來看看
    2021-10-10

最新評論