Project

General

Profile

Morph-M Python - ImportExport to other python librairies » History » Revision 2

Revision 1 (Serge Koudoro, 10/23/2009 04:03 PM) → Revision 2/3 (Serge Koudoro, 10/23/2009 04:06 PM)

h1. Morph-M Python - ImportExport to other python librairies 

 h2{color:#8B0000;background:#ddd}. Introduction 

 Sometimes, you need to convert an image created by morphee to the image format of other libraries. It's possible to make this conversion and then to transform them back to MorpheeImages. 

 h2{color:#8B0000;background:#ddd}. PIL(Python Image Library) 

 bq. *%{color:#006400}+Morph-M Gray images To PIL...+%* 

 <pre><code class="ruby"> 
 im = mp.fileRead("testGray.png") 
 imPIL = pil.new("L",(im.getWxSize(),im.getWySize())) 
 data =mp.ImageToString(im,1,0,mp.colorType.GRAY) 
 imPIL.fromstring(data) 
 </code></pre> 

 bq. *%{color:#006400}+... To Morph-M+%* 

 <pre><code class="ruby"> 
 imMorp=mp.ImCreate(imPIL.size[0],imPIL.size[1],"UINT8") 
 data = imPIL.tostring() 
 mp.ImageFromString(imMorp, data, 1, 0, colorType.GRAY) 
 </code></pre> 

 bq. *%{color:#006400}+Morph-M Color images to PIL...+%* 

 <pre><code class="ruby"> 
 import morphee as mp 
 import Image as pil 
 im = mp.fileRead("testRGB.png") 
 imPIL = pil.new("RGB",(im.getWxSize(),im.getWySize())) 
 data =mp.ImageToString(im,3,0,mp.colorType.ARGB) 
 imPIL.fromstring(data) 
 </code></pre> 

 h2{color:#8B0000;background:#ddd}. wxPython 

 bq. *%{color:#006400}+Morph-M Images To WX+%* 

 <pre><code class="ruby"> 
 import morphee as mp 
 import wx 
 im = mp.fileRead("testRGB.png") 
 imWX = wx.EmptyImage(im.getWxSize(), im.getWySize()) 
 data = mp.ImageToString(im,3,0,mp.colorType.ABGR) 
 imWX.SetData(data) 
 </code></pre> 

 h2{color:#8B0000;background:#ddd}. Numpy(the replacement for Numeric/numarray) 

 bq. *%{color:#006400}+Morph-M Images To Numpy....+%* 

 <pre><code class="ruby"> 
 import morphee as mp 
 import numpy 
 im = mp.fileRead("testGray.png") 
 arr = mp.ImageToNumarray(im) 
 </code></pre> 

 bq. *%{color:#006400}+... To Morph-M+%* 

 <pre><code class="ruby"> 
 imMorp=mp.ImCreate(arr.shape[0],arr.shape[1],"UINT8") 
 im.fromList( map(int,arr.flat) ) 
 </code></pre> 

 h2{color:#8B0000;background:#ddd}. VTK images 

 bq. *%{color:#006400}+Morph-M Images To VTK+%* 

 <pre><code class="ruby"> 
 import morphee as mp 
 import vtk 
 im = mp.fileRead("testGray.png") 
 str = mp.ImageToString(im,1,0,mp.colorType.GRAY) 
 x=im.getXSize() 
 y=im.getYSize() 
 z=im.getZSize() 
 imimport = vtk.vtkImageImport() 
 imimport.CopyImportVoidPointer(str,x*y*z) 
 imimport.SetDataScalarTypeToUnsignedChar() 
 imimport.SetDataExtent(0,x-1,0,y-1,0,z-1) 
 imimport.SetWholeExtent(0,x-1,0,y-1,0,z-1) 
 imdata = imimport.GetOutput() 
 imdata.Update() 
 </code></pre> 

 h2{color:#8B0000;background:#ddd}. Qt4 Image 

 bq. *%{color:#006400}+Morph-M To QT4: Gray level images+%* 

 <pre><code class="ruby"> 
 import morphee as mp 
 from PyQt4 import QtGui 
 # create/read the image 
 im = mp.fileRead("testGray.png") 
 x=im.getXSize() 
 y=im.getYSize() 
 sdata=mp.ImageToString(im,1,(4 - x % 4) % 4,mp.colorType.GRAY) 
 fmt = QtGui.QImage.Format_Indexed8 
 img = QtGui.QImage(sdata, x, y, fmt) 
 # make sure the image is displayed in shades of gray 
 # (and not with a funky LUT) 
 img.setColorTable([QtGui.qRgb(i,i,i) for i in range(0,256)]) 
 </code></pre> 

 bq. *%{color:#006400}+Morph-M To QT4: Color images+%* 

 <pre><code class="ruby"> 
 # create/read the color image 
 im = mp.fileRead("testRGB.png") 
 # transfering the data to a qtpixmap 
 sdata = mp.ImageToString(imLUT,4,0,mp.colorType.ARGB) 
 x=imLUT.getXSize() 
 y=imLUT.getYSize() 
 # convert 32bit buffer to a Qt QImage 
 fmt = QtGui.QImage.Format_ARGB32 
 img = QtGui.QImage(sdata, x, y, fmt) 
 </code></pre>