Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flask-SQLAlchemy: Photo column type

In a web application I'm coding with Flask/SQLAlchemy, several of my models needs a "Photo" column type, which would handle storing the original image somewhere on the filesystem, and creating differents thumbnails size of the image. Ideally, I-d want something like:

class MyModel(Base):
    id        = Column(Integer, primary_key=True)
    photo     = Column(Photo(root="/path/to/photos/", formats={
        "big"      : "800x600",
        "small"    : "400x300",
        "thumbnail": "100x75"
    }))

and then, I could access URI/URL of file like this: model.photo.big etc...

So, my question is: how to add setters/getters on the model.photo object so that I can access URIS/URLS with the mentionned syntax ? By the way, if someone has a good tutorial/resource (other than the official doc) on user defined types with SQLAlchemy, I would be grateful if he could share it.

Thx.

like image 612
arnaud briche Avatar asked Nov 24 '11 16:11

arnaud briche


2 Answers

Have you looked at Flask-Upload? It seems to be exactly what you were looking for.

like image 115
iurii Avatar answered Sep 28 '22 03:09

iurii


It is not an answer but i think it is more convenient to use a structure like described at below link for image thumbnails:

http://flask.pocoo.org/mailinglist/archive/2011/1/26/pil-to-create-thumbnails-automatically-using-tag/#32aff91e05ba9985a49a76a4fb5338d7

Using a tag for creating thumbnails, you need to store only original image path in the db.

like image 27
Ahmet Recep Navruz Avatar answered Sep 28 '22 03:09

Ahmet Recep Navruz