File: pilutils.py

package info (click to toggle)
python-fabio 0.11.0%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 9,092 kB
  • sloc: python: 19,244; ansic: 1,085; makefile: 219; sh: 215
file content (135 lines) | stat: -rw-r--r-- 4,273 bytes parent folder | download | duplicates (4)
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# coding: utf-8
#
#    Project: X-ray image reader
#             https://github.com/silx-kit/fabio
#
#
#    Copyright (C) European Synchrotron Radiation Facility, Grenoble, France
#
#    Principal author:       Jérôme Kieffer (Jerome.Kieffer@ESRF.eu)
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE

"""Helper functions using Python Imaging Library (PIL)
"""

__authors__ = ["Jérôme Kieffer", "Jon Wright"]
__date__ = "25/06/2018"
__license__ = "MIT"
__copyright__ = "European Synchrotron Radiation Facility, Grenoble, France"
__status__ = "stable"

import logging
import numpy

logger = logging.getLogger(__name__)

try:
    from PIL import Image
except ImportError:
    Image = None


PIL_TO_NUMPY = {
    "I;8": numpy.uint8,
    "I;16": numpy.uint16,
    "I;16B": numpy.uint16,  # big endian
    "I;16L": numpy.uint16,  # little endian
    "I;32": numpy.uint32,
    "I;32L": numpy.uint32,  # little endian
    "I;32B": numpy.uint32,  # big endian
    "F;32F": numpy.float32,
    "F;32BF": numpy.float32,  # big endian
    "F;64F": numpy.float64,
    "F;64BF": numpy.float64,  # big endian
    "F": numpy.float32,
    "1": bool,
    "I": numpy.int32,
    "L": numpy.uint8,
    "P": numpy.uint8,
}


NUMPY_TO_PIL = {
    'float32': "F",
    'int32': "F;32NS",
    'uint32': "F;32N",
    'int16': "F;16NS",
    'uint16': "F;16N",
    'int8': "F;8S",
    'uint8': "F;8"
}


def get_numpy_array(pil_image):
    """
    Returns a numpy array from a PIL image

    :param PIL.Image pil_image: A PIL Image object
    """
    dim1, dim2 = pil_image.size
    if pil_image.mode in PIL_TO_NUMPY:
        dtype = PIL_TO_NUMPY[pil_image.mode]
    else:
        dtype = numpy.float32
        pil_image = pil_image.convert("F")
    try:
        if pil_image.mode == 'P':
            # Indexed color
            data = numpy.asarray(pil_image.convert("RGB"), dtype)
        else:
            data = numpy.asarray(pil_image, dtype)
    except Exception:
        # This PIL version do not support buffer interface
        logger.debug("Backtrace", exc_info=True)
        if hasattr(pil_image, "tobytes"):
            data = numpy.frombuffer(pil_image.tobytes(), dtype=dtype).copy()
        else:
            data = numpy.frombuffer(pil_image.tobytes(), dtype=dtype).copy()
        # byteswap ?
        if numpy.dtype(dtype).itemsize > 1:
            need_swap = False
            need_swap |= numpy.little_endian and "B" in pil_image.mode
            need_swap |= not numpy.little_endian and pil_image.mode.endswith("L")
            if need_swap:
                data.byteswap(True)
        data = data.reshape((dim2, dim1))

    return data


def create_pil_16(numpy_array):
    """
    Convert a numpy array to a Python Imaging Library 16 bit greyscale image.

    :param numpy.ndarray numpy_array: A numpy array
    """
    if Image is None:
        raise ImportError("PIL is not installed")

    size = numpy_array.shape[:2][::-1]
    if numpy_array.dtype.name in NUMPY_TO_PIL:
        mode2 = NUMPY_TO_PIL[numpy_array.dtype.name]
        mode1 = mode2[0]
    else:
        raise RuntimeError("Unknown numpy type: %s" % (numpy_array.dtype.type))
    dats = numpy_array.tobytes()
    pil_image = Image.frombuffer(mode1, size, dats, "raw", mode2, 0, 1)

    return pil_image