python 向mysql中存儲圖片以及讀取圖片


python 向mysql中存儲圖片以及讀取圖片

 

轉載自:http://www.cnblogs.com/sherlockhua/archive/2012/03/29/2423786.html

python2.0

Python 操作 MySQL 數據庫

https://www.runoob.com/python/python-mysql.html

 

(十二)插入圖片

有人喜歡使用mysql來存儲圖片,而有的人喜歡把圖片存儲在文件系統中。而當我們要處理成千上萬的圖片時,會引起技術問題。圖片時二進制數據,mysql有種特殊的數據類型,用來存儲二進制數據,叫做BLOB(Binary Large Ojbect)。

開始之前,我們創建一個images表用來存儲圖片數據,代碼如下:

mysql> CREATE TABLE Images(Id INT PRIMARY KEY AUTO_INCREMENT, Data MEDIUMBLOB);

Query OK, 0 rows affected (0.06 sec)

接着,我們讀取圖片數據,並把它插入到數據庫中,示例代碼如下:

復制代碼
#!/usr/bin/python

# -*- coding: utf-8 -*-
import MySQLdb as mdb
import sys

try:
    fin = open("chrome.png")
    img = fin.read()
    fin.close()

except IOError, e:
    print "Error %d: %s" % (e.args[0],e.args[1])
    sys.exit(1)
try:
    conn = mdb.connect(host='localhost',user='testuser',
       passwd='test623', db='testdb')
    cursor = conn.cursor()
    cursor.execute("INSERT INTO Images SET Data='%s'" % \
        mdb.escape_string(img))
    conn.commit()
    cursor.close()
    conn.close()

except mdb.Error, e:
    print "Error %d: %s" % (e.args[0],e.args[1])
    sys.exit(1)
復制代碼

 

首先,我們打開一個圖片,並讀取圖片數據,代碼如下:

fin = open("chrome.png")

img = fin.read()

接着,我們把圖片數據插入到數據庫中,並使用escape_string進行特殊字符串轉義。代碼如下:

cursor.execute("INSERT INTO Images SET Data='%s'" % \

    mdb.escape_string(img))

(十三)讀取圖片

上一節中,我們把圖片存儲到數據庫中了,在本節,我們將取回並保存為圖片文件。本節示例如下:

復制代碼
#!/usr/bin/python

# -*- coding: utf-8 -*-
import MySQLdb as mdb 
import sys
try:
    conn = mdb.connect(host='localhost',user='testuser', 
        passwd='test623', db='testdb')
    cursor = conn.cursor()
    cursor.execute("SELECT Data FROM Images LIMIT 1")
    fout = open('image.png','wb')
    fout.write(cursor.fetchone()[0])
    fout.close()
    cursor.close()
    conn.close()

except IOError, e:
    print "Error %d: %s" % (e.args[0],e.args[1])
    sys.exit(1)
復制代碼

 

首先,我們從數據庫中讀取一張圖片數據:

cursor.execute("SELECT Data FROM Images LIMIT 1")

接着,我們以二進制的寫方式打開一個文件,寫入圖片數據:

fout = open('image.png','wb')

fout.write(cursor.fetchone()[0])

執行程序,我們當前目錄應該有一張圖片,驗證一下是否和寫入數據庫之前的圖片一致。

 


python3 操作方法:
https://blog.csdn.net/laomao9112/article/details/97819966
//保存
 
# -*- coding=utf-8 -*-
import pymysql
import sys
 
#讀取圖片文件
#blob最大只能存65K的文件
 
#fp = open("test.jpg",'rb',encoding='utf-8')
fp = open("IMG_0469.JPG",'rb')
img = fp.read()
fp.close()
# 創建連接
conn = pymysql.connect(host='127.0.0.1',
port=3306,
user='root',
passwd='',
db='books',
charset='utf8',
use_unicode=True,)
# 創建游標
cursor = conn.cursor()
 
#注意使用Binary()函數來指定存儲的是二進制
#cursor.execute("INSERT INTO Images SET Data= %s" % pymysql.Binary(img))
 
sql="INSERT INTO Images (Data) VALUES (%s)"
cursor.execute(sql , img)
 
# 提交,不然無法保存新建或者修改的數據
conn.commit()
 
# 關閉游標
cursor.close()
# 關閉連接
conn.close()
 

//讀取

#!/usr/bin/python

# -*- coding: utf-8 -*-
import pymysql
import sys
try:
  conn = pymysql.connect(host='localhost',user='root',
  passwd='', db='books')
  cursor = conn.cursor()
  cursor.execute("SELECT Data FROM Images LIMIT 1")
  fout = open('image.jpg','wb')
  fout.write(cursor.fetchone()[0])
  fout.close()
  cursor.close()
  conn.close()

except IOError as e:
  print("Error %d: %s" % (e.args[0],e.args[1]))
  sys.exit(1)

 

https://www.runoob.com/python3/python3-mysql.html

常用軟件開發學習資料目錄(詳見我愛分享資源論壇):  

1.經典編程電子書收藏  

2.C&C++編程學習資料收藏   

3.算法及數據結構(有關c,c++,java)   

4.Java開發學習資料收藏      

5.Android開發學習資料收藏  

6.Python開發學習資料收藏  

7.大數據,機器學習,人工智能資料收藏

8.Docker資料收藏

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM