我跟着instructions found on SO将图像加载到sqlites中我将数据从sqlite3导出到jpeg图像失败。
你可以帮我解决这个问题吗?这是我准备图像的方式:
import io
import sqlite3
import picamera
from PIL import Image
# Create the in-memory stream
stream = io.BytesIO()
with picamera.PiCamera() as camera:
camera.rotation = 180
camera.capture(stream, format='jpeg')
# "Rewind" the stream to the beginning so we can read its content
stream.seek(0)
image = Image.open(stream)
我可以保存PIL图像进行检查:
image.save("out.jpg", "JPEG", quality=100, optimize=True, progressive=True)
然后将图像存储在上面的解决方案中:
# storing images into sqlite3 database
con = sqlite3.connect("test.db")
cur = con.cursor()
cur.execute("create table img (x blob)")
cur.execute("insert into img(x) values(?)", [ buffer( image.tostring() )] )
con.commit()
cur.close()
con.close()
我无法找到如何循环表记录并将blob导出为单个jpeg文件。
祝你好运
答案 0 :(得分:2)
将图像保存到BytesIO
对象(类似于捕获图像的方式):
stream = io.BytesIO()
image.save(stream, format="JPEG")
获取包含编码图像的bytes
对象:
imagebytes = stream.getvalue()
然后将bytes
对象作为查询参数插入查询中:
mydb.execute("INSERT INTO images VALUES (?, ?)", (imagename, imagebytes))