API reference

Configuration

s4u.image.configure(original_path, scale_path)

Configure the filesystem paths used to store image files.

Parameters:
  • original_path – filesystem path used for full size images.
  • scale_path – filesystem path used for scaled images.
s4u.image.includeme(config)

Configure s4u.image using a Pyramid Configurator object. This will take the filesystem paths from the application settings using the keys fs.images.original and fs.images.scaled.

s4u.image.initialise_filesystem()

Create required filesystem.

This function requires that s4u.image is already configured.

It is safe to call this function multiple times: it will notice if a directory already exists.

Scaling functions

s4u.image.scale.scale_image(image, width=None, height=None, crop=False, strip_whitespace=False)

Scale the given image data to another size and return the result as a string or optionally write in to the file-like result object.

Parameters:
  • image (file) – open file for image to scale
  • width (int) – desired maximum image width
  • height (int) – desired maximum image width
  • crop (bool) – allow cropping image to fill full width and height
  • strip_whitespace (bool) – crop surrounding whitespace before processing the image

The return value is a tuple with the new image, the image format and a size-tuple.

The width, height, direction parameters will be passed to scale_pil_image(), which performs the actual scaling.

s4u.image.scale.correct_colour_mode(image)

Make sure an image uses a colour handling scheme which allows for high quality scaling and can be rendered by web browsers.

s4u.image.scale.crop_surrounding_whitespace(image)

Remove surrounding empty space around an image.

This implemenation assumes that the surrounding space has the same colour as the top leftmost pixel.

Parameters:image – PIL image
Return type:PIL image
s4u.image.scale.center_crop(image, width, height)

Crop an image to the desired width and height. The crop is made from the middle of the image.

Parameters:
  • image – PIL image
  • width (int) – maximum width, or None of width is unrestrained
  • height (int) – maximum height, or None of height is unrestrained
Return type:

PIL image

s4u.image.scale.scale_pil_image(image, width=None, height=None, crop=False)

Scale a PIL image to another size.

Parameters:
  • image – PIL Image instance
  • width (int) – desired maximum image width
  • height (int) – desired maximum image width
  • crop (bool) – allow cropping image to fill full width and height
Return type:

PIL Image

The generated image is a JPEG image, unless the original is a GIF or PNG image. This is needed to make sure alpha channel information is not lost, which JPEG does not support.

Image model

class s4u.image.model.Image(data=None, filename=None, url=None)

A source image.

filesystem_path

Return the (absolute) filesystem path for the image data.

scale(width=None, height=None, crop=False, strip_whitespace=False)

Return a scaled version of this image. If a matching ImageScale is found it is returned directly. Otherwise the image will be scaled and a new ImageScale instance is created.

See scale_image for more information on the scaling parameters.

Return type:ImageScale
class s4u.image.model.ImageScale(image, width=None, height=None, crop=False, strip_whitespace=False)

A scaled version of image. Each Image can have many different scaled versions. The final dimensions of the image are stored to allow efficient creation of HTML img tags.

width

The width in pixels of the scaled image.

height

The heighy in pixels of the scaled image.

filesystem_path

Return the (absolute) filesystem path for the image data.