2016-01-22 07:54:28 +01:00
|
|
|
title: process_image
|
|
|
|
---
|
|
|
|
module: lektor.imagetools
|
|
|
|
---
|
2018-10-18 21:34:43 +02:00
|
|
|
signature: ctx, source_image, dst_filename, width=None, height=None, mode=ThumbnailMode.DEFAULT
|
2016-01-22 07:54:28 +01:00
|
|
|
---
|
|
|
|
summary: Build an image from a source image, optionally compressing and resizing.
|
|
|
|
---
|
|
|
|
type: function
|
|
|
|
---
|
|
|
|
version_added: 2.0
|
|
|
|
---
|
|
|
|
body:
|
|
|
|
|
2018-10-18 21:34:43 +02:00
|
|
|
This function takes a [Context :ref](../../build/context/) object, the
|
|
|
|
absolute paths to the image's source and target files, the target image's
|
|
|
|
width and/or height, and the operation mode.
|
|
|
|
In the default mode, if width or height are `None`, they are calculated
|
|
|
|
from the source image's dimensions so that the image is scaled proportionally.
|
2016-01-22 07:54:28 +01:00
|
|
|
|
|
|
|
Used internally for the implementation of [thumbnail :ref](../../db/record/thumbnail), and exposed as an API for image-processing plugins.
|
|
|
|
|
|
|
|
## Example
|
|
|
|
|
|
|
|
```python
|
|
|
|
from lektor.build_programs import AttachmentBuildProgram
|
|
|
|
from lektor.context import get_ctx
|
|
|
|
from lektor.db import Image
|
2018-10-18 21:34:43 +02:00
|
|
|
from lektor.imagetools import process_image, ThumbnailMode
|
2016-01-22 07:54:28 +01:00
|
|
|
from lektor.pluginsystem import Plugin
|
|
|
|
|
|
|
|
|
2018-10-18 21:34:43 +02:00
|
|
|
class ImageCropBuildProgram(AttachmentBuildProgram):
|
2016-01-22 07:54:28 +01:00
|
|
|
def build_artifact(self, artifact):
|
|
|
|
ctx = get_ctx()
|
2018-10-18 21:34:43 +02:00
|
|
|
width, height = 600, 400
|
2016-01-22 07:54:28 +01:00
|
|
|
source_img = artifact.source_obj.attachment_filename
|
|
|
|
artifact.ensure_dir()
|
|
|
|
|
|
|
|
process_image(ctx,
|
|
|
|
source_img,
|
|
|
|
artifact.dst_filename,
|
2018-10-18 21:34:43 +02:00
|
|
|
width, height, mode=ThumbnailMode.CROP)
|
2016-01-22 07:54:28 +01:00
|
|
|
|
|
|
|
|
2018-10-18 21:34:43 +02:00
|
|
|
class ImageCropPlugin(Plugin):
|
2016-01-22 07:54:28 +01:00
|
|
|
def on_setup_env(self, **extra):
|
2018-10-18 21:34:43 +02:00
|
|
|
self.env.add_build_program(Image, ImageCropBuildProgram)
|
2016-01-22 07:54:28 +01:00
|
|
|
```
|