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