CVBpy 15.0
All Classes Namespaces Functions Variables Enumerations Enumerator Properties Modules Pages
cvb/PixelFormatConversion
1# @brief Example for converting PFNC compliant pixel formats.
2
3# CVBpy Example Script
4#
5# 1. Load a bitmap file as the source PFNC-compliant image.
6# 2. Create a pixel format converter.
7# 3. Execute conversion to create another version.
8# 4. Save the newly created image as a bitmap file.
9#
10# Requires: -
11
12import inspect
13import os
14
15import cvb
16
17
18def get_image(file_name: str) -> (cvb.Image, cvb.PfncFormat):
19 # Create a pair of a PFNC-compliant image and its pixel format.
20 pixel_format = cvb.PfncFormat.BayerGB8
21 image_source = cvb.Image.load(file_name)
22 image_format_pair = cvb.Image.from_pixel_format(
23 image_source.size, pixel_format), pixel_format
24
25 # For the right buffer layout, copy the data from an image file to
26 # the PFNC-compliant image buffer.
27 image_source.copy(image_format_pair[0])
28
29 return image_format_pair
30
31
32def get_another_representation(
33 src_image: cvb.Image, converter: cvb.PixelFormatConverter) -> cvb.Image:
34 # Create a PFNC-compliant image that will hold the converted image data.
35 dst_image = cvb.Image.from_pixel_format(src_image.size,
36 converter.destination_format)
37
38 # Convert the source data by following the configuration it will overwrite
39 # the destination image object with the converted image data.
40 converter.execute(src_image, dst_image)
41
42 return dst_image
43
44
45format_value_name_dict = {
46 v: k for k, v in dict(inspect.getmembers(cvb.PfncFormat)).items()}
47
48
49def to_string(pixel_format: int) -> str:
50 return format_value_name_dict[pixel_format]
51
52
53def save(image: cvb.Image, path: str) -> None:
54 image.save(path)
55 print(f" Saved as: {path}")
56
57
58if __name__ == '__main__':
59 print("As a source, a Bayer pattern image is given.")
60 base_name = "FruitBowl"
61 extension = ".bmp"
62 src_image_format_pair = get_image(
63 os.path.join(cvb.install_path(), "tutorial",
64 base_name + "Bayer" + extension))
65 save(src_image_format_pair[0],
66 base_name + to_string(src_image_format_pair[1]) + extension)
67
68 print("As the destination, an RGB format is given.")
69 dst_format = cvb.PfncFormat.RGB8
70
71 print("Create a pixel format converter.")
72 converter = cvb.PixelFormatConverter(src_image_format_pair[1], dst_format)
73
74 print("Convert the source to another.")
75 dst_image = get_another_representation(src_image_format_pair[0], converter)
76
77 print("Saving the converted image as a file.")
78 save(dst_image, base_name + to_string(dst_format) + extension)
The Common Vision Blox image.
Definition: __init__.py:2097
cvb.Image load(str file_name)
Loads an image with the given file name.
Definition: __init__.py:2309
cvb.Image from_pixel_format(cvb.Size2D size, int format)
Creates an uninitialized image with the given parameters.
Definition: __init__.py:2212
GenICam Pixel Format Naming Convention (PFNC) format values.
Definition: __init__.py:6858
Helper class that takes care of pixel format conversion.
Definition: __init__.py:4085
str install_path()
Directory Common Vision Blox has been installed to.
Definition: __init__.py:8318