Python API docs

Python 3.6 or higher is required to use scaler-pics library.

You can also download or clone git repository with examples.

Installation

To use the Scaler Python API, install the npm package as follows:
pip install scaler-pics

Basic Usage

Initialize the scaler object with your API key. Then use it to transform images as needed.
from scaler-pics import Scaler, TransformOptions, InputOptions, OutputOptions, Fit, ImageDelivery
import asyncio

scaler = Scaler('YOUR_API_KEY')

async def main():
		options = TransformOptions(
			input=InputOptions(localPath='/path/to/large-image.heic'),
			output=OutputOptions(
				type='jpeg',
				fit=Fit(width=512, height=512),
				quality=0.8
			)
		)
		response = await scaler.transform(options)
		outputImage = response['outputImage']


if __name__ == "__main__":
		asyncio.run(main())

When testing the api make sure you test from location with a good upload speed as that can greatly affect the response time. (for example, test from your server and not from your local machine)

Multiple Outputs

Generate multiple images in a single request (up to 10). Images can be returned as an ArrayBuffer, saved to a specified local path, or uploaded to a storage bucket.
import sys
from scaler_pics import Scaler, TransformOptions, InputOptions, OutputOptions, Fit, ImageDelivery
import asyncio

scaler = Scaler('YOUR_API_KEY')

async def main():
		options = TransformOptions(
			input=InputOptions(localPath='/path/to/large-image.heic'),
			output=[OutputOptions(
				type='jpeg',
				fit=Fit(width=1280, height=1280),
				imageDelivery=ImageDelivery(
						saveToLocalPath='/tmp/image-1280.jpeg'),
				quality=0.8
			),
				OutputOptions(
				type='jpeg',
				fit=Fit(width=1024, height=1024),
				imageDelivery=ImageDelivery(
						saveToLocalPath='results/output-1024x1024.jpg'),
				quality=0.8
			),
				OutputOptions(
				type='jpeg',
				fit=Fit(width=512, height=512),
				imageDelivery=ImageDelivery(
					upload=Upload(
						url='https://bucket.domain/path/to/image-1024.jpeg?signature=...',
						method='PUT'
					)
				),
				quality=0.8
			)]
		)
		response = await scaler.transform(options)
		print('response', response.to_dict())


if __name__ == "__main__":
		asyncio.run(main())

Document thumbnails and previews

Generate previews and thumbnails for documents like pdf, Word, Excel, Libre Office and many more.
from scaler-pics import Scaler, TransformOptions, InputOptions, OutputOptions, Fit, ImageDelivery
import asyncio

scaler = Scaler('YOUR_API_KEY')

async def main():
		options = TransformOptions(
			input=InputOptions(localPath='/path/to/document.pdf'),
			output=OutputOptions(
				type='jpeg',
				fit=Fit(width=1024, height=1024),
				quality=0.8
			)
		)
		response = await scaler.transform(options)
		outputImage = response['outputImage']


if __name__ == "__main__":
		asyncio.run(main())

Transform Options

Below are self-explanatory TypeScript interfaces of the transform options.
export interface TransformOptions {
	input: InputOptions;
	output?: OutputOptions | OutputOptions[];
}

interface InputOptions {
	remoteUrl?: string;
	localPath?: string;
	buffer?: Buffer;
	fileName?: string;
}

interface OutputOptions {
	fit: Fit;
	type: OutputImageType;
	quality?: number;
	imageDelivery?: ImageDelivery;
	crop?: NormalizedCrop;
}

interface Fit {
	width: number;
	height: number;
	updscale?: boolean;
}

type OutputImageType = 'jpeg' | 'png' | 'heic';

interface ImageDelivery {
	saveToLocalPath?: string;
	upload?: Upload;
	buffer?: boolean;
}

export interface Upload {
	url: string;
	method?: 'POST' | 'PUT';
}

Exactly one of the following properties of the InputOptions needs to be specified: remoteUrl or localPath or buffer. If input is set as buffer it is recommended to specify the fileName property as well. If specifying remoteUrl make sure the URL is valid and the image freely accessible.

You can set either single output or array of outputs (up to 10).

Exactly one optional parameter of ImageDelivery needs to be specified. If imageDelivery itself is undefined, the image will be delivered as an ArrayBuffer.

The upload parameter of ImageDelivery allows you to upload the image directly to services like AWS S3 bucket or Google Cloud Storage. Provide a signed URL and the method for the upload.

Transform Response

interface TransformResponse {
	inputImage: InputImageInfo;
	outputImage?: OutputImage | OutputImage[];
	timeStats: {
		signMs: number;
		sendImageMs: number;
		transformMs: number;
		getImagesMs: number;
		totalMs: number;
	};
}
								
interface InputImageInfo {
	pixelSize: Size;
	byteSize: number;
}

interface OutputImage {
	fit: Fit;
	pixelSize: Size;
	image: ImageResult;
}

type ImageResult = ArrayBuffer | string | 'uploaded';

interface Fit {
	width: number;
	height: number;
	updscale?: boolean;
}

interface Size {
	width: number;
	height: number;
}

inputImage contains information about the original image.

The image property of the OutputImage varies: it can be an ArrayBuffer, a string indicating the image was 'uploaded', or a path to the local file where the transformed image was saved.