从sqlite3检索图像并在Kivy图像小部件中显示-ValueError [英] Retrieve image from sqlite3 and display in Kivy image widget - ValueError

查看:110
本文介绍了从sqlite3检索图像并在Kivy图像小部件中显示-ValueError的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

要求

我正在尝试从数据库中检索图像并将此图像设置为kivy图像小部件,此操作将引发ValueError,不确定原因.欢迎任何输入.

I'm trying to retrieve an image from Database and set this image to kivy image widget, this operation throws a ValueError, unsure of the cause. Welcome any inputs.

数据库:Sqlite3

Database: Sqlite3

表名:用户

列:UserID,UserName,UserImage

Columns: UserID, UserName, UserImage

   def populate_fields(self): # NEW
      # Code retrieves text data and display in textinput fields here.

      # STEP 1: RETRIEVE IMAGE
      connection = sqlite3.connect("demo.db")
      with connection:
          cursor = connection.cursor()
          cursor.execute("SELECT UserImage from Users where 
          UserID=?",self.data_items[columns[0]]['text'] )
          image = cursor.fetchone()
          data = io.BytesIO(image[0])

      #STEP 2: SET OUTPUT TO IMAGE WIDGET
          self.image.source = data # ---> triggers an Error

错误跟踪:

self.image.source = data
   File "kivy\weakproxy.pyx", line 33, in kivy.weakproxy.WeakProxy.__setattr__ (kivy\weakproxy.c:1471)
   File "kivy\properties.pyx", line 478, in kivy.properties.Property.__set__ (kivy\properties.c:5572)
   File "kivy\properties.pyx", line 513, in kivy.properties.Property.set (kivy\properties.c:6352)
   File "kivy\properties.pyx", line 504, in kivy.properties.Property.set (kivy\properties.c:6173)
   File "kivy\properties.pyx", line 676, in kivy.properties.StringProperty.check (kivy\properties.c:8613)
 ValueError: Image.source accept only str

推荐答案

执行io.BytesIO()后,data以字节为单位.使用Kivy CoreImage texture转换data.

After execution of io.BytesIO(), data is in Bytes. Use Kivy CoreImage and texture to convert data.

替换

self.image.source = data

具有:

self.image.texture = CoreImage(data, ext="png").texture

图片来源

source

图像的文件名/来源.

source是 StringProperty ,默认为没有

source is a StringProperty and defaults to None

输出

这篇关于从sqlite3检索图像并在Kivy图像小部件中显示-ValueError的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆