from BQImagePixels to numpy array without saving to disk?

16 views
Skip to first unread message

Matt O'Brien

unread,
Apr 3, 2018, 10:21:52 PM4/3/18
to bisque bioimage
Say I have a uri for an image, and I successfully get the pixels for the 2nd slice in the series of images in the dicom:

image = session.load(uri)
im = image.pixels().slice(z=0, t=2)

Now, how can I get the BQImagePixels object directly into a numpy array?

Ideally, something like this would be possible:

np_im = np.asarray(im)
or maybe
skimage.io.imread(im)

I don't want to read the image to disk, then open it again

Thanks,
Matt

Matt O'Brien

unread,
Apr 3, 2018, 10:24:35 PM4/3/18
to bisque bioimage
It would also be equally as useful to put it into an Image object in PIL directly, as that's where it will ultimately go...thanks!

Dmitry Fedorov

unread,
Apr 4, 2018, 12:14:13 PM4/4/18
to bisque-...@googlegroups.com
Hi Matt,

You are absolutely right, python API does not provide this functionality. You could implement it tough quite easily by first fetching the dimensions of the object you are trying to fetch with the "dims" operation:

/image_service/UUID?slice=,,Z,T&dims -> you'll get an XML with the dimensions of the image

and then fetching the image as an uncompressed RAW:
/image_service/UUID?slice=,,Z,T&format=raw

Then you massage the bytes to fit into a numpy array with proper dimensions. I use this technique in the MATLAB API. Personally I would not use PILLOW (PIL was deprecated a while back) since its image model is quite limited for our uses and most of the operations are available in scikit.image. For example, you will not be able to load a 3D/4D image as a PIL image.

Said that, you need to be careful with in-memory operations like that because images coming back may be quite large, you guys mostly work with very small images but an output of a slice operation may be very large. Second, file system caching and lossless compression on transmitted images would make gains from in-memory manipulation quite small if at all noticeable. 

--
__________________________________

Dmitry Fedorov Levit <di...@dimin.net>
Web: http://www.dimin.net/
__________________________________

Matt O'Brien

unread,
Apr 4, 2018, 3:38:09 PM4/4/18
to bisque-...@googlegroups.com
This sounds great and I'm totally on board, but as far as even accessing the bytes, how can I get that out of my image?

im = session.load(uri).pixels().slice(z=0, t=int(1) + 1).format('raw')

How do I access the raw pixel data from this object? 

--
You received this message because you are subscribed to a topic in the Google Groups "bisque bioimage" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/bisque-bioimage/fd26i-BllWc/unsubscribe.
To unsubscribe from this group and all its topics, send an email to bisque-bioimage+unsubscribe@googlegroups.com.
To post to this group, send email to bisque-bioimage@googlegroups.com.
Visit this group at https://groups.google.com/group/bisque-bioimage.
For more options, visit https://groups.google.com/d/optout.



--

Dmitry Fedorov

unread,
Apr 5, 2018, 4:36:07 PM4/5/18
to bisque-...@googlegroups.com
Hi Matt,

If you look at the BQApi, for example the image pixels class in bqclass.py you'll see it uses session from comm.py:

session.c.fetch (url, path=path)

comm.py is where all the communication is. It simply uses python requests library like this for blobs:

    self.c.fetch(url, path=path )

That .c in the session is the BQServer which is basically requests Session with authentication bolted on. You can see that for XML it looks something like this:
    r = self.c.fetch(url, headers = {'Content-Type':'text/xml', 'Accept':'text/xml'})
    return self.factory.string2etree(r)

here r is the response from that request. Basically you can use the fetch directly on the session and not to pass a path argument and you will get your bytes back. Now, how to reshape them into your numpy array that is a part of your task:)

-d

--
You received this message because you are subscribed to the Google Groups "bisque bioimage" group.
To unsubscribe from this group and stop receiving emails from it, send an email to bisque-bioimage+unsubscribe@googlegroups.com.

To post to this group, send email to bisque-bioimage@googlegroups.com.
Visit this group at https://groups.google.com/group/bisque-bioimage.
For more options, visit https://groups.google.com/d/optout.



--

Matt O'Brien

unread,
Apr 16, 2018, 11:39:30 PM4/16/18
to bisque bioimage
here r is the response from that request. Basically you can use the fetch directly on the session and not to pass a path argument and you will get your bytes back. Now, how to reshape them into your numpy array that is a part of your task:)

This brings us full circle to the original question. After I do what your recoomend, I have a <bqapi.bqclass.BQImagePixels at 0x7fa9541f2ad0>

This object has no method for me to use to access the bytes.



My question is very simple. How can I get the pixels OUT of the BQImagePixels container. I don't see any tools for that.

Dmitry Fedorov

unread,
Apr 17, 2018, 11:48:46 AM4/17/18
to bisque-...@googlegroups.com
My question is very simple. How can I get the pixels OUT of the BQImagePixels container. I don't see any tools for that.

Use The Source, Luke! 

BQImagePixels does not hold the pixles, it only constructs the url which is used to fetch the bytes using Requests, described in details in my previous email. Look at the fetch and you will see construction of URL and then requests call giving it a file path. If you don't give the path you'll get a buffer back.

Reply all
Reply to author
Forward
0 new messages