简体   繁体   English

Python,Sqlite-如何将存储的图像(作为Blob或base64字符串)放入tkinter标签上的sqlite DB中

[英]Python, Sqlite - how to put an image stored (either as Blob or base64 string) in a sqlite DB on a tkinter Label

I guess this question probably will get accused of duplication but honestly I wasn't able to find my way despite spending quite long time fumbling around here and there. 我想这个问题可能会被指责为重复,但老实说,尽管我在这里和那里花了很长时间摸索,但还是找不到自己的路。

It's straightforward to store an image in a Sqlite DB as Blob but the reverse action in a way that's suitable for my intended use (which is retrieving that image in order to displaying on a label ) is not. 将图像以Blob的形式存储在Sqlite数据库中很简单,但是以适合我的预期用途的方式进行反向操作( 检索该图像在标签上显示 )是不正确的。 There is a not accepted guid here that I hope someone verifies, explains and corrects it (for instance may I use str() instead of tostring() in this line?) 有一个不接受GUID 在这里 ,我希望有人验证,解释和纠正它(例如我可以使用STR()代替的ToString()在这一行?)

data = cStringIO.StringIO(results.tostring())

There's also a lot of talk about that an image could be stored in DB as a base64 string . 关于将图像作为base64字符串存储在DB中的讨论也很多。 I think it would make the retrieving and displaying process easier maybe at the expense of extra actions while storing (by the way I couldn't find any concrete guide on how to do that). 我认为这样做可以简化检索和显示过程,但可能会牺牲存储时的额外操作(通过这种方式,我找不到有关该操作的具体指南)。

Please give some instructions in layman's term, it would be very appreciated. 请给一些外行的指示,不胜感激。

You can use the following to turn it into an Image object which tkinter could then display through a label (or elsewhere) from a base64 string. 您可以使用以下代码将其转换为Image对象,然后tkinter可以通过base64字符串中的标签(或其他位置)显示该对象。

from PIL import Image
import base64, io
# Get the base64 string
Image.open(io.BytesIO(base64.b64decode(data)))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM