Flask-SQLAlchemy: Photo Column Type

In a web application that I code using Flask / SQLAlchemy, some of my models need a β€œPhoto” column type that would handle storing the original image somewhere in the file system and create different sizes of image thumbnails. Ideally, Id wants 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 the file URI / URL like this: model.photo.big etc.

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

thanks.

+8
python flask flask-sqlalchemy sqlalchemy
source share
2 answers

This is not an answer, but I find it more convenient to use the structure described below for image thumbnails:

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

Using the tag to create thumbnails, you only need to save the original image path in db.

+1
source share

Have you watched Flask-Upload ? This seems to be exactly what you were looking for.

+1
source share

All Articles