Before you can use s4u.image you need tell it where it can store image data on disk. This is done by calling s4u.image.configure().

from s4u.image import configure

configure('/var/lib/photos/fullsize', '/var/lib/photos/scaled')

If you are using Pyramid application you can also configure s4u.image using Pyramid’s Configurator object. When doing this the paths will be taken from the application settings using the keys fs.images.original and fs.images.scaled.

from pyramid.config import Configurator

config = Configurator()

Images stored using s4u.image.model.Image objects. When creating an image you need to pass in the raw image data, and optionally a filename. The filename is only used determine the extension for the image file that will be created.

from s4u.sqlalchemy import meta
from s4u.image.model import Image

def load_image(filename):
    session = meta.Session()
    image = Image(open(filename).read(), filename)
    return image

An image has two useful properties: path which contains the filename for the image relative to the directories where all images are stored, and filesystem_path which contains the full filesystem path of the image.

Most of the time you do not want to use the full size image, but scaled to a specific width or height. This is handled by the scale method.

image = load_image('mugshot.png')
# Scale to maximum of 50x100 pixels
scale = image.scale(width=50, height=100)

This will return a ImageScale object which contains a scaled version of the image. Scaled images are stored automatically, so an image will never be scaled twice to the same dimensions.