# encoding: utf-8
import sqlite3
from io import BytesIO
from PIL import Image
import os
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
# pil
def bytes_pil_img(bytes):
# convert to byte
# print(bytes[:50])
bytes_stream = BytesIO(bytes)
# print(bytes_stream)
roiimg = Image.open(bytes_stream)
roiimg.show() # 展示图片
# plt
def bytes_plt_img(bytes):
bytes_stream = BytesIO(bytes)
roiimg = mpimg.imread(bytes_stream)
plt.imshow(roiimg) # 显示图片
plt.axis('off') # 不显示坐标轴
plt.show()
def save_image(res):
with open(r'233.png', 'wb') as f:
f.write(res)
def mdx2db(dbname, paraList, pathbase):
if os.path.isfile(dbname):
os.remove(dbname)
conn = sqlite3.connect(dbname)
c = conn.cursor()
c.execute('''CREATE TABLE pics
(ID INTEGER PRIMARY KEY AUTOINCREMENT,
tupian TEXT ,
geshi TEXT ,
image blob
);''')
conn.commit()
for item in paraList:
msql = '''INSERT INTO pics (tupian,geshi,image ) VALUES (?,?,?)'''
filename, file_extension = os.path.splitext(item)
imgPath = pathbase + '/' + item
with open(imgPath, 'rb') as input_file:
image = input_file.read()
para = (filename, file_extension, image)
c.execute(msql, para)
c.execute('select image from pics')
res = c.fetchall()[0][0]
# save_image(res)
# bytes_pil_img(res)
bytes_plt_img(res)
conn.commit()
conn.close()
mdx2db('demo.db', ['2.png'], r'C:\Users\demokey\Desktop')
sqlite3,图片的读写及显示
最新推荐文章于 2023-06-02 15:14:05 发布