Salome HOME
Update copyrights
[modules/med.git] / doc / tut / medcoupling / testmed_lena.py
1 #!/usr/bin/env python3
2 # Copyright (C) 2011-2019  CEA/DEN, EDF R&D
3 #
4 # This library is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU Lesser General Public
6 # License as published by the Free Software Foundation; either
7 # version 2.1 of the License, or (at your option) any later version.
8 #
9 # This library is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12 # Lesser General Public License for more details.
13 #
14 # You should have received a copy of the GNU Lesser General Public
15 # License along with this library; if not, write to the Free Software
16 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
17 #
18 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
19 #
20
21 # This use case illustrates the usage of PIL (Python Imaging Library)
22 # combined with the MEDCoupling and MEDLoader modules to save an image
23 # as a field in a med file.
24 # (gboulant - 27/06/2011)
25
26 import os
27
28 import medcoupling as MC
29 import MEDLoader as ML
30
31 #
32 # ===============================================================
33 # We first get data from the test image to render as a field
34 # ===============================================================
35 #
36 import scipy, numpy
37 # The data field array may be created from the lena image
38 #image = scipy.lena()
39 # We could either read a real image using the PIL python package.
40 from scipy.misc import pilutil
41 CURDIR = os.path.dirname(__file__)
42 image = pilutil.imread(os.path.join(CURDIR, "images", "avatar.png"),True)
43
44
45 #from PIL import Image
46 #im=Image.open("images/irm.png")
47 #im=Image.open("images/lena.png")
48 #image=pilutil.fromimage(im,True)
49 #image=numpy.asarray(im)
50 # print(image)
51
52 dim  = len(image.shape)
53 print("Image space dimension = %d"%dim)
54 sizeX = image.shape[1]
55 sizeY = image.shape[0]
56
57 # The sizes defined the number of pixel in a direction, then the
58 # number of cells to create in the mesh in that direction.
59
60 # We must reshape the matrix of pixel in a 1D vector that concatenates
61 # all the rows, and then convert this vector in a simple list of
62 # double as required by the MEDCoupling field specification.
63 import numpy
64 imageDataNArray       = image.reshape(1,sizeX*sizeY)[0]
65 print(imageDataNArray)
66
67 imageDataNArrayDouble = numpy.array(imageDataNArray, dtype='float64')
68 imageDataArrayDouble  = list(imageDataNArrayDouble)
69
70 #
71 # ===============================================================
72 # Creating a cartesian mesh with a grid of the size of the image
73 # ===============================================================
74 #
75
76 # >>>
77 # WARNING: remember the problem of tics and spaces. The data values
78 # are considered as values defined on cells. With size values in a
79 # direction, we have to create size+1 mesh nodes in that direction.
80 # <<<
81
82 # The mesh is created using MEDCoupling
83 cmesh=MC.MEDCouplingCMesh.New();
84 cmesh.setName("imagemesh")
85
86 # We use an arbitrary step between cells (the value does not matter)
87 stepX = 0.1
88 nbNodesX = sizeX+1
89 arrX = [float(i * stepX) for i in range(nbNodesX)]
90 coordsX=MC.DataArrayDouble.New()
91 coordsX.setValues(arrX,nbNodesX,1)
92
93 stepY = 0.1
94 nbNodesY = sizeY+1
95 arrY=[float(i * stepY) for i in range(nbNodesY)]
96 coordsY=MC.DataArrayDouble.New()
97 coordsY.setValues(arrY,nbNodesY,1)
98
99 cmesh.setCoords(coordsX,coordsY)
100 print("Imagem mesh dimension: %d"%cmesh.getSpaceDimension())
101
102 # WARN: In the current state of development of MEDLoader, only
103 # unstructured meshes are supported for writing function in med
104 # files. We just have to convert the cartesian mesh in an unstructured
105 # mesh before creating the field.
106 umesh=cmesh.buildUnstructured();
107 umesh.setName("imagemesh")
108
109 #
110 # ===============================================================
111 # Creating a scalar field on the mesh using image data
112 # ===============================================================
113 #
114
115 # Create the field using MEDCoupling
116 field = MC.MEDCouplingFieldDouble.New(MC.ON_CELLS,MC.ONE_TIME);
117 field.setName("imagefield");
118 field.setMesh(umesh);
119 # OPTIONAL: We set an arbitrary time step for test purpose
120 field.setIteration(3);
121 field.setOrder(0)
122
123 dataArray=MC.DataArrayDouble.New();
124 nbCells = sizeX*sizeY
125 nbComponents=1 # For a scalar field
126
127 # This example shows haw to initialize all cell with the same
128 # value. Just create an array of size nbCells
129 # dataArray.setValues(nbCells*[3.4],nbCells,nbComponents)
130
131 dataArray.setValues(imageDataArrayDouble,nbCells,nbComponents)
132 field.setArray(dataArray);
133
134 # The MEDLoader can be used to save all the stuff in a med file. You
135 # just have to specify the field and the MEDLoader will save the
136 # underlying mesh.
137 createFromScratch=True
138 ML.MEDLoader.WriteField("fieldimage.med",field,createFromScratch)