[SciPy-user] saving raw image

Vincent Schut schut at sarvision.nl
Thu Oct 16 04:39:26 EDT 2008


Numpy int's are int32 by default, so in your '.astype(int)' you're 
casting to int32. If you want a different type, you need to specify the 
number of bits. As a side note, you're reading as *unsigned* integer 
(uint16) and then converting to *signed* int32. Try "Raw = 
fromstring(f0, uint16).astype(uint16)" to keep the original (unsigned 
int16) datatype.

Vincent.

Sahar Vilan wrote:
> Hi Zach,
> Thanks for your help.
> 
> I used your advices and got an image I can view. However, its size doubles
> and I have to open it as 32 bit while the original looks fine as 16 bit.
> Here is the code I used:
> # ************************************************
> from scipy import *
> 
> # read image
> f0 = file('Image0.raw', 'rb').read()
> Raw = fromstring(f0, uint16).astype(int)
> Im = Raw.reshape([1024, 1024], order='F')
> 
> # save image
> Im_str = Im.tostring(order='F')
> f1 = file( 'Image1.raw', 'wb').write(Im_str)
> 
> # ************************************************
> Thanks again,
> Sahar
> 
> -----Original Message-----
> From: scipy-user-bounces at scipy.org [mailto:scipy-user-bounces at scipy.org]On
> Behalf Of Zachary Pincus
> Sent: Wed, October 15, 2008 4:21 PM
> To: SciPy Users List
> Subject: Re: [SciPy-user] saving raw image
> 
> 
> Hi Sahar,
> 
> Please send a minimal example of loading and saving an array that
> produces "garbage" output, if you could... (If you could send a small
> input file that would be helpful too.)
> 
> Also, does the garbage you see in ImageJ have some structure -- does
> it look streaky, like you can see rows of pixels that should be
> together, but the rows don't line up right?
> 
> One possible problem is that the array you are saving has been
> promoted to a different dtype (e.g. by participating in signed/float
> arithmetic), so the pixels you save are no longer uint16s. Unless you
> have an 'astype(uint16)' in your save code, this could be the issue,
> since the loading code you showed does immediately convert the array
> from uint16s. (This is why sending a complete example of the failure
> is useful...)
> 
> Another possible problem has to do with the order the pixels are read/
> written out. Typically, images on disk are stored as rows of pixels
> next to one another -- this is "column major" or "fortran" order
> (going from one memory location to the next typically increments the x-
> value, except at row boundaries where the y-value is incremented, so
> it is said that the x-value "varies the fastest"). Typically, numpy
> arrays are created in row major or "C" order, where the y-value varies
> the fastest. When loading and manipulating images, you need either to
> make sure that the images are loaded in fortran-order, or reverse the
> x/y coordinates and shape.
> 
> So, e.g. when loading a 200x300 image:
> 
> 
> s = file(path, 'rb').read()
> raw = fromstring(s, uint16).astype(int)
> image = raw.reshape((200,300), order='F')
> 
> now, image[30,40] gives the same pixel as coordinate (30,40) in ImageJ.
> 
> If you did:
> image = raw.reshape((300,200), order='C')
> then image[30,40] would give the same pixel as coordinate (40,30) in
> ImageJ.
> 
> Note that the 'C' order is default. So:
> image = raw.reshape((200,300))
> will give garbage, with the rows of pixels broken up along the wrong
> boundaries, giving rise to the "streaky" images I mentioned.
> 
> Finally, the tostring() method also takes an order option, so for
> saving images, you need:
> raw = image.tostring(order='F')
> (assuming that you reshaped the image as order 'F')
> 
> Zach Pincus
> 
> 
> 
> On Oct 15, 2008, at 4:49 AM, Sahar Vilan wrote:
> 
>> I used scipy for basic image processing of raw images, and I can't
>> save
>> these images in the same format:
>> To open image I use:
>> 	s = file(path, 'rb').read()
>>    	raw = fromstring(s, uint16).astype(int)
>>
>> I try to save this matrix as raw image again but I get some garbage
>> when I
>> open it in some viewer (Image-J, for instance).
>> Can anyone help me with this?
>>
>> Thanks,
>> Sahar
> 
> _______________________________________________
> SciPy-user mailing list
> SciPy-user at scipy.org
> http://projects.scipy.org/mailman/listinfo/scipy-user
> 
> 
> 
> ****************************************************************************
> *************************** This e-mail message may contain confidential,and
> privileged information or data that constitute proprietary information of
> CMT Medical Ltd. Any review or distribution by others is strictly
> prohibited. If you are not the intended recipient you are hereby notified
> that any use of this information or data by any other person is absolutely
> prohibited. If you are not the intended recipient, please delete all copies.
> Thank You. http://www.cmt.co.il
> ****************************************************************************
> ****************************
> 
> 
> 
> 
> 
> 
> 
> ****************************************************************************
> ********
> This footnote confirms that this email message has been scanned by
> PineApp Mail-SeCure for the presence of malicious code, vandals & computer
> viruses.
> ****************************************************************************
> ********
> 
> 
> 
> 
> No virus found in this incoming message.
> Checked by AVG - http://www.avg.com
> Version: 8.0.173 / Virus Database: 270.8.0/1725 - Release Date: 14/10/2008
> 21:25
> 
> 
> 
> 
> *******************************************************************************************************
> This e-mail message may contain confidential,and  privileged information or data that constitute proprietary information of CMT Medical Ltd. Any review or distribution by others is strictly prohibited. If you are not the intended recipient you are hereby notified that any use of this information or data by any other person is absolutely prohibited. If you are not the intended recipient, please delete all copies. Thank You. http://www.cmt.co.il
> ********************************************************************************************************
> 
> 
> 
> 
> 
>  
>  
> ************************************************************************************
> This footnote confirms that this email message has been scanned by
> PineApp Mail-SeCure for the presence of malicious code, vandals & computer viruses.
> ************************************************************************************




More information about the SciPy-User mailing list