Skip to content

Better way to convert to numpy array? #175

Discussion options

You must be logged in to vote

You can cast a dip.Image to np.array using the np.asarray function, and the reverse using the dip.Image constructor. But you don't necessarily need to do this. You can use a dip.Image as input to any Numpy function, and any np.array as input to any PyDIP function.

>>> a=dip.ImageReadICS('trui')
>>> import numpy as np
>>> b=np.array(a)
>>> b
array([[115, 113, 115, ..., 145, 145, 141],
       [119, 116, 117, ..., 147, 140, 138],
       [116, 117, 120, ..., 146, 140, 142],
       ..., 
       [ 90,  90,  91, ..., 198, 204, 203],
       [ 88,  87,  87, ..., 198, 201, 202],
       [ 88,  87,  89, ..., 185, 194, 203]], dtype=uint8)
>>> np.sum(b)
9023332
>>> np.sum(a)
9023332

Note that casting …

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by crisluengo
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
2 participants
Converted from issue

This discussion was converted from issue #11 on November 25, 2024 18:52.