comparison sat/tools/images.py @ 3066:2cc2f65379f7

core: added imageCheck and imageResize methods: imageCheck will give a report on image, notably it will tell if it's too big and needs to be resized before a transfer. imageResize will create a new image with the requested size and return a path to it.
author Goffi <goffi@goffi.org>
date Tue, 29 Oct 2019 20:38:39 +0100
parents
children 9d0df638c8b4
comparison
equal deleted inserted replaced
3065:f8e3789912d0 3066:2cc2f65379f7
1 #!/usr/bin/env python3
2 # -*- coding: utf-8 -*-
3
4 # SAT: a jabber client
5 # Copyright (C) 2009-2019 Jérôme Poisson (goffi@goffi.org)
6
7 # This program is free software: you can redistribute it and/or modify
8 # it under the terms of the GNU Affero General Public License as published by
9 # the Free Software Foundation, either version 3 of the License, or
10 # (at your option) any later version.
11
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU Affero General Public License for more details.
16
17 # You should have received a copy of the GNU Affero General Public License
18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
19
20 """Methods to manipulate images"""
21
22 import tempfile
23 from PIL import Image
24 from sat.core.constants import Const as C
25 from pathlib import Path
26 from twisted.internet import threads
27
28
29 def checkImage(host, path, context=C.CONTEXT_CHAT):
30 """Analyze image and return a report
31
32 report will indicate if image is too large, and the recommended new size if this is
33 the case
34 @param host: SàT instance
35 @param path(str, pathlib.Path): image to open
36 @param context(str): context in which the image is transfered
37 @return dict: report on image, with following keys:
38 - too_large: true if image is oversized
39 - recommended_size: if too_large is True, recommended size to use
40 """
41 # TODO: context is not used yet
42 report = {}
43 image = Image.open(path)
44 max_size = tuple(host.memory.getConfig(None, "image_max", (1200, 720)))
45 if image.size > max_size:
46 report['too_large'] = True
47 if image.size[0] > max_size[0]:
48 factor = max_size[0] / image.size[0]
49 if image.size[1] * factor > max_size[1]:
50 factor = max_size[1] / image.size[1]
51 else:
52 factor = max_size[1] / image.size[1]
53 report['recommended_size'] = [image.width*factor, image.height*factor]
54 else:
55 report['too_large'] = False
56
57 return report
58
59
60 def _resizeImageBlocking(image_path, new_size):
61 im_path = Path(image_path)
62 im = Image.open(im_path)
63 resized = im.resize(new_size, Image.LANCZOS)
64 with tempfile.NamedTemporaryFile(suffix=im_path.suffix, delete=False) as f:
65 resized.save(f, format=im.format)
66 return Path(f.name)
67
68
69 def resizeImage(image_path, new_size):
70 """Resize an image to a new temporary file, and return it path
71
72 @param image_path(str, Path): path of the original image
73 @param new_size(tuple[int, int]): size to use for new image
74 @return (Path): path of the resized file. The image at this path must be deleted
75 after use
76 """
77 return threads.deferToThread(_resizeImageBlocking, image_path, new_size)