0

The code retrieves the image data and prints, STEP 2 section of the code throws UnicodeDecodeError. In STEP 3, the retrieved image has to be displayed on the kivy coreimage widget.

def populate_fields(self, instance): # 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()
          print(image[0]) 
      # Retrieve operation works as a image byte stream is printed as output.

      # STEP 2: CONVERT BLOB TO COREIMAGE
          image_loci = image[0]
          data = io.BytesIO(open(image_loci, 'rb').read())
          #image opened in binary mode
          im = CoreImage(data, ext="png")

      #STEP 3: SET OUTPUT TO IMAGE WIDGET
          self.image.source = im
  • Database: Sqlite3

  • Table name: Users

  • Columns: UserID, UserName, UserImage

  • OS : Windows

TRACEBACK ERROR:

data = io.BytesIO(open(image_loci, 'rb').read()) #image opened in binary mode
UnicodeDecodeError: 'utf-8' codec can't decode byte 0x89 in position 0: invalid 
    start byte

I'm unable to understand the cause of this error. Appreciate any help and look forward to your inputs.

Berch
  • 185
  • 1
  • 13
  • Can you include the traceback of your error in the question? – Joel Aug 07 '18 at 14:23
  • 1
    @Joel, have included the error traceback. – Berch Aug 07 '18 at 14:37
  • you may be interested in this SO question: https://stackoverflow.com/questions/12468179/unicodedecodeerror-utf8-codec-cant-decode-byte-0x9c – Joel Aug 07 '18 at 15:18
  • 2
    Possible duplicate of [UnicodeDecodeError: 'utf8' codec can't decode byte 0x9c](https://stackoverflow.com/questions/12468179/unicodedecodeerror-utf8-codec-cant-decode-byte-0x9c) – Joel Aug 07 '18 at 15:19
  • This is quite peculiar. If you open a file in binary mode with "rb" you shouldn't get a UnicodeDecodeError, because no decoding is needed. Is this Python 2 or 3? – lenz Aug 07 '18 at 18:57
  • @lenz, I'm using 3.6.5 – Berch Aug 07 '18 at 22:57
  • @Joel Your proposed duplicate is about reading text. Here, the OP is clearly trying to read image (binary!) data. So instead of trying to get Python to decode text properly, here we need to find out why Python is attempting to text-decode at all in the first place. – lenz Aug 08 '18 at 05:58

1 Answers1

1

The first argument to open is expected to be a file name (or a file descriptor), but you are giving it image data.

I'm guessing a bit, but it's the only explanation I can think of. In Step 1 of your program, you retrieve an image blob from the DB, and assign it to the variable image. Since sqlite gives you a row with one field, you write image[0] to access it.

A comment in your code says you see an "image byte stream" when you print it, but I'm quite sure sqlite returns the data (bytes) directly, not by providing a filehandle. So I guess you see something like:

b'\x89z~fI\xa4j7&8\x00\xaf...'

Now this is what you pass to the open call. Judging from the variable name (image_loci), it seems you think this is some kind of a pointer to a filehandle, but as I said, I'm quite sure you're wrong about this. Python now tries to decode the image, because it assumes it's a path string, but it fails of course, as this isn't UTF-8 encoded text.

Now what to do? – Leave out the open call, the data are already there. Just wrap them in a BytesIO object:

# Step 2.
data = io.BytesIO(image[0])
lenz
  • 5,658
  • 5
  • 24
  • 44
  • Thank you for the explanation. Your assumptions about the code is spot on!!! Per your recommendations simplified STEP 2, # STEP 2 data = io.BytesIO(image[0]) # STEP 3 self.image.source = data # ----> trying to set retrieved image to kivy image widget This however throws a ValueError: Image.source accept only str – Berch Aug 08 '18 at 15:51
  • I'm glad if it helped. If my answer solved your problem, feel free to upvote and/or accept it, as you see fit. For the ValueError, I'm not sure I can help. To get the right attention, you should post a new question (if you haven't done so already). – lenz Aug 08 '18 at 17:00