NaraMotion / tests /test_numpy_pil.py
ArifiIslam's picture
Release NaraMotion v0.26.0: Structure of Arrays (SoA), Path Arena Allocator, Uniform Grid Indexing, Dual-Buffer Rendering & Centroid Spring Pop-in
b516e00 verified
Raw History Blame Contribute Delete
1.26 kB
import numpy as np
from PIL import Image as PILImage
import pytest
from naramotion import Image, Context, Format
def test_numpy_zero_copy_view():
img = Image(100, 100, Format.PRGB32)
with Context(img) as ctx:
ctx.fill_all("black")
arr = img.to_numpy(copy=False)
assert arr.shape == (100, 100, 4)
assert arr.dtype == np.uint8
# Modify pixel via NumPy directly
arr[50, 50] = [255, 0, 0, 255] # Blue in BGRA
# Verify that image buffer updated in-place without copy
arr2 = img.to_numpy(copy=False)
assert list(arr2[50, 50]) == [255, 0, 0, 255]
def test_image_from_numpy():
raw = np.zeros((80, 120, 4), dtype=np.uint8)
raw[:, :] = [0, 255, 0, 255] # Green in BGRA
img = Image.from_numpy(raw, Format.PRGB32)
assert img.width == 120
assert img.height == 80
arr = img.to_numpy()
assert np.array_equal(arr, raw)
def test_pil_conversion():
pil_src = PILImage.new("RGBA", (150, 100), color=(255, 128, 0, 255))
img = Image.from_pil(pil_src)
assert img.width == 150
assert img.height == 100
pil_out = img.to_pil()
assert pil_out.size == (150, 100)
assert pil_out.getpixel((50, 50)) == (255, 128, 0, 255)