Open Sourcing our new Serverless Image Transformation Service

Haley Sanders
OpenPBS
Published in
2 min readAug 18, 2017

PBS’ Digital Product Development Team is proud to announce our first open source project in quite a while, the Serverless Image Transform Service (ITS)!

Check it out on GitHub today!

ITS is an overhaul of a previously primarily internal tool used to modify images on the fly for use in various PBS and Member Station digital properties. It’s intended to run on AWS Lambda (using Zappa) and pulls images stored in S3 buckets. The application itself takes query parameters and uses them as inputs to four supported transformations: fit, overlay, resize, and optimize. In practice this looks like:

https://ITS_URL/namespace/filename?fit=P1xP2&resize=P3xP4

Where ITS_URL is the API url provided by API Gateway, namespace a pre-defined mapping to an S3 bucket, and filename is the path to the image inside of the bucket. The transformations are then determined by different query parameters such as fit & resize.

Available Transformations

The Fit Transform is similar to a crop, except that it crops to a user-specified focus point and resizes the image to the user-specified size. ITS comes with three types of fit transforms, including a default that focuses on the center of the image, a focal type that accepts user-specified focus point, and a smart type that grabs the focus arguments from the file name.

The Overlay Transform places an image with an alpha channel over the input image at the user-specified position. This is necessary for cases where the user wants to add a special visual marker on an image while still leaving the original for other interfaces and uses. For example, say a user had an image of a green checkmark and wanted to put this over an image that a site visitor had clicked. Then a user could apply the green check mark using an ITS call without changing the original image file and swap out the image source urls in the HTML using jQuery. This is particularly useful on non-web clients (such as Roku) where a simple image overlay isn’t available.

The Resize Transform resizes the input image to the specified width and height while maintaining the aspect ratio.

Finally, the Optimize transform compresses the input image using Pillow and PNGQuant (where applicable), and if specified in the query, converts it to and from PNG, JPEG, or WEBP. It’s is applied automatically after all other transforms have been applied.

The best thing about ITS is that it’s also extensible — if the pre-packaged transforms don’t cover everything you need, then create a new one by inheriting from the base transform class. This is also true of the way that images are loaded, if the default loaders (local file system & S3) don’t meet your needs, implementing a new loader is straightforward as well.

We’re currently evaluating switching our legacy image transformation service to this new infrastructure and will continue to work on improving this new iteration.

Check it out on GitHub today!

(We also did some experiments to figure out what compression tools to use in Optimize. See this post to learn more!)

--

--

Haley Sanders
OpenPBS
Writer for

PBS Digital Product Development Intern | UC Davis Computer Science Department