Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!selfless.tophat.at!newsfeed.xs4all.nl!newsfeed5.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'skip:# 20': 0.03; 'scipy': 0.05; 'int,': 0.07; 'pixel': 0.07; 'slice': 0.07; 'extension.': 0.09; 'from:addr:python': 0.09; 'header,': 0.09; 'subject:files': 0.09; 'subject:image': 0.09; 'pm,': 0.10; '>>>': 0.12; 'wrote:': 0.14; '"\\\\"': 0.16; "'rb')": 0.16; '2:18': 0.16; 'arrays.': 0.16; 'bit.': 0.16; 'data2': 0.16; 'from:addr:mrabarnett.plus.com': 0.16; 'from:name:mrab': 0.16; 'image1': 0.16; 'message-id:@mrabarnett.plus.com': 0.16; 'numpy': 0.16; 'pylab': 0.16; 'received:84.92': 0.16; 'received:84.92.122': 0.16; 'received:84.92.122.60': 0.16; 'received:84.93': 0.16; 'received:84.93.230': 0.16; 'reply-to:addr:python-list': 0.16; 'set,': 0.16; 'subject:raw': 0.16; 'simpler': 0.19; 'header:In- Reply-To:1': 0.21; 'set.': 0.23; 'values.': 0.23; 'worked': 0.24; 'code': 0.24; "doesn't": 0.25; 'checked': 0.25; 'like.': 0.25; 'received:84': 0.25; "i'm": 0.27; 'e.g.': 0.29; 'import': 0.29; 'skip:i 30': 0.29; 'bit': 0.30; 'array': 0.30; 'buffer,': 0.30; 'subject:working': 0.30; 'changes': 0.30; 'print': 0.31; 'to:addr :python-list': 0.33; 'file': 0.34; 'however,': 0.34; 'there': 0.35; 'header:User-Agent:1': 0.35; 'reply-to:addr:python.org': 0.35; 'using': 0.35; 'data,': 0.36; 'open': 0.36; 'something': 0.37; 'change': 0.37; 'think': 0.38; 'could': 0.38; 'but': 0.38; 'data': 0.38; 'size,': 0.38; 'subject:: ': 0.38; 'some': 0.38; 'printed': 0.39; 'subject:with': 0.39; 'to:addr:python.org': 0.39; 'really': 0.40; 'more': 0.60; 'medical': 0.65; 'here': 0.66; 'noise': 0.67; 'header:Reply-To:1': 0.72; 'reply-to:no real name:2**0': 0.72; 'messed': 0.84; 'pro': 0.89; '8bit': 0.91; 'something.': 0.91 X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: AhMIAE909k1UXebj/2dsb2JhbABSmAqOJ3eIcr8vhiQElgOLDw Date: Mon, 13 Jun 2011 21:36:01 +0100 From: MRAB User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-GB; rv:1.9.2.17) Gecko/20110414 Thunderbird/3.1.10 MIME-Version: 1.0 To: python-list@python.org Subject: Re: working with raw image files References: <0cd64f09-bd32-4b67-9266-46003dbea4b7@m4g2000yqk.googlegroups.com> <1f7a452a-8bae-43e0-ab9f-9bfed55ee206@b21g2000yqc.googlegroups.com> In-Reply-To: Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.12 Precedence: list Reply-To: python-list@python.org List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 80 NNTP-Posting-Host: 82.94.164.166 X-Trace: 1307997359 news.xs4all.nl 49175 [::ffff:82.94.164.166]:58251 X-Complaints-To: abuse@xs4all.nl Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:7555 On 13/06/2011 21:20, Wanderer wrote: > On Jun 13, 4:08 pm, Wanderer wrote: >> On Jun 13, 2:18 pm, kafooster wrote: >> >> >> >> >> >> >> >> >> >>> I am working on some medical image data, and I try to look into >>> specific slice of 3d *.raw image. I know voxels are 16 bit int, and >>> dimensions are 352*470*96. I checked it in some pro medical image >>> viewer, it is alright. However, with the code I use, I display just >>> white noise image.(but worked well for other, 8bit raw image). >>> Also, printed size is half the original size, like it was 8 bit. I >>> read some documentations on PIL, numpy etc but I think I just do not >>> understand something. >>> I open image data set, I change it to array, give it dimensions, >>> dtype, and change it to image, right? I think there is something >>> messed up in 'binvalues', but dont really know how to write it in >>> simpler way. >> >>> P.S.1 >>> If I want to change data type to e.g. 8 bit uint, is it just change in >>> scipy.array? or it requires some more changes >> >>> P.S.2 >>> Lets say I have my array of image data and want to save it to *.raw >>> data set. is it array.tofile? >> >>> Here is my code >> >>> ############################ >> >>> import scipy as sc >>> from pylab import * >>> import array >>> import Image >> >>> fileobj = open("hand.raw", 'rb') >>> binvalues = array.array('B') >>> binvalues.read (fileobj, 352*470*96) >>> data1 = sc.array(binvalues, dtype=sc.int16) >>> data2 = sc.reshape(data1, (352,470,96)) >>> fileobj.close() >>> print data2.size , data2.dtype >> >>> im = Image.fromarray(data2[:,:,40]) >>> im.show() >> >> Try using numpy arrays. >> >> import numpy as np >> import Image >> >> image1 = Image.open("hand.raw", 'rb') >> imshape = image1.size >> npArray = np.array(image1.getdata()) >> npArray.shape = imshape >> >> im = Image.fromarray(npArray) >> im.show() > > P.S.1 > If you want to change data size from a data buffer, you could use > something like. > > image1 = np.frombuffer(Buffer, np.uint16) > > P.S.2 > I'm not sure what a *.raw file is but if Image has support for it you > just need to include the extension. > > im = Image.fromarray(npArray) > im.save(self.resultDir + "\\" + imageName + '.tif') > A .raw file doesn't contain a header, just the pixel values.