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