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.
-
Enter and save the program
Save the following code as
single_image.pyinC:\infinicam_projects\python.from pypuclib import CameraFactory, PUCExceptionprint("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:# Receive the compressed data for one framedata = 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 notcam.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.shapeprint("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) -
Run it
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.pyIf you see the following and
captured_image.pgmappears inC:\infinicam_projects\python, it worked.Connecting to the camera...Image size: 800 x 600Saved: captured_image.pgm -
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).
How the program works
Section titled “How the program works”1. Receive and decode an image
Section titled “1. Receive and decode an image”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.
3. Save as PGM
Section titled “3. Save as PGM”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)”. Withwith, the file is closed automatically when the block ends.- The first
writewrites 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.
About the PGM format
Section titled “About the PGM format”PGM (Portable Graymap Format) is a very simple monochrome image format: a short text header followed by the raw pixel values.
P5800 600255[pixel values (width × height bytes)]P5: marks this as a binary monochrome image.800 600: the width and height of the image in pixels.255: the maximum pixel value (255 for 8-bit monochrome).- Pixel values: right after the line break, one byte per pixel.
