Salome HOME
Add documentation for users and basic tutotial script examples (associated to the...
[modules/med.git] / src / MEDOP / tut / medcoupling / testmed_lena.py
1 #!/usr/bin/env python
2 #  -*- coding: iso-8859-1 -*-
3
4 # This use case illustrates the usage of PIL (Python Imaging Library)
5 # combined with the MEDCoupling and MEDLoader modules to save an image
6 # as a field in a med file.
7 # (gboulant - 27/06/2011)
8
9 import MEDCoupling as MC
10 import MEDLoader as ML
11
12 #
13 # ===============================================================
14 # We first get data from the test image to render as a field
15 # ===============================================================
16 #
17 import scipy, numpy
18 # The data field array may be created from the lena image
19 #image = scipy.lena()
20 # We could either read a real image using the PIL python package.
21 from scipy.misc import pilutil
22 image = pilutil.imread("images/avatar.png",True)
23
24
25 #from PIL import Image
26 #im=Image.open("images/irm.png")
27 #im=Image.open("images/lena.png")
28 #image=pilutil.fromimage(im,True)
29 #image=numpy.asarray(im)
30 #print image
31
32 dim  = len(image.shape)
33 print "Image space dimension = %d"%dim
34 sizeX = image.shape[1]
35 sizeY = image.shape[0]
36
37 # The sizes defined the number of pixel in a direction, then the
38 # number of cells to create in the mesh in that direction.
39
40 # We must reshape the matrix of pixel in a 1D vector that concatenates
41 # all the rows, and then convert this vector in a simple list of
42 # double as required by the MEDCoupling field specification.
43 import numpy
44 imageDataNArray       = image.reshape(1,sizeX*sizeY)[0]
45 print imageDataNArray
46
47 imageDataNArrayDouble = numpy.array(imageDataNArray, dtype='float64')
48 imageDataArrayDouble  = list(imageDataNArrayDouble)
49
50 #
51 # ===============================================================
52 # Creating a cartesian mesh with a grid of the size of the image
53 # ===============================================================
54 #
55
56 # >>>
57 # WARNING: remember the problem of tics and spaces. The data values
58 # are considered as values defined on cells. With size values in a
59 # direction, we have to create size+1 mesh nodes in that direction.
60 # <<<
61
62 # The mesh is created using MEDCoupling
63 cmesh=MC.MEDCouplingCMesh.New();
64 cmesh.setName("imagemesh")
65
66 # We use an arbitrary step between cells (the value does not matter)
67 stepX = 0.1
68 nbNodesX = sizeX+1
69 arrX = [float(i * stepX) for i in range(nbNodesX)]
70 coordsX=MC.DataArrayDouble.New()
71 coordsX.setValues(arrX,nbNodesX,1)
72
73 stepY = 0.1
74 nbNodesY = sizeY+1
75 arrY=[float(i * stepY) for i in range(nbNodesY)]
76 coordsY=MC.DataArrayDouble.New()
77 coordsY.setValues(arrY,nbNodesY,1)
78
79 cmesh.setCoords(coordsX,coordsY)
80 print "Imagem mesh dimension: %d"%cmesh.getSpaceDimension()
81
82 # WARN: In the current state of development of MEDLoader, only
83 # unstructured meshes are supported for writting function in med
84 # files. We just have to convert the cartesian mesh in an unstructured
85 # mesh before creating the field.
86 umesh=cmesh.buildUnstructured();
87 umesh.setName("imagemesh")
88
89 #
90 # ===============================================================
91 # Creating a scalar field on the mesh using image data
92 # ===============================================================
93 #
94
95 # Create the field using MEDCoupling
96 field = MC.MEDCouplingFieldDouble.New(MC.ON_CELLS,MC.ONE_TIME);
97 field.setName("imagefield");
98 field.setMesh(umesh);
99 # OPTIONAL: We set an arbitrary time step for test purpose
100 field.setIteration(3);
101 field.setOrder(0)
102
103 dataArray=MC.DataArrayDouble.New();
104 nbCells = sizeX*sizeY
105 nbComponents=1 # For a scalar field
106
107 # This example shows haw to initialize all cell with the same
108 # value. Just create an array of size nbCells
109 # dataArray.setValues(nbCells*[3.4],nbCells,nbComponents)
110
111 dataArray.setValues(imageDataArrayDouble,nbCells,nbComponents)
112 field.setArray(dataArray);
113
114 # The MEDLoader can be used to save all the stuff in a med file. You
115 # just have to specify the field and the MEDLoader will save the
116 # underlying mesh.
117 createFromScratch=True
118 ML.MEDLoader.WriteField("fieldimage.med",field,createFromScratch)