Project

General

Profile

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

Serge Koudoro, 10/23/2009 04:06 PM

1 1 Serge Koudoro
h1. Morph-M Python - ImportExport to other python librairies
2
3
h2{color:#8B0000;background:#ddd}. Introduction
4
5
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.
6
7
h2{color:#8B0000;background:#ddd}. PIL(Python Image Library)
8
9
bq. *%{color:#006400}+Morph-M Gray images To PIL...+%*
10
11 2 Serge Koudoro
<pre><code class="ruby">
12 1 Serge Koudoro
im = mp.fileRead("testGray.png")
13
imPIL = pil.new("L",(im.getWxSize(),im.getWySize()))
14
data =mp.ImageToString(im,1,0,mp.colorType.GRAY)
15
imPIL.fromstring(data)
16 2 Serge Koudoro
</code></pre>
17 1 Serge Koudoro
18
bq. *%{color:#006400}+... To Morph-M+%*
19
20 2 Serge Koudoro
<pre><code class="ruby">
21 1 Serge Koudoro
imMorp=mp.ImCreate(imPIL.size[0],imPIL.size[1],"UINT8")
22
data = imPIL.tostring()
23
mp.ImageFromString(imMorp, data, 1, 0, colorType.GRAY)
24 2 Serge Koudoro
</code></pre>
25 1 Serge Koudoro
26
bq. *%{color:#006400}+Morph-M Color images to PIL...+%*
27
28 2 Serge Koudoro
<pre><code class="ruby">
29 1 Serge Koudoro
import morphee as mp
30
import Image as pil
31
im = mp.fileRead("testRGB.png")
32
imPIL = pil.new("RGB",(im.getWxSize(),im.getWySize()))
33
data =mp.ImageToString(im,3,0,mp.colorType.ARGB)
34
imPIL.fromstring(data)
35 2 Serge Koudoro
</code></pre>
36 1 Serge Koudoro
37
h2{color:#8B0000;background:#ddd}. wxPython
38
39
bq. *%{color:#006400}+Morph-M Images To WX+%*
40
41 2 Serge Koudoro
<pre><code class="ruby">
42 1 Serge Koudoro
import morphee as mp
43
import wx
44
im = mp.fileRead("testRGB.png")
45
imWX = wx.EmptyImage(im.getWxSize(), im.getWySize())
46
data = mp.ImageToString(im,3,0,mp.colorType.ABGR)
47
imWX.SetData(data)
48 2 Serge Koudoro
</code></pre>
49 1 Serge Koudoro
50
h2{color:#8B0000;background:#ddd}. Numpy(the replacement for Numeric/numarray)
51
52
bq. *%{color:#006400}+Morph-M Images To Numpy....+%*
53
54 2 Serge Koudoro
<pre><code class="ruby">
55 1 Serge Koudoro
import morphee as mp
56
import numpy
57
im = mp.fileRead("testGray.png")
58
arr = mp.ImageToNumarray(im)
59 2 Serge Koudoro
</code></pre>
60 1 Serge Koudoro
61
bq. *%{color:#006400}+... To Morph-M+%*
62
63 2 Serge Koudoro
<pre><code class="ruby">
64 1 Serge Koudoro
imMorp=mp.ImCreate(arr.shape[0],arr.shape[1],"UINT8")
65
im.fromList( map(int,arr.flat) )
66 2 Serge Koudoro
</code></pre>
67 1 Serge Koudoro
68
h2{color:#8B0000;background:#ddd}. VTK images
69
70
bq. *%{color:#006400}+Morph-M Images To VTK+%*
71
72 2 Serge Koudoro
<pre><code class="ruby">
73 1 Serge Koudoro
import morphee as mp
74
import vtk
75
im = mp.fileRead("testGray.png")
76
str = mp.ImageToString(im,1,0,mp.colorType.GRAY)
77
x=im.getXSize()
78
y=im.getYSize()
79
z=im.getZSize()
80
imimport = vtk.vtkImageImport()
81
imimport.CopyImportVoidPointer(str,x*y*z)
82
imimport.SetDataScalarTypeToUnsignedChar()
83
imimport.SetDataExtent(0,x-1,0,y-1,0,z-1)
84
imimport.SetWholeExtent(0,x-1,0,y-1,0,z-1)
85
imdata = imimport.GetOutput()
86
imdata.Update()
87 2 Serge Koudoro
</code></pre>
88 1 Serge Koudoro
89
h2{color:#8B0000;background:#ddd}. Qt4 Image
90
91
bq. *%{color:#006400}+Morph-M To QT4: Gray level images+%*
92
93 2 Serge Koudoro
<pre><code class="ruby">
94 1 Serge Koudoro
import morphee as mp
95
from PyQt4 import QtGui
96
# create/read the image
97
im = mp.fileRead("testGray.png")
98
x=im.getXSize()
99
y=im.getYSize()
100
sdata=mp.ImageToString(im,1,(4 - x % 4) % 4,mp.colorType.GRAY)
101
fmt = QtGui.QImage.Format_Indexed8
102
img = QtGui.QImage(sdata, x, y, fmt)
103
# make sure the image is displayed in shades of gray
104
# (and not with a funky LUT)
105
img.setColorTable([QtGui.qRgb(i,i,i) for i in range(0,256)])
106 2 Serge Koudoro
</code></pre>
107 1 Serge Koudoro
108
bq. *%{color:#006400}+Morph-M To QT4: Color images+%*
109
110 2 Serge Koudoro
<pre><code class="ruby">
111 1 Serge Koudoro
# create/read the color image
112
im = mp.fileRead("testRGB.png")
113
# transfering the data to a qtpixmap
114
sdata = mp.ImageToString(imLUT,4,0,mp.colorType.ARGB)
115
x=imLUT.getXSize()
116
y=imLUT.getYSize()
117
# convert 32bit buffer to a Qt QImage
118
fmt = QtGui.QImage.Format_ARGB32
119
img = QtGui.QImage(sdata, x, y, fmt)
120 2 Serge Koudoro
</code></pre>