Actions
Morph-M Python - ImportExport to other python librairies » History » Revision 2
« Previous |
Revision 2/3
(diff)
| Next »
Serge Koudoro, 10/23/2009 04:06 PM
Morph-M Python - ImportExport to other python librairies¶
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.
PIL¶
Morph-M Gray images To PIL...
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)
... To Morph-M
imMorp=mp.ImCreate(imPIL.size[0],imPIL.size[1],"UINT8")
data = imPIL.tostring()
mp.ImageFromString(imMorp, data, 1, 0, colorType.GRAY)
Morph-M Color images to PIL...
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)
wxPython¶
Morph-M Images To WX
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)
Numpy(the replacement for Numeric/numarray)¶
Morph-M Images To Numpy....
import morphee as mp
import numpy
im = mp.fileRead("testGray.png")
arr = mp.ImageToNumarray(im)
... To Morph-M
imMorp=mp.ImCreate(arr.shape[0],arr.shape[1],"UINT8")
im.fromList( map(int,arr.flat) )
VTK images¶
Morph-M Images To VTK
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()
Qt4 Image¶
Morph-M To QT4: Gray level images
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)])
Morph-M To QT4: Color images
# 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)
Updated by Serge Koudoro over 14 years ago · 2 revisions