image_open_data()

Creates an image from raw pixel data or a NumPy array.

Supports two modes:

Currently, NumPy does not support Python subinterpreters, which makes it almost unusable in parallel or multiprocessing environments.
It only works on the first run, and all subsequent attempts fail.
Can be run without subinterpreters with expression “py(file, false);”

Syntax

planetcnc.image_open_data(numpy_array, copy=True, swap=False)
planetcnc.image_open_data(raw_data, width, height, channels, copy=True, swap=False)

Parameters

Parameter Type Description Comment
numpy_array object A NumPy array containing pixel data. optional
raw_data object Raw image data buffer. optional
width int Image width (required for raw data input). optional
height int Image height (required for raw data input). optional
channels int Number of color channels (1, 3, or 4). optional
copy bool Whether to copy data (default: True). optional
swap bool Whether to swap byte order (default: False). optional

Return Value

Examples

#! /usr/bin/env python
 
import planetcnc
import numpy as np
 
# Example using a NumPy array
array = np.random.randint(0, 255, (100, 100, 3), dtype=np.uint8)
img = planetcnc.image_open_data(array)
 
if img:
    print("Image created successfully.")
else:
    print("Failed to create image.")
 
# Example using raw data
raw_data = bytes(100 * 100 * 3)  # 100x100 RGB image
img2 = planetcnc.image_open_data(raw_data, 100, 100, 3)
 
if img2:
    print("Image from raw data created successfully.")
#! /usr/bin/env python
 
import planetcnc
 
# Image size
width, height = 100, 100
channels = 3  # Bytes per pixel (RGB)
 
# Create a blank image (white background)
image_data = bytearray([255] * (width * height * channels))
 
# Draw a simple black line from (10,10) to (90,90)
for i in range(10, 91):  # Iterate from 10 to 90
    x, y = i, i  # Diagonal line
    if 0 <= x < width and 0 <= y < height:
        index = (y * width + x) * channels
        image_data[index:index+3] = bytes([0, 0, 0])  # Black pixel (R, G, B)
 
# Convert bytearray to bytes object (required for planetcnc.image_open_data)
image_bytes = bytes(image_data)
 
# Open the image in PlanetCNC
img = planetcnc.image_open_data(image_bytes, width, height, channels)
 
if img:
    print("Image successfully created and opened in PlanetCNC.")
 
    # Save the image (optional)
    planetcnc.image_save(img, "drawn_image.png")
 
    # Close the image
    planetcnc.image_close(img)
else:
    print("Failed to create image.")

See also

image_open
image_open_data
image_close
image_save
image_size
image_get_pixel
image_get_pixel_gray
image_to_cam
image_from_cam