Reputation: 458
I'm getting URL through POST via DAJAX.
The URL is then passed into the function below. A TypeError is thrown.
I do not want to save the 'img' to disk and then reopen it to do the conversion.
I'm not sure what else to try so I figured I as the world. Thanks for the help in advance.
def getqrcode(link):
bsettings = Bitcoinsettings.objects.get(pk=1)
qr = qrcode.QRCode(version=1, error_correction=qrcode.constants.ERROR_CORRECT_H, box_size=bsettings.qrcodesize , border=5,)
qr.add_data(link)
qr.make(fit=True)
img = qr.make_image()
output = StringIO.StringIO()
img.save(output, 'GIF')
contents = output.getvalue()
data = base64.b64encode(open(contents,'rb').read())
data = "data:image/png;base64," + data
output.close()
img = []
return data
TypeError: file() argument 1 must be encoded string without NULL bytes, not str
Here is the ajax.py code.
from torgap.bitcoin.bitcoin import getqrcode
from dajaxice.decorators import dajaxice_register
from dajax.core import Dajax
@dajaxice_register
def getimage(request, image):
try:
dajax = Dajax()
link = image
image = getqrcode(link)
dajax.assign('#qrcode', 'src', image)
return dajax.json()
except Exception as e:
print e
Upvotes: 0
Views: 567
Reputation: 14360
I'm not sure you understand what returns ouput.getvalue()
since you are trying to read the file again with
data = base64.b64encode(open(contents,'rb').read())
but in the line above contents
already contains a string representation of the image file. And it is almost sure that here is where are hidden the annoying NULL bytes that file()
complains about.
Try change the line above by:
data = base64.b64encode(contents)
Also you can give a look at StringIO reference.
Upvotes: 0