CVBpy 15.0
cvb/PixelFormatConversion
1# CVBpy Example Script
2#
3# 1. Load a bitmap file as the source PFNC-compliant image.
4# 2. Create a pixel format converter.
5# 3. Execute conversion to create another version.
6# 4. Save the newly created image as a bitmap file.
7#
8# Requires: -
9
10import inspect
11import os
12
13import cvb
14
15
16def get_image(file_name: str) -> (cvb.Image, cvb.PfncFormat):
17 # Create a pair of a PFNC-compliant image and its pixel format.
18 pixel_format = cvb.PfncFormat.BayerGB8
19 image_source = cvb.Image.load(file_name)
20 image_format_pair = cvb.Image.from_pixel_format(
21 image_source.size, pixel_format), pixel_format
22
23 # For the right buffer layout, copy the data from an image file to
24 # the PFNC-compliant image buffer.
25 image_source.copy(image_format_pair[0])
26
27 return image_format_pair
28
29
30def get_another_representation(
31 src_image: cvb.Image, converter: cvb.PixelFormatConverter) -> cvb.Image:
32 # Create a PFNC-compliant image that will hold the converted image data.
33 dst_image = cvb.Image.from_pixel_format(src_image.size,
34 converter.destination_format)
35
36 # Convert the source data by following the configuration it will overwrite
37 # the destination image object with the converted image data.
38 converter.execute(src_image, dst_image)
39
40 return dst_image
41
42
43format_value_name_dict = {
44 v: k for k, v in dict(inspect.getmembers(cvb.PfncFormat)).items()}
45
46
47def to_string(pixel_format: int) -> str:
48 return format_value_name_dict[pixel_format]
49
50
51def save(image: cvb.Image, path: str) -> None:
52 image.save(path)
53 print(f" Saved as: {path}")
54
55
56if __name__ == '__main__':
57 print("As a source, a Bayer pattern image is given.")
58 base_name = "FruitBowl"
59 extension = ".bmp"
60 src_image_format_pair = get_image(
61 os.path.join(cvb.install_path(), "tutorial",
62 base_name + "Bayer" + extension))
63 save(src_image_format_pair[0],
64 base_name + to_string(src_image_format_pair[1]) + extension)
65
66 print("As the destination, an RGB format is given.")
67 dst_format = cvb.PfncFormat.RGB8
68
69 print("Create a pixel format converter.")
70 converter = cvb.PixelFormatConverter(src_image_format_pair[1], dst_format)
71
72 print("Convert the source to another.")
73 dst_image = get_another_representation(src_image_format_pair[0], converter)
74
75 print("Saving the converted image as a file.")
76 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