-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathscaler.py
More file actions
61 lines (47 loc) · 1.79 KB
/
scaler.py
File metadata and controls
61 lines (47 loc) · 1.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# -*- coding: utf-8 -*-
#
# Licensed under the terms of the BSD 3-Clause
# (see plotpy/LICENSE for details)
# pylint: disable=C0103
"""
Scaling functions
-----------------
Overview
^^^^^^^^
The :py:mod:`.scaler` module provides scaling functions for images, thanks to
the C++ scaler engine (`_scaler` extension).
The following functions are available:
* :py:func:`.resize`: resize an image using the scaler engine
Reference
^^^^^^^^^
.. autofunction:: resize
"""
# TODO: Move all _scaler imports in this module and do something to avoid
# the need to import INTERP_LINEAR, INTERP_AA, ... in all modules using the
# scaler (code refactoring between pyplot.imshow,
# styles.BaseImageParam.update_item)
# TODO: Other functions like resize could be written in the future
import numpy as np
from plotpy._scaler import INTERP_AA, INTERP_LINEAR, INTERP_NEAREST, _scale_rect
def resize(data, shape, interpolation=None):
"""Resize array *data* to *shape* (tuple)
interpolation: 'nearest', 'linear' (default), 'antialiasing'"""
interpolate = (INTERP_NEAREST,)
if interpolation is not None:
interp_dict = {
"nearest": INTERP_NEAREST,
"linear": INTERP_LINEAR,
"antialiasing": INTERP_AA,
}
assert interpolation in interp_dict, "invalid interpolation option"
interp_mode = interp_dict[interpolation]
if interp_mode in (INTERP_NEAREST, INTERP_LINEAR):
interpolate = (interp_mode,)
if interp_mode == INTERP_AA:
aa = np.ones((5, 5), data.dtype)
interpolate = (interp_mode, aa)
out = np.empty(shape)
src_rect = (0, 0, data.shape[1], data.shape[0])
dst_rect = (0, 0, out.shape[1], out.shape[0])
_scale_rect(data, src_rect, out, dst_rect, (1.0, 0.0, None), interpolate)
return out