I was wondering if someone could help me with something that I think should
be simple or basic, but I can't seem to figure out. I'm just starting to
work with web2py/python and I'm not sure how to save an image from a
request. I'm trying to save an image from a get request into an upload
database field. I feel like I'm missing one part but I'm not sure what it
is.
This is the model:
db.define_table('insect_images',
Field('accession_code', 'reference accessions',required=True),
Field('name'),
Field('image', 'upload'),
singular = 'insect_image',
plural = 'insect_images',
format = '%(name)s')
Here's the code:
import requests, json, re
from requests.auth import HTTPBasicAuth
from PIL import Image
from StringIO import StringIO
response_image =
requests.get('https://kc.kobotoolbox.org/attachment/large?media_file=%s' %
(x['filename']),stream=True)
img = Image.open(StringIO(response_image.content))
img2 = db.insect_images.image.store(img,x['filename'])
db.insect_images.insert(image=img2,accession_code=dat['accession_code_'])
When I run this, I get this error:
<type 'exceptions.AttributeError'> read
Traceback (most recent call last):
File "/usr/lib/pymodules/python2.7/gluon/restricted.py", line 205, in
restricted
exec ccode in environment
File "/home/mayrolin/web2py/applications/test1/controllers/default.py",
line 294, in <module>
File "/usr/lib/pymodules/python2.7/gluon/globals.py", line 173, in
<lambda>
self._caller = lambda f: f()
File "/usr/lib/pymodules/python2.7/gluon/tools.py", line 2575, in f
return action(*a, **b)
File "/home/mayrolin/web2py/applications/test1/controllers/default.py",
line 226, in kobofetch
img2 = db.insect_images.image.store(i,x['filename'])
File "/usr/lib/pymodules/python2.7/gluon/dal.py", line 7366, in store
shutil.copyfileobj(file, dest_file)
File "/usr/lib/python2.7/shutil.py", line 49, in copyfileobj
buf = fsrc.read(length)
File "/usr/lib/python2.7/dist-packages/PIL/Image.py", line 528, in
__getattr__
raise AttributeError(name)
AttributeError: read
Any help will be appreciated!
--
Resources:
- http://web2py.com
- http://web2py.com/book (Documentation)
- http://github.com/web2py/web2py (Source code)
- https://code.google.com/p/web2py/issues/list (Report Issues)
---
You received this message because you are subscribed to the Google Groups
"web2py-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
For more options, visit https://groups.google.com/d/optout.