Skip to content

OpenVINO™ is an open source toolkit for optimizing and deploying AI inference

[PyAPI] Tensor creation from Pillow Image (#28984)

Anastasia Kuporosovab70dff8e1784
diff --git a/src/bindings/python/requirements_test.txt b/src/bindings/python/requirements_test.txt
index 5fcee45c2ff..65d8c1ecda6 100644
--- a/src/bindings/python/requirements_test.txt
+++ b/src/bindings/python/requirements_test.txt
@@ -25,6 +25,7 @@ flake8_pep3101<=2.1.0
flake8_quotes<=3.4.0
mypy
Pep8-naming
+pillow
pydocstyle
pytest-forked; sys_platform != 'win32'
pytest-xdist
diff --git a/src/bindings/python/src/pyopenvino/core/infer_request.cpp b/src/bindings/python/src/pyopenvino/core/infer_request.cpp
index 4405fbd62c9..157b4b8c6f8 100644
--- a/src/bindings/python/src/pyopenvino/core/infer_request.cpp
+++ b/src/bindings/python/src/pyopenvino/core/infer_request.cpp
@@ -108,8 +108,8 @@ void regclass_InferRequest(py::module m) {
R"(
Set output tensors using given indexes.
- :param inputs: Data to set on output tensors.
- :type inputs: Dict[int, openvino.Tensor]
+ :param outputs: Data to set on output tensors.
+ :type outputs: Dict[int, openvino.Tensor]
)");
// Python API exclusive function
diff --git a/src/bindings/python/src/pyopenvino/core/tensor.cpp b/src/bindings/python/src/pyopenvino/core/tensor.cpp
index c21e6603a91..782de265ea0 100644
--- a/src/bindings/python/src/pyopenvino/core/tensor.cpp
+++ b/src/bindings/python/src/pyopenvino/core/tensor.cpp
@@ -214,6 +214,31 @@ void regclass_Tensor(py::module m) {
py::arg("begin"),
py::arg("end"));
+ cls.def(py::init([](py::object& image) {
+ if (!py::isinstance(image, py::module::import("PIL.Image").attr("Image"))) {
+ throw py::type_error("Input must be a PIL.Image.Image object");
+ }
+ auto numpy = py::module::import("numpy");
+ py::array np_array = numpy.attr("array")(image);
+
+ return Common::object_from_data<ov::Tensor>(np_array, false);
+ }),
+ py::arg("image"),
+ R"(
+ Constructs Tensor from a Pillow Image.
+
+ :param image: Pillow Image to create the tensor from.
+ :type image: PIL.Image.Image
+ :Example:
+ .. code-block:: python
+
+ from PIL import Image
+ import openvino as ov
+
+ img = Image.open("example.jpg")
+ tensor = ov.Tensor(img)
+ )");
+
cls.def("get_element_type",
&ov::Tensor::get_element_type,
R"(
diff --git a/src/bindings/python/tests/test_runtime/test_tensor.py b/src/bindings/python/tests/test_runtime/test_tensor.py
index c3ee75ce39e..c5e0021dbd3 100644
--- a/src/bindings/python/tests/test_runtime/test_tensor.py
+++ b/src/bindings/python/tests/test_runtime/test_tensor.py
@@ -640,3 +640,23 @@ def test_copy_and_deepcopy(copy_func, should_share_data):
assert tensor_copy.data[0, 0] == outlier
else:
assert tensor_copy.data[0, 0] == value
+
+
+# supported dtypes by Pillow
[email protected](("numpy_dtype", "shape"), [
+ (np.float32, (224, 224)),
+ (np.int32, (224, 224)),
+ (np.uint8, (224, 224, 3)),
+ (np.uint16, (224, 224)),],)
+def test_tensor_from_pillow(numpy_dtype, shape):
+ from PIL import Image
+
+ arr = generate_image(shape, numpy_dtype)
+ img = Image.fromarray(arr)
+
+ tensor = ov.Tensor(img)
+ assert tensor.shape == shape
+ assert tensor.element_type == ov.Type(numpy_dtype)
+ assert isinstance(tensor.data, np.ndarray)
+ assert tensor.data.dtype == numpy_dtype
+ assert tensor.data.shape == shape