CVBpy 15.0
All Classes Namespaces Functions Variables Enumerations Enumerator Properties Modules Pages
cvb/ImagePixelAccess
1# @brief Example for accessing image pixel values.
2
3# CVBpy Example Script
4#
5# 1. Loads an image from file.
6# 2. Converts it to a numpy array without copying
7# 3. Modify pixel values through numpy
8# 4. Save the image.
9#
10# Requires: numpy
11
12import os
13import cvb
14
15image = cvb.Image(os.path.join(cvb.install_path(), "tutorial", "Clara.bmp"))
16
17# copy=Fasle is default, but just a request
18np_array = cvb.as_array(image, copy=False)
19
20if np_array.flags["OWNDATA"]:
21 raise RuntimeError("cannot map to numpy array")
22
23# pixel access
24print("Modifying pixel data via numpy array.")
25np_array[83 : 108, 48 : 157] = 0
26
27print("Saving: ./ClaraUnknown.bmp")
28image.save("ClaraUnknown.bmp")
29
The Common Vision Blox image.
Definition: __init__.py:2097
str install_path()
Directory Common Vision Blox has been installed to.
Definition: __init__.py:8318
numpy.array as_array(Any buffer, bool copy=False)
Maps a cvb object to a numpy array.
Definition: __init__.py:8068