tools (images): max_size can now be manually specified in checkImage and dest in resizeImage:
`context` has been removed as it is not used and doesn't seem that useful.
This commit is contained in:
parent
143dd9bc0a
commit
8777bd07cb
2 changed files with 23 additions and 19 deletions
|
|
@ -370,13 +370,6 @@ class Const(object):
|
||||||
# internationalisation
|
# internationalisation
|
||||||
DEFAULT_LOCALE = "en_GB"
|
DEFAULT_LOCALE = "en_GB"
|
||||||
|
|
||||||
# Contexts
|
|
||||||
# context indicate how things are done (e.g. to who/what a file is transfered)
|
|
||||||
# this is useful to make some decision (e.g. image size)
|
|
||||||
|
|
||||||
# something is done in instant messaging
|
|
||||||
CONTEXT_CHAT = "CONTEXT_CHAT"
|
|
||||||
|
|
||||||
## Misc ##
|
## Misc ##
|
||||||
SAVEFILE_DATABASE = APP_NAME_FILE + ".db"
|
SAVEFILE_DATABASE = APP_NAME_FILE + ".db"
|
||||||
IQ_SET = '/iq[@type="set"]'
|
IQ_SET = '/iq[@type="set"]'
|
||||||
|
|
|
||||||
|
|
@ -21,27 +21,27 @@
|
||||||
|
|
||||||
import tempfile
|
import tempfile
|
||||||
from PIL import Image
|
from PIL import Image
|
||||||
from sat.core.constants import Const as C
|
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
from twisted.internet import threads
|
from twisted.internet import threads
|
||||||
|
|
||||||
|
|
||||||
def checkImage(host, path, context=C.CONTEXT_CHAT):
|
def checkImage(host, path, max_size=None):
|
||||||
"""Analyze image and return a report
|
"""Analyze image and return a report
|
||||||
|
|
||||||
report will indicate if image is too large, and the recommended new size if this is
|
report will indicate if image is too large, and the recommended new size if this is
|
||||||
the case
|
the case
|
||||||
@param host: SàT instance
|
@param host: SàT instance
|
||||||
@param path(str, pathlib.Path): image to open
|
@param path(str, pathlib.Path): image to open
|
||||||
@param context(str): context in which the image is transfered
|
@param max_size(tuple[int, int]): maximum accepted size of image
|
||||||
|
None to use value set in config
|
||||||
@return dict: report on image, with following keys:
|
@return dict: report on image, with following keys:
|
||||||
- too_large: true if image is oversized
|
- too_large: true if image is oversized
|
||||||
- recommended_size: if too_large is True, recommended size to use
|
- recommended_size: if too_large is True, recommended size to use
|
||||||
"""
|
"""
|
||||||
# TODO: context is not used yet
|
|
||||||
report = {}
|
report = {}
|
||||||
image = Image.open(path)
|
image = Image.open(path)
|
||||||
max_size = tuple(host.memory.getConfig(None, "image_max", (1200, 720)))
|
if max_size is None:
|
||||||
|
max_size = tuple(host.memory.getConfig(None, "image_max", (1200, 720)))
|
||||||
if image.size > max_size:
|
if image.size > max_size:
|
||||||
report['too_large'] = True
|
report['too_large'] = True
|
||||||
if image.size[0] > max_size[0]:
|
if image.size[0] > max_size[0]:
|
||||||
|
|
@ -57,21 +57,32 @@ def checkImage(host, path, context=C.CONTEXT_CHAT):
|
||||||
return report
|
return report
|
||||||
|
|
||||||
|
|
||||||
def _resizeImageBlocking(image_path, new_size):
|
def _resizeImageBlocking(image_path, new_size, dest=None):
|
||||||
im_path = Path(image_path)
|
im_path = Path(image_path)
|
||||||
im = Image.open(im_path)
|
im = Image.open(im_path)
|
||||||
resized = im.resize(new_size, Image.LANCZOS)
|
resized = im.resize(new_size, Image.LANCZOS)
|
||||||
with tempfile.NamedTemporaryFile(suffix=im_path.suffix, delete=False) as f:
|
|
||||||
|
if dest is None:
|
||||||
|
dest = tempfile.NamedTemporaryFile(suffix=im_path.suffix, delete=False)
|
||||||
|
elif isinstance(dest, Path):
|
||||||
|
dest = Path.open('wb')
|
||||||
|
|
||||||
|
with dest as f:
|
||||||
resized.save(f, format=im.format)
|
resized.save(f, format=im.format)
|
||||||
|
|
||||||
return Path(f.name)
|
return Path(f.name)
|
||||||
|
|
||||||
|
|
||||||
def resizeImage(image_path, new_size):
|
def resizeImage(image_path, new_size, dest=None):
|
||||||
"""Resize an image to a new temporary file, and return it path
|
"""Resize an image to a new file, and return its path
|
||||||
|
|
||||||
@param image_path(str, Path): path of the original image
|
@param image_path(str, Path): path of the original image
|
||||||
@param new_size(tuple[int, int]): size to use for new 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
|
@param dest(None, Path, file): where the resized image must be stored, can be:
|
||||||
after use
|
- None: use a temporary file
|
||||||
|
- Path: path to the file to create/overwrite
|
||||||
|
- file: a file object which must be opened for writing in binary mode
|
||||||
|
@return (Path): path of the resized file.
|
||||||
|
The image at this path should be deleted after use
|
||||||
"""
|
"""
|
||||||
return threads.deferToThread(_resizeImageBlocking, image_path, new_size)
|
return threads.deferToThread(_resizeImageBlocking, image_path, new_size, dest)
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue