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

SingleImage (saving as PGM)

This sample captures one image from the INFINICAM GbE-2 and saves it as a PGM image file. Without using libraries such as OpenCV, it walks through the flow of “receive, decode, save” with pypuclib alone.

  1. Enter and save the program

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

    from pypuclib import CameraFactory, PUCException
    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:
    # Receive the compressed data for one frame
    data = cam.grab()
    # Decode the compressed data into an image (a NumPy array)
    image = cam.decoder().decode(data)
    finally:
    # Always close the camera, whether this succeeded or not
    cam.close()
    if image.ndim == 3:
    print("This is a color camera. PGM is a monochrome format, so use the color sample instead.")
    raise SystemExit(1)
    height, width = image.shape
    print("Image size:", width, "x", height)
    # Save as PGM (a short header followed by the raw pixel values)
    filename = "captured_image.pgm"
    with open(filename, "wb") as f:
    f.write(f"P5\n{width} {height}\n255\n".encode("ascii"))
    f.write(image.tobytes())
    print("Saved:", filename)
  2. Run it

    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.py

    If you see the following and captured_image.pgm appears in C:\infinicam_projects\python, it worked.

    Connecting to the camera...
    Image size: 800 x 600
    Saved: captured_image.pgm
  3. Open the saved image

    The Photos app that comes with Windows cannot open PGM files. Open it with a free image tool such as:

    • ImageJ (image analysis software for research; you can open files by drag and drop)
    • IrfanView (a lightweight image viewer)
    • GIMP (image editing software)

    If you want to open it in an ordinary image viewer, use SingleImage (saving as PNG).


data = cam.grab()
image = cam.decoder().decode(data)

INFINICAM compresses each captured image inside the camera before sending it to the PC. So the data you receive cannot be used as an image as is.

Call What it does
cam.grab() Receives one frame of compressed data from the camera
cam.decoder() Gets the “decoder” that restores compressed data
.decode(data) Decodes the compressed data into an image

The decoded image is a NumPy array. For a monochrome camera it is a 600 × 800 table of numbers, and each number is the brightness of one pixel (0 = black to 255 = white). Use image.shape to get its size and image[y, x] to get the value of one pixel.

2. Always close the camera with try … finally

Section titled “2. Always close the camera with try … finally”
try:
...
finally:
cam.close()

Whatever you write under finally: always runs, even if an error occurs inside try:. close() is placed here so the camera gets closed even if receiving the image fails.

with open(filename, "wb") as f:
f.write(f"P5\n{width} {height}\n255\n".encode("ascii"))
f.write(image.tobytes())
  • open(filename, "wb") opens the file “for writing (w), in binary (b)”. With with, the file is closed automatically when the block ends.
  • The first write writes the PGM header.
  • In the second line, image.tobytes() turns the NumPy array into a sequence of bytes holding the pixel values, which is written as is.

PGM (Portable Graymap Format) is a very simple monochrome image format: a short text header followed by the raw pixel values.

P5
800 600
255
[pixel values (width × height bytes)]
  1. P5: marks this as a binary monochrome image.
  2. 800 600: the width and height of the image in pixels.
  3. 255: the maximum pixel value (255 for 8-bit monochrome).
  4. Pixel values: right after the line break, one byte per pixel.