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.
-
Enter and save the program
Save the following code as
single_image_color.pyinC:\infinicam_projects\python.import cv2from pypuclib import CameraFactory, PUCException, PUC_COLOR_TYPE, setWhiteBalanceGainprint("Connecting to the camera...")try:# Connect to the GbE-2 at this IP addresscam = 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 cameraif 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 imagedata = 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.shapeprint(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.") -
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\pythonTerminal window python single_image_color.pyIf you see the following and
captured_color.pngis created, it worked.Connecting to the camera...Image size: 800 x 600 (3 colors)Saved: captured_color.pngWith 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.
How the program works
Section titled “How the program works”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.
2. White balance
Section titled “2. White balance”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”.
3. The color image array
Section titled “3. The color image array”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 200green = image[100, 200, 1] # Green valuered = image[100, 200, 2] # Red valueBGR 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).
