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))

显示结果:

 

posted @ 2022-09-03 23:32  逍遥散人95  阅读(370)  评论(0编辑  收藏  举报