So I thought the problem might go away if I changed the definition of the
table to:
thumb_field = Field ("thumb", "upload", uploadfield = "thumb_data")
db.define_table ("images",
Field ("image", "upload", uploadfield = "image_data",
requires = IS_IMAGE (extensions = ('bmp', 'gif', 'jpeg', 'jpg', 'png'),
maxsize = (3048, 2400))),
Field ("image_data", "blob"),
thumb_field, #Field ("thumb", "upload", uploadfield = "thumb_data"),
Field ("thumb_data", "blob"),
)
I thought that that way store function code would see that the type of the
field was uploadfield, but that failed, too, showing mostly that I don't
understand the internals of how Field.store and Field.retrieve work for
manually uploaded files that are stored in the database.
Is there something wrong with my original images table definition?
Would I be better served to simply store the image in a blob field and not
use the store/retrieve functions?
Any help would gratefully received.
David
On Friday, October 26, 2012 1:27:09 PM UTC-5, David Phillips wrote:
>
> I am attempting to create and store a thumbnail image in a MySQL database.
> The source image comes from the same record and has already been stored in
> the database. When my code executes, the thumbnail name gets written to the
> "thumb" field, but the "thumb_data" field is null.
>
> My table is defined like this:
>
> db.define_table ("images",
> Field ("image", "upload", uploadfield = "image_data",
> requires = IS_IMAGE (extensions = ('bmp', 'gif', 'jpeg', 'jpg', 'png'),
> maxsize = (3048, 2400))),
> Field ("image_data", "blob"),
> Field ("thumb", "upload", uploadfield = "thumb_data"),
> Field ("thumb_data", "blob"),
> auth.signature,
> )
>
> My code does this:
>
> from cStringIO import StringIO
> from PIL import Image
>
> # Get the image
> images_rec = db (db.images.id == image_id).select().first()
> filename, stream = db.images.image.retrieve (images_rec.image)
>
> # Open the image in PIL and make a thumnail
> thumb_PIL = Image.open (stream)
> thumb_PIL.thumbnail ((thumb_width, thumb_height), Image.ANTIALIAS)
>
> # Create a file-like object and save the thumbnail to it.
> thumb_file = StringIO()
> thumb_PIL.save (thumb_file, "jpeg")
>
> # Store the thumbnail
> thumb_file.seek (0)
> upload_image_rec.update_record (thumb = db.images.thumb.store (thumb_file,
> filename))
>
> I've traced through the store function code in gluon/dal.py and it looks
> to me like the problem is here (line 8461 in version 2.0.8):
>
> if isinstance(self_uploadfield,Field):
> blob_uploadfield_name = self_uploadfield.uploadfield
> keys={self_uploadfield.name: newfilename,
> blob_uploadfield_name: file.read()}
> self_uploadfield.table.insert(**keys)
>
> self_uploadfield is of type "str" with a value of "thumb_data" and the
> test "isinstance(self_uploadfield,Field)" fails. But I've checked
> carefully that the definition of the thumb and thumb_data fields follow the
> examples in the manual.
>
> I am confident that the image field is correct because I can retrieve and
> display the image from it. I also believe that the thumbnail is being
> created properly. At least, thumb_file.getvalue() returns a sensible
> looking block of data.
>
> I am using web2py version 2.0.8 on MacOS 10.8. The database is hosted on
> Amazon RDS.
>
> Thanks for any help you can give me.
>
>
--