1

I'm writing an app using Python and Kivy. I have a function that draws an image and exports it as a png. I'm trying to use that image and save it in a sql db as a BLOB.

The route I'm trying to take is to convert the png in to a stream using BytesIO and then placing this value (string) in to a variable which I can then send in to a db.

The issue I'm having is that within the 'local' function I'm able to convert the png object into a stream and print it BUT when I try print this same variable outside of the function it returns empty.

Any insight help would be wonderfully appreciated! I think it's because I'm using the function's memory to convert the png>IO and when leaving the function it doesn't like it. Or if you have any better solutions I'm all ears.

 def savevar(self):
        global driversig
        data = io.BytesIO(open("B.png","r+b").read())
        test = (data.getvalue())

#i've also tried wrapping this in a str() but getvalue() is a string so shouldn't matter?

        driversig = test
        print(driversig)
#this prints fine.

When I try print(driversig) outside of this function it returns empty

I've also tried print(str(driversig))

my global variable is empty. driversig = '' just incase you were wondering. I also don't get any errors when print

Cl0ud-l3ss
  • 175
  • 2
  • 10
  • Why are you involving `BytesIO` at all? `open(...).read()` gives you the contents of the file, stuffing it in a `BytesIO` object and then retrieving it is just a waste of effort. – jasonharper Apr 06 '19 at 22:45
  • Are you sure the code snippet you've shown us matches what your real code is going? Because I can't reproduce this issue. Perhpas you're `read()`ing data from the (real) file more than once (i.e. in a loop), and so you get an empty result at the end because the file has all been read already? For testing, perhaps change the `print` line to `print("driversig is", repr(driversig))` so you can see if it's been printing an empty result after the successful ones. – Blckknght Apr 06 '19 at 23:26
  • @jasonharper I need the stream for db entry no? I'll try your method now. – Cl0ud-l3ss Apr 07 '19 at 01:44
  • @Blckknght arrr yeah that could be it although there certainly isn't a loop. The strange thing is that if I place the content of the png file in a variable and then print that variable inside the function.. it prints but when it's outside of it it doesn't. It's not the entire code but the function is in it's entirety. The other bits are just calling or printing that variable outside this function. I'll try that troubleshoot. – Cl0ud-l3ss Apr 07 '19 at 01:46
  • @jasonharper you're right! calling it without wrapping it up yields the same string. But still doesn't hold in the globally assigned variable. – Cl0ud-l3ss Apr 07 '19 at 01:59
  • Even if it reads it once.. it should store that in the variable (which it does because it prints within the function); I'm stumped as to why it doesn't globally. – Cl0ud-l3ss Apr 07 '19 at 02:03

1 Answers1

0

So I figured out the issue:

driversig = '' is global

def savevar(self):
        global driversig
        data = open("B.png","r+b").read()
        test = (data.getvalue())    

        driversig = str(test)
#       ^^ this *change* is now for the local variable and is not effecting the global variable

        print(driversig)
#       ^^ hence why this prints correctly

the global variable driversig is still an empty string and I'm trying to call this local driversig which contains the bytes(as a string) globally; but in reality I'm calling the emtpy global variable.

Sorry guys and thank you!

Cl0ud-l3ss
  • 175
  • 2
  • 10