Skip to content
⚡ Developer documentation for INFINICAM, the real-time high-speed camera (UC-1.1 / GbE-2)

SingleImage (color)

This sample captures one image from an INFINICAM GbE-2 color model and saves it as a color PNG image. It also shows how to check whether the connected camera is a color model.

  1. Enter and save the program

    Save the following code as single_image_color.py in C:\infinicam_projects\python.

    import cv2
    from pypuclib import CameraFactory, PUCException, PUC_COLOR_TYPE, setWhiteBalanceGain
    print("Connecting to the camera...")
    try:
    # Connect to the GbE-2 at this IP address
    cam = CameraFactory().create(ip="192.168.0.10")
    except PUCException as e:
    print("Error: could not connect to the camera.", e)
    raise SystemExit(1)
    try:
    # Check whether this is a color camera
    if cam.colortype() != PUC_COLOR_TYPE.PUC_COLOR_COLOR:
    print("Error: the connected camera is a monochrome model.")
    print("Use the SingleImage samples with monochrome models.")
    raise SystemExit(1)
    # Set the white balance (strength of red, green, and blue). 1.0 means "unchanged"
    setWhiteBalanceGain(1.0, 1.0, 1.0)
    # Receive one frame of compressed data and decode it into a color image
    data = cam.grab()
    image = cam.decoder().decode(data)
    finally:
    cam.close()
    # A color image is an array of height x width x 3 colors (blue, green, red)
    height, width, colors = image.shape
    print(f"Image size: {width} x {height} ({colors} colors)")
    filename = "captured_color.png"
    if cv2.imwrite(filename, image):
    print("Saved:", filename)
    else:
    print("Error: could not save the image.")
  2. Run it

    Connect a color model camera and run the following 2 lines, one at a time, in Command Prompt (or PowerShell).

    Terminal window
    cd C:\infinicam_projects\python
    Terminal window
    python single_image_color.py

    If you see the following and captured_color.png is created, it worked.

    Connecting to the camera...
    Image size: 800 x 600 (3 colors)
    Saved: captured_color.png

    With a monochrome camera, it prints the following and exits.

    Connecting to the camera...
    Error: the connected camera is a monochrome model.
    Use the SingleImage samples with monochrome models.

1. Check whether the camera is a color model

Section titled “1. Check whether the camera is a color model”
if cam.colortype() != PUC_COLOR_TYPE.PUC_COLOR_COLOR:

cam.colortype() returns the type of camera.

Return value Camera
PUC_COLOR_TYPE.PUC_COLOR_MONO Monochrome model
PUC_COLOR_TYPE.PUC_COLOR_COLOR Color model

Even if raise SystemExit(1) runs inside try:, cam.close() under finally: still runs, so the camera is closed properly.

setWhiteBalanceGain(1.0, 1.0, 1.0)

This sets how much to multiply red, green, and blue by when decoding a color image. The color of the lighting changes the color cast of the image, so you adjust this so that white objects come out white. 1.0 means “unchanged”.

With a monochrome camera, image is a 2-dimensional “height × width” array. With a color camera it is a 3-dimensional “height × width × 3” array. The 3 values are in blue, green, red (BGR) order.

blue = image[100, 200, 0] # Blue value of the pixel at row 100, column 200
green = image[100, 200, 1] # Green value
red = image[100, 200, 2] # Red value

BGR order is the same as OpenCV, so saving with cv2.imwrite() as is gives the correct colors. When passing the image to a library that assumes RGB order, such as Pillow, swap the color order as described in the caution on SingleImage (saving as PNG).