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);”
planetcnc.image_open_data(numpy_array, copy=True, swap=False) planetcnc.image_open_data(raw_data, width, height, channels, copy=True, swap=False)
#! /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.")