Common Vision Blox 15.0
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Friends Modules Pages
Image Manager/CVBpy/PixelFormatConversion

This example program is located in your CVB installation under %CVB%Tutorial/Image Manager/CVBpy/PixelFormatConversion.

pixel_format_conversion.py:

# @brief Example for converting PFNC compliant pixel formats.
# CVBpy Example Script
#
# 1. Load a bitmap file as the source PFNC-compliant image.
# 2. Create a pixel format converter.
# 3. Execute conversion to create another version.
# 4. Save the newly created image as a bitmap file.
#
# Requires: -
import inspect
import os
import cvb
def get_image(file_name: str) -> (cvb.Image, cvb.PfncFormat):
# Create a pair of a PFNC-compliant image and its pixel format.
pixel_format = cvb.PfncFormat.BayerGB8
image_source = cvb.Image.load(file_name)
image_format_pair = cvb.Image.from_pixel_format(
image_source.size, pixel_format), pixel_format
# For the right buffer layout, copy the data from an image file to
# the PFNC-compliant image buffer.
image_source.copy(image_format_pair[0])
return image_format_pair
def get_another_representation(
src_image: cvb.Image, converter: cvb.PixelFormatConverter) -> cvb.Image:
# Create a PFNC-compliant image that will hold the converted image data.
dst_image = cvb.Image.from_pixel_format(src_image.size,
converter.destination_format)
# Convert the source data by following the configuration it will overwrite
# the destination image object with the converted image data.
converter.execute(src_image, dst_image)
return dst_image
format_value_name_dict = {
v: k for k, v in dict(inspect.getmembers(cvb.PfncFormat)).items()}
def to_string(pixel_format: int) -> str:
return format_value_name_dict[pixel_format]
def save(image: cvb.Image, path: str) -> None:
image.save(path)
print(f" Saved as: {path}")
if __name__ == '__main__':
print("As a source, a Bayer pattern image is given.")
base_name = "FruitBowl"
extension = ".bmp"
src_image_format_pair = get_image(
os.path.join(cvb.install_path(), "tutorial",
base_name + "Bayer" + extension))
save(src_image_format_pair[0],
base_name + to_string(src_image_format_pair[1]) + extension)
print("As the destination, an RGB format is given.")
dst_format = cvb.PfncFormat.RGB8
print("Create a pixel format converter.")
converter = cvb.PixelFormatConverter(src_image_format_pair[1], dst_format)
print("Convert the source to another.")
dst_image = get_another_representation(src_image_format_pair[0], converter)
print("Saving the converted image as a file.")
save(dst_image, base_name + to_string(dst_format) + extension)
cvb.Image load(str file_name)
cvb.Image from_pixel_format(cvb.Size2D size, int format)
str install_path()