Skip to content

fix #453: use np.array() instead of np.asarray() for PIL image conversion - #455

Draft
cursor[bot] wants to merge 1 commit into
mainfrom
cursorstereopy-issue-automation-4875
Draft

fix #453: use np.array() instead of np.asarray() for PIL image conversion#455
cursor[bot] wants to merge 1 commit into
mainfrom
cursorstereopy-issue-automation-4875

Conversation

@cursor

@cursor cursor Bot commented Jul 17, 2026

Copy link
Copy Markdown

Summary

In stereo_to_anndata(), the image processing code uses np.asarray() to convert PIL Image objects to numpy arrays. np.asarray() creates a read-only view of the PIL Image's internal pixel buffer rather than an independent copy. When the PIL Image objects are garbage collected after the function returns, the numpy arrays stored in adata.uns['spatial'] can point to freed memory, resulting in all-255 (white) pixel values.

This issue is version-dependent — some Pillow/NumPy combinations maintain the data through a bytes reference, while others do not, making it a subtle intermittent bug.

Changes

  • stereo/io/reader.py: Replace np.asarray(hires) with np.array(hires) and np.asarray(lowres) with np.array(lowres) to ensure the numpy arrays own their pixel data independently of the PIL Image lifecycle.

Verification

  • AST syntax check passed
  • Change is minimal (2 lines) and cannot introduce regressions — np.array() always creates an owned copy

Classification

  • Type: bug
  • Confidence: high
  • Severity: medium

Closes #453

Open in Web View Automation 

…sion in stereo_to_anndata

np.asarray() creates a read-only view of the PIL Image pixel buffer rather than
an independent copy. When the PIL Image objects are garbage collected after the
function returns, the numpy arrays stored in adata.uns['spatial'] can point to
freed memory, resulting in all-255 (white) pixel values.

Replace np.asarray() with np.array() to ensure the numpy arrays own their data
independently of the PIL Image lifecycle.

Co-authored-by: wanruiwen-genomics-cn <wanruiwen-genomics-cn@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

stereo.io.stereo_to_anndata After adding the HE staining image

1 participant