view sat/tools/ @ 3157:8b4354b5c05f

tools (images): fixed type for recommended_size.
author Goffi <>
date Wed, 05 Feb 2020 23:59:19 +0100
parents 559a625a236b
children 5c3bf37f2202
line wrap: on
line source

#!/usr/bin/env python3

# SAT: a jabber client
# Copyright (C) 2009-2020 Jérôme Poisson (

# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.

# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# GNU Affero General Public License for more details.

# You should have received a copy of the GNU Affero General Public License
# along with this program.  If not, see <>.

"""Methods to manipulate images"""

import tempfile
from PIL import Image
from sat.core.constants import Const as C
from pathlib import Path
from twisted.internet import threads

def checkImage(host, path, context=C.CONTEXT_CHAT):
    """Analyze image and return a report

    report will indicate if image is too large, and the recommended new size if this is
    the case
    @param host: SàT instance
    @param path(str, pathlib.Path): image to open
    @param context(str): context in which the image is transfered
    @return dict: report on image, with following keys:
        - too_large: true if image is oversized
        - recommended_size: if too_large is True, recommended size to use
    # TODO: context is not used yet
    report = {}
    image =
    max_size = tuple(host.memory.getConfig(None, "image_max", (1200, 720)))
    if image.size > max_size:
        report['too_large'] = True
        if image.size[0] > max_size[0]:
            factor = max_size[0] / image.size[0]
            if image.size[1] * factor > max_size[1]:
                factor = max_size[1] / image.size[1]
            factor = max_size[1] / image.size[1]
        report['recommended_size'] = [int(image.width*factor), int(image.height*factor)]
        report['too_large'] = False

    return report

def _resizeImageBlocking(image_path, new_size):
    im_path = Path(image_path)
    im =
    resized = im.resize(new_size, Image.LANCZOS)
    with tempfile.NamedTemporaryFile(suffix=im_path.suffix, delete=False) as f:, format=im.format)
    return Path(

def resizeImage(image_path, new_size):
    """Resize an image to a new temporary file, and return it path

    @param image_path(str, Path): path of the original image
    @param new_size(tuple[int, int]): size to use for new image
    @return (Path): path of the resized file. The image at this path must be deleted
        after use
    return threads.deferToThread(_resizeImageBlocking, image_path, new_size)