Python:將SQLite數據庫中的圖片導出來並且保存png格式

 1 # -*- coding: utf-8 -*-
 2 # @Time : 2022/9/3
 3 # @Author : leict
 4 # @File : SQLiteExportPicture.py
 5 
 6 
 7 # -------------
 8 # 保存圖片
 9 # -------------
10 import sqlite3
11 import pandas as pd
12 
13 
14 def saveImage(_id, byte):
15     f = open('%s_image.png' % _id, 'wb')
16     f.write(byte)
17     f.close()
18     return
19 
20 
21 if __name__ == '__main__':
22     conn = sqlite3.connect("G:\PictureImportSQLite\src\應用題四年級(上).dt")
23     cur = conn.cursor()
24     cur.execute("select * from  picture")
25 
26     # 獲取table所有字段名
27     col_name_list = [tuple[0] for tuple in cur.description]
28 
29     # 所有記錄列表
30     record_all = cur.fetchall()
31     df = pd.DataFrame(record_all, columns=col_name_list)
32 
33     for index in range(len(df)):
34         bytes_id = df['_id'][index]
35         bytes_img = df['image'][index]
36         saveImage(bytes_id, bytes_img)
37 
38     print("----------------執行完成-----總個數:%s" % len(df))

顯示結果:

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章