从图像创建缩略图并以blob的形式插入SQLite [英] Create thumbnail from image and insert in SQLite as blob
本文介绍了从图像创建缩略图并以blob的形式插入SQLite的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想从图像创建缩略图,然后将该缩略图作为BLOB插入到SQLite中. (不先将缩略图另存为文件)
I'd like to create a thumbnail from an image and then insert that thumbnail in SQLite as BLOB. (without saving thumbnail as file first)
我的代码;
from PIL import Image
size = 120,120
file = "a.jpg"
imgobj = Image.open(file)
imgobj.thumbnail(size)
但是如何将其另存为BLOB到SQLite
But how to save it to SQLite as BLOB
推荐答案
嗯,有很多方法,这是其中一种:
Well, there are many ways, and this is one of them:
import sqlite3
from PIL import Image
size = 120, 120
file = "/tmp/a.jpg"
imgobj = Image.open(file)
imgobj.thumbnail(size)
con = sqlite3.connect("/tmp/imagesdb")
cur = con.cursor()
cur.execute("create table img (x blob)")
cur.execute("insert into img(x) values(?)", [ buffer(imgobj.tostring()) ] )
con.commit()
cur.close()
con.close()
# read it back.
con = sqlite3.connect("/tmp/imagesdb")
cur = con.cursor()
row = cur.execute('SELECT * FROM img')
for item in row:
print item #dont worry just pointers to files...
#print item[0] # has actually binary contents.
这篇关于从图像创建缩略图并以blob的形式插入SQLite的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文